1 package com.android.launcher3.icons;
2 
3 import android.content.res.Resources;
4 import android.content.res.Resources.Theme;
5 import android.graphics.Canvas;
6 import android.graphics.drawable.ColorDrawable;
7 import android.graphics.drawable.DrawableWrapper;
8 import android.util.AttributeSet;
9 
10 import org.xmlpull.v1.XmlPullParser;
11 
12 /**
13  * Extension of {@link DrawableWrapper} which scales the child drawables by a fixed amount.
14  */
15 public class FixedScaleDrawable extends DrawableWrapper {
16 
17     // TODO b/33553066 use the constant defined in MaskableIconDrawable
18     private static final float LEGACY_ICON_SCALE = .7f * .6667f;
19     private float mScaleX, mScaleY;
20 
FixedScaleDrawable()21     public FixedScaleDrawable() {
22         super(new ColorDrawable());
23         mScaleX = LEGACY_ICON_SCALE;
24         mScaleY = LEGACY_ICON_SCALE;
25     }
26 
27     @Override
draw(Canvas canvas)28     public void draw(Canvas canvas) {
29         int saveCount = canvas.save();
30         canvas.scale(mScaleX, mScaleY,
31                 getBounds().exactCenterX(), getBounds().exactCenterY());
32         super.draw(canvas);
33         canvas.restoreToCount(saveCount);
34     }
35 
36     @Override
inflate(Resources r, XmlPullParser parser, AttributeSet attrs)37     public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs) { }
38 
39     @Override
inflate(Resources r, XmlPullParser parser, AttributeSet attrs, Theme theme)40     public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs, Theme theme) { }
41 
setScale(float scale)42     public void setScale(float scale) {
43         float h = getIntrinsicHeight();
44         float w = getIntrinsicWidth();
45         mScaleX = scale * LEGACY_ICON_SCALE;
46         mScaleY = scale * LEGACY_ICON_SCALE;
47         if (h > w && w > 0) {
48             mScaleX *= w / h;
49         } else if (w > h && h > 0) {
50             mScaleY *= h / w;
51         }
52     }
53 }
54