OSDN Git Service

When creating bitmap, do not apply width/height ratio if AdaptiveIconDrawable
authorHyunyoung Song <hyunyoungs@google.com>
Tue, 25 Apr 2017 18:20:05 +0000 (11:20 -0700)
committerHyunyoung Song <hyunyoungs@google.com>
Thu, 27 Apr 2017 21:19:58 +0000 (14:19 -0700)
b/37670867

Change-Id: I60884c52e7eb4b2d6f0ae2a5dc8e7c730f1f2454

src/com/android/launcher3/graphics/FixedScaleDrawable.java
src/com/android/launcher3/graphics/LauncherIcons.java

index 7ee3d80..262a95e 100644 (file)
@@ -19,17 +19,18 @@ public class FixedScaleDrawable extends DrawableWrapper {
 
     // TODO b/33553066 use the constant defined in MaskableIconDrawable
     private static final float LEGACY_ICON_SCALE = .7f * .6667f;
-    private float mScale;
+    private float mScaleX, mScaleY;
 
     public FixedScaleDrawable() {
         super(new ColorDrawable());
-        mScale = LEGACY_ICON_SCALE;
+        mScaleX = LEGACY_ICON_SCALE;
+        mScaleY = LEGACY_ICON_SCALE;
     }
 
     @Override
     public void draw(Canvas canvas) {
         int saveCount = canvas.save(Canvas.MATRIX_SAVE_FLAG);
-        canvas.scale(mScale, mScale,
+        canvas.scale(mScaleX, mScaleY,
                 getBounds().exactCenterX(), getBounds().exactCenterY());
         super.draw(canvas);
         canvas.restoreToCount(saveCount);
@@ -42,6 +43,14 @@ public class FixedScaleDrawable extends DrawableWrapper {
     public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs, Theme theme) { }
 
     public void setScale(float scale) {
-        mScale = scale * LEGACY_ICON_SCALE;
+        float h = getIntrinsicHeight();
+        float w = getIntrinsicWidth();
+        mScaleX = scale * LEGACY_ICON_SCALE;
+        mScaleY = scale * LEGACY_ICON_SCALE;
+        if (h > w && w > 0) {
+            mScaleX *= w / h;
+        } else if (w > h && h > 0) {
+            mScaleY *= h / w;
+        }
     }
 }
index e671a82..746a639 100644 (file)
@@ -252,7 +252,6 @@ public class LauncherIcons {
                     width = (int) (height * ratio);
                 }
             }
-
             // no intrinsic size --> use default size
             int textureWidth = iconBitmapSize;
             int textureHeight = iconBitmapSize;
@@ -266,7 +265,13 @@ public class LauncherIcons {
             final int top = (textureHeight-height) / 2;
 
             sOldBounds.set(icon.getBounds());
-            icon.setBounds(left, top, left+width, top+height);
+            if (icon instanceof AdaptiveIconDrawable) {
+                int offset = Math.min(left, top);
+                int size = Math.max(width, height);
+                icon.setBounds(offset, offset, offset + size, offset + size);
+            } else {
+                icon.setBounds(left, top, left+width, top+height);
+            }
             canvas.save(Canvas.MATRIX_SAVE_FLAG);
             canvas.scale(scale, scale, textureWidth / 2, textureHeight / 2);
             icon.draw(canvas);
@@ -289,16 +294,13 @@ public class LauncherIcons {
         }
 
         try {
-            Class clazz = Class.forName("android.graphics.drawable.AdaptiveIconDrawable");
-            if (!clazz.isAssignableFrom(drawable.getClass())) {
-                Drawable iconWrapper =
+            if (!(drawable instanceof AdaptiveIconDrawable)) {
+                AdaptiveIconDrawable iconWrapper = (AdaptiveIconDrawable)
                         context.getDrawable(R.drawable.adaptive_icon_drawable_wrapper).mutate();
-                FixedScaleDrawable fsd = ((FixedScaleDrawable) clazz.getMethod("getForeground")
-                        .invoke(iconWrapper));
+                FixedScaleDrawable fsd = ((FixedScaleDrawable) iconWrapper.getForeground());
                 fsd.setDrawable(drawable);
                 fsd.setScale(scale);
-
-                return iconWrapper;
+                return (Drawable) iconWrapper;
             }
         } catch (Exception e) {
             return drawable;