OSDN Git Service

Fix image icons on Razr M.
authorRuben Brunk <rubenbrunk@google.com>
Thu, 14 Mar 2013 20:47:51 +0000 (13:47 -0700)
committerRuben Brunk <rubenbrunk@google.com>
Thu, 14 Mar 2013 21:04:32 +0000 (14:04 -0700)
Change-Id: I9f47dd043055fb5dc25dda643a3e9c81356bd0d5

src/com/android/gallery3d/filtershow/ui/FilterIconButton.java
src/com/android/gallery3d/filtershow/ui/IconButton.java

index 9d50d5a..59c18e0 100644 (file)
@@ -19,13 +19,13 @@ package com.android.gallery3d.filtershow.ui;
 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;
@@ -33,6 +33,7 @@ import com.android.gallery3d.filtershow.imageshow.GeometryListener;
 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";
@@ -43,9 +44,6 @@ public class FilterIconButton extends IconButton implements View.OnClickListener
     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);
     }
@@ -68,27 +66,6 @@ public class FilterIconButton extends IconButton implements View.OnClickListener
     }
 
     @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;
     }
@@ -117,31 +94,55 @@ public class FilterIconButton extends IconButton implements View.OnClickListener
         }
         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);
+        }
     }
 }
index ed10be3..2484d5f 100644 (file)
@@ -30,10 +30,10 @@ import android.widget.Button;
  */
 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);
@@ -53,7 +53,9 @@ public class IconButton extends Button {
     }
 
     /**
-     * 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.
      */
@@ -68,7 +70,7 @@ public class IconButton extends Button {
      *
      * @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));
@@ -81,7 +83,7 @@ public class IconButton extends Button {
      *
      * @param image bitmap to set the icon to.
      */
-    protected boolean setImageIcon(Bitmap image) {
+    private boolean setImageIcon(Bitmap image) {
         if (image == null) {
             return false;
         }
@@ -99,11 +101,11 @@ public class IconButton extends Button {
      * @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;
     }
 
@@ -113,7 +115,7 @@ public class IconButton extends Button {
      *
      * @return icon side length
      */
-    protected int getGoodIconSideSize() {
+    private int getGoodIconSideSize() {
         Paint p = getPaint();
         Rect bounds = new Rect();
         String s = getText().toString();
@@ -128,7 +130,9 @@ public class IconButton extends Button {
     @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
@@ -140,12 +144,20 @@ public class IconButton extends Button {
         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;
@@ -160,9 +172,8 @@ public class IconButton extends Button {
                 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;
     }
 
 }