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