import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
+import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import com.android.gallery3d.filtershow.PanelController;
-import com.android.gallery3d.filtershow.cache.ImageLoader;
import com.android.gallery3d.filtershow.cache.RenderingRequest;
import com.android.gallery3d.filtershow.cache.RenderingRequestCaller;
import com.android.gallery3d.filtershow.filters.FilterRepresentation;
import com.android.gallery3d.filtershow.imageshow.MasterImage;
import com.android.gallery3d.filtershow.presets.ImagePreset;
+// TODO: merge back IconButton and FilterIconButton?
public class FilterIconButton extends IconButton implements View.OnClickListener,
RenderingRequestCaller, GeometryListener {
private static final String LOGTAG = "FilterIconButton";
private LinearLayout mParentContainer = null;
private View.OnClickListener mListener = null;
private Bitmap mIconBitmap = null;
- private ImagePreset mPreset = null;
- private Rect mDestination = null;
-
public FilterIconButton(Context context) {
super(context);
}
}
@Override
- protected Bitmap drawImage(Bitmap dst, Bitmap image, Rect destination) {
- if (mOverlayOnly) {
- // TODO: merge back IconButton and FilterIconButton
- return super.drawImage(dst, image, destination);
- }
- if (mIconBitmap == null && mPreset == null) {
- dst = MasterImage.getImage().getThumbnailBitmap();
- if (dst != null) {
- ImagePreset mPreset = new ImagePreset();
- mPreset.addFilter(mFilterRepresentation);
- mPreset.setDoApplyGeometry(false);
- mDestination = destination;
- RenderingRequest.post(dst.copy(Bitmap.Config.ARGB_8888, true), mPreset, RenderingRequest.ICON_RENDERING, this);
- }
- return dst;
- } else {
- return mIconBitmap;
- }
- }
-
- @Override
public void setOnClickListener(View.OnClickListener listener) {
mListener = listener;
}
}
mOverlayOnly = mFilterRepresentation.getOverlayOnly();
if (mOverlayOnly) {
+ assert(mOverlayBitmap != null);
setIcon(mOverlayBitmap);
}
- stale_icon = true;
invalidate();
}
@Override
+ protected void onDraw(Canvas canvas) {
+ if (mIconBitmap == null && !mOverlayOnly) {
+ postNewIconRenderRequest();
+ }
+ super.onDraw(canvas);
+ }
+
+ @Override
public void available(RenderingRequest request) {
- if (request.getBitmap() == null) {
+ Bitmap bmap = request.getBitmap();
+ if (bmap == null) {
return;
}
- mIconBitmap = request.getBitmap();
- if (mOverlayBitmap != null) {
- mIconBitmap = super.drawImage(mIconBitmap, mOverlayBitmap, mDestination);
+ if (mOverlayOnly) {
+ setIcon(mOverlayBitmap);
+ } else {
+ mIconBitmap = bmap;
+ if (mOverlayBitmap != null) {
+ Rect destination = new Rect(0, 0, mIconBitmap.getWidth(), mIconBitmap.getHeight());
+ drawImage(mIconBitmap, mOverlayBitmap, destination);
+ }
+ setIcon(mIconBitmap);
}
- stale_icon = true;
- invalidate();
}
@Override
public void geometryChanged() {
- stale_icon = true;
-
+ if (mOverlayOnly) {
+ return;
+ }
mIconBitmap = null;
- mPreset = null;
- invalidate();
+ postNewIconRenderRequest();
+ }
+
+ private void postNewIconRenderRequest() {
+ Bitmap dst = MasterImage.getImage().getThumbnailBitmap();
+ if (dst != null) {
+ ImagePreset mPreset = new ImagePreset();
+ mPreset.addFilter(mFilterRepresentation);
+ mPreset.setDoApplyGeometry(false);
+ RenderingRequest.post(dst.copy(Bitmap.Config.ARGB_8888, true),
+ mPreset, RenderingRequest.ICON_RENDERING, this);
+ }
}
}
*/
public class IconButton extends Button {
- protected Bitmap mImageMirror = null;
- protected Bitmap mIcon = null;
+ private Bitmap mImageMirror = null;
+ private Bitmap mIcon = null;
- protected boolean stale_icon = true;
+ private boolean stale_icon = true;
public IconButton(Context context) {
this(context, null);
}
/**
- * Set the image that the button icon will use.
+ * Set the image that the button icon will use. The image bitmap will be scaled
+ * and cropped into the largest square bitmap that will fit cleanly within the
+ * IconButton's layout.
*
* @param image image that icon will be set to before next draw.
*/
*
* @param image bitmap to use as icon
*/
- protected boolean makeAndSetIcon(Bitmap image) {
+ private boolean makeAndSetIcon(Bitmap image) {
int size = getGoodIconSideSize();
if (size > 0) {
return setImageIcon(makeImageIcon(image, size, size));
*
* @param image bitmap to set the icon to.
*/
- protected boolean setImageIcon(Bitmap image) {
+ private boolean setImageIcon(Bitmap image) {
if (image == null) {
return false;
}
* @param height icon height
* @return the scaled/cropped icon bitmap
*/
- protected Bitmap makeImageIcon(Bitmap image, int width, int height) {
+ private Bitmap makeImageIcon(Bitmap image, int width, int height) {
Rect destination = new Rect(0, 0, width, height);
Bitmap bmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
- bmap = drawImage(bmap, image, destination);
+ drawImage(bmap, image, destination);
return bmap;
}
*
* @return icon side length
*/
- protected int getGoodIconSideSize() {
+ private int getGoodIconSideSize() {
Paint p = getPaint();
Rect bounds = new Rect();
String s = getText().toString();
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
- stale_icon = true;
+ if (w != oldw || h != oldh) {
+ stale_icon = true;
+ }
}
@Override
super.onDraw(canvas);
}
- // Override this for custom icon generation
- protected Bitmap drawImage(Bitmap dst, Bitmap image, Rect destination) {
- if (image != null) {
+ /**
+ * Draws the src image into the destination rectangle within the dst bitmap.
+ * If src is a non-square image, clips to be a square before drawing into dst.
+ *
+ * @param dst bitmap being drawn on.
+ * @param src bitmap to draw into dst.
+ * @param destination square in dst in which to draw src.
+ */
+ protected static void drawImage(Bitmap dst, Bitmap src, Rect destination) {
+ if (src != null && dst != null && src.getWidth() > 0 && dst.getWidth() > 0
+ && src.getHeight() > 0 && dst.getHeight() > 0) {
Canvas canvas = new Canvas(dst);
- int iw = image.getWidth();
- int ih = image.getHeight();
+ int iw = src.getWidth();
+ int ih = src.getHeight();
int x = 0;
int y = 0;
int size = 0;
y = (int) ((ih - size) / 2.0f);
}
source = new Rect(x, y, x + size, y + size);
- canvas.drawBitmap(image, source, destination, new Paint());
+ canvas.drawBitmap(src, source, destination, new Paint());
}
- return dst;
}
}