blob: 516965ec2ba2fb6f30723e030a9b4f58fed33fe4 [file] [log] [blame]
Aurimas Liutikas88c7ff12023-08-10 12:42:26 -07001package com.android.launcher3.icons;
2
3import android.content.res.Resources;
4import android.content.res.Resources.Theme;
5import android.graphics.Canvas;
6import android.graphics.drawable.ColorDrawable;
7import android.graphics.drawable.DrawableWrapper;
8import android.util.AttributeSet;
9
10import org.xmlpull.v1.XmlPullParser;
11
12/**
13 * Extension of {@link DrawableWrapper} which scales the child drawables by a fixed amount.
14 */
15public 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
21 public FixedScaleDrawable() {
22 super(new ColorDrawable());
23 mScaleX = LEGACY_ICON_SCALE;
24 mScaleY = LEGACY_ICON_SCALE;
25 }
26
27 @Override
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
37 public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs) { }
38
39 @Override
40 public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs, Theme theme) { }
41
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}