blob: d3b86ded80db1cceb0f0d2da9b6eec286b33ae7b [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Sunny Goyal726bee72018-03-05 12:54:24 -080019import static com.android.launcher3.anim.Interpolators.ACCEL;
Hyunyoung Songef468d82019-01-03 01:02:43 -080020import static com.android.launcher3.anim.Interpolators.DEACCEL;
Sunny Goyal726bee72018-03-05 12:54:24 -080021
Sunny Goyal508da152014-08-14 10:53:27 -070022import android.animation.ObjectAnimator;
Sunny Goyal14168432019-10-24 15:59:49 -070023import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.graphics.Bitmap;
25import android.graphics.Canvas;
Sunny Goyal508da152014-08-14 10:53:27 -070026import android.graphics.Color;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.graphics.ColorFilter;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070028import android.graphics.ColorMatrix;
29import android.graphics.ColorMatrixColorFilter;
Winson Chung45e1d6e2010-11-09 17:19:49 -080030import android.graphics.Paint;
31import android.graphics.PixelFormat;
Sunny Goyal726bee72018-03-05 12:54:24 -080032import android.graphics.Rect;
Winson Chung45e1d6e2010-11-09 17:19:49 -080033import android.graphics.drawable.Drawable;
Tony Wickham1e618492017-02-02 12:57:18 -080034import android.util.Property;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035
Sunny Goyal14168432019-10-24 15:59:49 -070036import com.android.launcher3.graphics.PlaceHolderIconDrawable;
Hyunyoung Song48cb7bc2018-09-25 17:03:34 -070037import com.android.launcher3.icons.BitmapInfo;
Sunny Goyale396abf2020-04-06 15:11:17 -070038import com.android.launcher3.model.data.ItemInfoWithIcon;
Samuel Fufa61bc63a2020-06-05 12:17:16 -070039import com.android.launcher3.util.Themes;
Tony Wickham9a8d11f2017-01-11 09:53:12 -080040
Samuel Fufa61d39642020-01-23 12:42:06 -080041
Hyunyoung Song3f471442015-04-08 19:01:34 -070042public class FastBitmapDrawable extends Drawable {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080043
Sunny Goyal726bee72018-03-05 12:54:24 -080044 private static final float PRESSED_SCALE = 1.1f;
45
Tony Wickham6b910a22016-11-08 10:40:34 -080046 private static final float DISABLED_DESATURATION = 1f;
47 private static final float DISABLED_BRIGHTNESS = 0.5f;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070048
Sunny Goyal726bee72018-03-05 12:54:24 -080049 public static final int CLICK_FEEDBACK_DURATION = 200;
Sunny Goyal508da152014-08-14 10:53:27 -070050
Samuel Fufa61d39642020-01-23 12:42:06 -080051 private static ColorFilter sDisabledFColorFilter;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070052
Sunny Goyal55cb70b2016-11-12 09:58:29 -080053 protected final Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG);
Sunny Goyal61e08462018-03-02 17:25:59 -080054 protected Bitmap mBitmap;
Sunny Goyal179249d2017-12-19 16:49:24 -080055 protected final int mIconColor;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080056
57 private boolean mIsPressed;
Tony Wickham6b910a22016-11-08 10:40:34 -080058 private boolean mIsDisabled;
Samuel Fufa61bc63a2020-06-05 12:17:16 -070059 private float mDisabledAlpha = 1f;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070060
Sunny Goyal726bee72018-03-05 12:54:24 -080061 // Animator and properties for the fast bitmap drawable's scale
62 private static final Property<FastBitmapDrawable, Float> SCALE
63 = new Property<FastBitmapDrawable, Float>(Float.TYPE, "scale") {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080064 @Override
65 public Float get(FastBitmapDrawable fastBitmapDrawable) {
Sunny Goyal726bee72018-03-05 12:54:24 -080066 return fastBitmapDrawable.mScale;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080067 }
68
69 @Override
70 public void set(FastBitmapDrawable fastBitmapDrawable, Float value) {
Sunny Goyal726bee72018-03-05 12:54:24 -080071 fastBitmapDrawable.mScale = value;
72 fastBitmapDrawable.invalidateSelf();
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080073 }
74 };
Sunny Goyal726bee72018-03-05 12:54:24 -080075 private ObjectAnimator mScaleAnimation;
76 private float mScale = 1;
77
Winsonc0880492015-08-21 11:16:27 -070078 private int mAlpha = 255;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079
Hyunyoung Song3f471442015-04-08 19:01:34 -070080 public FastBitmapDrawable(Bitmap b) {
Sunny Goyal179249d2017-12-19 16:49:24 -080081 this(b, Color.TRANSPARENT);
82 }
83
84 public FastBitmapDrawable(BitmapInfo info) {
85 this(info.icon, info.color);
86 }
87
Sunny Goyal179249d2017-12-19 16:49:24 -080088 protected FastBitmapDrawable(Bitmap b, int iconColor) {
Jon Mirandab6d686d2019-03-29 10:49:43 -070089 this(b, iconColor, false);
90 }
91
92 protected FastBitmapDrawable(Bitmap b, int iconColor, boolean isDisabled) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093 mBitmap = b;
Sunny Goyal179249d2017-12-19 16:49:24 -080094 mIconColor = iconColor;
Sunny Goyal96ac68a2017-02-02 16:37:21 -080095 setFilterBitmap(true);
Jon Mirandab6d686d2019-03-29 10:49:43 -070096 setIsDisabled(isDisabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097 }
98
99 @Override
Sunny Goyal726bee72018-03-05 12:54:24 -0800100 public final void draw(Canvas canvas) {
Matthew Ngeb9cc9d2018-06-25 15:32:24 -0700101 if (mScale != 1f) {
Sunny Goyal726bee72018-03-05 12:54:24 -0800102 int count = canvas.save();
103 Rect bounds = getBounds();
104 canvas.scale(mScale, mScale, bounds.exactCenterX(), bounds.exactCenterY());
105 drawInternal(canvas, bounds);
106 canvas.restoreToCount(count);
107 } else {
108 drawInternal(canvas, getBounds());
109 }
110 }
111
112 protected void drawInternal(Canvas canvas, Rect bounds) {
113 canvas.drawBitmap(mBitmap, null, bounds, mPaint);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114 }
115
116 @Override
Adam Cohenbadf71e2011-05-26 19:08:29 -0700117 public void setColorFilter(ColorFilter cf) {
Alistair Delva087a9e32020-10-05 14:46:26 +0000118 // No op
Adam Cohenbadf71e2011-05-26 19:08:29 -0700119 }
120
121 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122 public int getOpacity() {
123 return PixelFormat.TRANSLUCENT;
124 }
125
126 @Override
127 public void setAlpha(int alpha) {
Jon Mirandadff0de42019-08-30 18:42:01 -0700128 if (mAlpha != alpha) {
129 mAlpha = alpha;
130 mPaint.setAlpha(alpha);
131 invalidateSelf();
132 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133 }
134
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700135 @Override
Adam Cohen76fc0852011-06-17 13:26:23 -0700136 public void setFilterBitmap(boolean filterBitmap) {
137 mPaint.setFilterBitmap(filterBitmap);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700138 mPaint.setAntiAlias(filterBitmap);
Adam Cohen76fc0852011-06-17 13:26:23 -0700139 }
140
Winson Chung29d6fea2010-12-01 15:47:31 -0800141 public int getAlpha() {
142 return mAlpha;
143 }
144
Matthew Ngeb9cc9d2018-06-25 15:32:24 -0700145 public void setScale(float scale) {
146 if (mScaleAnimation != null) {
147 mScaleAnimation.cancel();
148 mScaleAnimation = null;
149 }
150 mScale = scale;
151 invalidateSelf();
152 }
153
Sunny Goyal726bee72018-03-05 12:54:24 -0800154 public float getAnimatedScale() {
155 return mScaleAnimation == null ? 1 : mScale;
156 }
157
Matthew Ngeb9cc9d2018-06-25 15:32:24 -0700158 public float getScale() {
159 return mScale;
160 }
161
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 public int getIntrinsicWidth() {
Sunny Goyalc424f222014-09-05 07:04:59 -0700164 return mBitmap.getWidth();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 }
166
167 @Override
168 public int getIntrinsicHeight() {
Sunny Goyalc424f222014-09-05 07:04:59 -0700169 return mBitmap.getHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170 }
171
172 @Override
173 public int getMinimumWidth() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800174 return getBounds().width();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 }
176
177 @Override
178 public int getMinimumHeight() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800179 return getBounds().height();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800180 }
181
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800182 @Override
183 public boolean isStateful() {
184 return true;
Winsonc0880492015-08-21 11:16:27 -0700185 }
186
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800187 @Override
Sunny Goyal28141122017-06-21 17:28:23 -0700188 public ColorFilter getColorFilter() {
189 return mPaint.getColorFilter();
190 }
191
192 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800193 protected boolean onStateChange(int[] state) {
194 boolean isPressed = false;
195 for (int s : state) {
196 if (s == android.R.attr.state_pressed) {
197 isPressed = true;
198 break;
199 }
200 }
201 if (mIsPressed != isPressed) {
202 mIsPressed = isPressed;
Winsonc0880492015-08-21 11:16:27 -0700203
Sunny Goyal726bee72018-03-05 12:54:24 -0800204 if (mScaleAnimation != null) {
205 mScaleAnimation.cancel();
206 mScaleAnimation = null;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800207 }
Winsonc0880492015-08-21 11:16:27 -0700208
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800209 if (mIsPressed) {
210 // Animate when going to pressed state
Sunny Goyal726bee72018-03-05 12:54:24 -0800211 mScaleAnimation = ObjectAnimator.ofFloat(this, SCALE, PRESSED_SCALE);
212 mScaleAnimation.setDuration(CLICK_FEEDBACK_DURATION);
213 mScaleAnimation.setInterpolator(ACCEL);
214 mScaleAnimation.start();
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800215 } else {
Hyunyoung Songef468d82019-01-03 01:02:43 -0800216 if (isVisible()) {
217 mScaleAnimation = ObjectAnimator.ofFloat(this, SCALE, 1f);
218 mScaleAnimation.setDuration(CLICK_FEEDBACK_DURATION);
219 mScaleAnimation.setInterpolator(DEACCEL);
220 mScaleAnimation.start();
221 } else {
222 mScale = 1f;
223 invalidateSelf();
224 }
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800225 }
Winsonc0880492015-08-21 11:16:27 -0700226 return true;
227 }
228 return false;
229 }
230
Tony Wickham6b910a22016-11-08 10:40:34 -0800231 public void setIsDisabled(boolean isDisabled) {
232 if (mIsDisabled != isDisabled) {
233 mIsDisabled = isDisabled;
Samuel Fufa61d39642020-01-23 12:42:06 -0800234 updateFilter();
Tony Wickham6b910a22016-11-08 10:40:34 -0800235 }
236 }
237
Jon Mirandab6d686d2019-03-29 10:49:43 -0700238 protected boolean isDisabled() {
239 return mIsDisabled;
240 }
241
Samuel Fufa61d39642020-01-23 12:42:06 -0800242 private ColorFilter getDisabledColorFilter() {
243 if (sDisabledFColorFilter == null) {
244 ColorMatrix tempBrightnessMatrix = new ColorMatrix();
245 ColorMatrix tempFilterMatrix = new ColorMatrix();
246
247 tempFilterMatrix.setSaturation(1f - DISABLED_DESATURATION);
248 float scale = 1 - DISABLED_BRIGHTNESS;
249 int brightnessI = (int) (255 * DISABLED_BRIGHTNESS);
250 float[] mat = tempBrightnessMatrix.getArray();
251 mat[0] = scale;
252 mat[6] = scale;
253 mat[12] = scale;
254 mat[4] = brightnessI;
255 mat[9] = brightnessI;
256 mat[14] = brightnessI;
Samuel Fufa61bc63a2020-06-05 12:17:16 -0700257 mat[18] = mDisabledAlpha;
Samuel Fufa61d39642020-01-23 12:42:06 -0800258 tempFilterMatrix.preConcat(tempBrightnessMatrix);
259 sDisabledFColorFilter = new ColorMatrixColorFilter(tempFilterMatrix);
Sunny Goyal95abbb32014-08-04 10:53:22 -0700260 }
Samuel Fufa61d39642020-01-23 12:42:06 -0800261 return sDisabledFColorFilter;
Winsonc0880492015-08-21 11:16:27 -0700262 }
263
264 /**
265 * Updates the paint to reflect the current brightness and saturation.
266 */
Sunny Goyal0ffab442018-06-07 17:31:48 -0700267 protected void updateFilter() {
Alistair Delva087a9e32020-10-05 14:46:26 +0000268 mPaint.setColorFilter(mIsDisabled ? getDisabledColorFilter() : null);
Winsonc0880492015-08-21 11:16:27 -0700269 invalidateSelf();
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700270 }
Sunny Goyal338d15d2018-02-23 12:19:44 -0800271
272 @Override
273 public ConstantState getConstantState() {
Jon Mirandab6d686d2019-03-29 10:49:43 -0700274 return new MyConstantState(mBitmap, mIconColor, mIsDisabled);
Sunny Goyal338d15d2018-02-23 12:19:44 -0800275 }
276
Sunny Goyal61e08462018-03-02 17:25:59 -0800277 protected static class MyConstantState extends ConstantState {
278 protected final Bitmap mBitmap;
279 protected final int mIconColor;
Jon Mirandab6d686d2019-03-29 10:49:43 -0700280 protected final boolean mIsDisabled;
Sunny Goyal338d15d2018-02-23 12:19:44 -0800281
Jon Mirandab6d686d2019-03-29 10:49:43 -0700282 public MyConstantState(Bitmap bitmap, int color, boolean isDisabled) {
Sunny Goyal338d15d2018-02-23 12:19:44 -0800283 mBitmap = bitmap;
284 mIconColor = color;
Jon Mirandab6d686d2019-03-29 10:49:43 -0700285 mIsDisabled = isDisabled;
Sunny Goyal338d15d2018-02-23 12:19:44 -0800286 }
287
288 @Override
Sunny Goyal14168432019-10-24 15:59:49 -0700289 public FastBitmapDrawable newDrawable() {
Jon Mirandab6d686d2019-03-29 10:49:43 -0700290 return new FastBitmapDrawable(mBitmap, mIconColor, mIsDisabled);
Sunny Goyal338d15d2018-02-23 12:19:44 -0800291 }
292
293 @Override
294 public int getChangingConfigurations() {
295 return 0;
296 }
297 }
Sunny Goyal14168432019-10-24 15:59:49 -0700298
299 /**
300 * Interface to be implemented by custom {@link BitmapInfo} to handle drawable construction
301 */
302 public interface Factory {
303
304 /**
305 * Called to create a new drawable
306 */
307 FastBitmapDrawable newDrawable();
308 }
309
310 /**
311 * Returns a FastBitmapDrawable with the icon.
312 */
313 public static FastBitmapDrawable newIcon(Context context, ItemInfoWithIcon info) {
314 FastBitmapDrawable drawable = newIcon(context, info.bitmap);
315 drawable.setIsDisabled(info.isDisabled());
316 return drawable;
317 }
318
319 /**
320 * Creates a drawable for the provided BitmapInfo
321 */
322 public static FastBitmapDrawable newIcon(Context context, BitmapInfo info) {
Samuel Fufa61bc63a2020-06-05 12:17:16 -0700323 final FastBitmapDrawable drawable;
Sunny Goyal14168432019-10-24 15:59:49 -0700324 if (info instanceof Factory) {
Samuel Fufa61bc63a2020-06-05 12:17:16 -0700325 drawable = ((Factory) info).newDrawable();
Sunny Goyal14168432019-10-24 15:59:49 -0700326 } else if (info.isLowRes()) {
Samuel Fufa61bc63a2020-06-05 12:17:16 -0700327 drawable = new PlaceHolderIconDrawable(info, context);
Sunny Goyal14168432019-10-24 15:59:49 -0700328 } else {
Samuel Fufa61bc63a2020-06-05 12:17:16 -0700329 drawable = new FastBitmapDrawable(info);
Sunny Goyal14168432019-10-24 15:59:49 -0700330 }
Samuel Fufa61bc63a2020-06-05 12:17:16 -0700331 drawable.mDisabledAlpha = Themes.getFloat(context, R.attr.disabledIconAlpha, 1f);
332 return drawable;
Sunny Goyal14168432019-10-24 15:59:49 -0700333 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800334}