blob: 50916840d91e1c72262cb359b76535c350f3761d [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 Goyal508da152014-08-14 10:53:27 -070032import android.graphics.PorterDuff;
33import android.graphics.PorterDuffColorFilter;
Sunny Goyal726bee72018-03-05 12:54:24 -080034import android.graphics.Rect;
Winson Chung45e1d6e2010-11-09 17:19:49 -080035import android.graphics.drawable.Drawable;
Tony Wickham1e618492017-02-02 12:57:18 -080036import android.util.Property;
Sunny Goyal508da152014-08-14 10:53:27 -070037import android.util.SparseArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038
Sunny Goyal14168432019-10-24 15:59:49 -070039import com.android.launcher3.graphics.PlaceHolderIconDrawable;
Hyunyoung Song48cb7bc2018-09-25 17:03:34 -070040import com.android.launcher3.icons.BitmapInfo;
Tony Wickham9a8d11f2017-01-11 09:53:12 -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
Winsonc0880492015-08-21 11:16:27 -070051 // Since we don't need 256^2 values for combinations of both the brightness and saturation, we
52 // reduce the value space to a smaller value V, which reduces the number of cached
53 // ColorMatrixColorFilters that we need to keep to V^2
54 private static final int REDUCED_FILTER_VALUE_SPACE = 48;
Sunny Goyal95abbb32014-08-04 10:53:22 -070055
Winsonc0880492015-08-21 11:16:27 -070056 // A cache of ColorFilters for optimizing brightness and saturation animations
57 private static final SparseArray<ColorFilter> sCachedFilter = new SparseArray<>();
Sunny Goyal508da152014-08-14 10:53:27 -070058
Winsonc0880492015-08-21 11:16:27 -070059 // Temporary matrices used for calculation
60 private static final ColorMatrix sTempBrightnessMatrix = new ColorMatrix();
61 private static final ColorMatrix sTempFilterMatrix = new ColorMatrix();
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070062
Sunny Goyal55cb70b2016-11-12 09:58:29 -080063 protected final Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG);
Sunny Goyal61e08462018-03-02 17:25:59 -080064 protected Bitmap mBitmap;
Sunny Goyal179249d2017-12-19 16:49:24 -080065 protected final int mIconColor;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080066
67 private boolean mIsPressed;
Tony Wickham6b910a22016-11-08 10:40:34 -080068 private boolean mIsDisabled;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070069
Sunny Goyal726bee72018-03-05 12:54:24 -080070 // Animator and properties for the fast bitmap drawable's scale
71 private static final Property<FastBitmapDrawable, Float> SCALE
72 = new Property<FastBitmapDrawable, Float>(Float.TYPE, "scale") {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080073 @Override
74 public Float get(FastBitmapDrawable fastBitmapDrawable) {
Sunny Goyal726bee72018-03-05 12:54:24 -080075 return fastBitmapDrawable.mScale;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080076 }
77
78 @Override
79 public void set(FastBitmapDrawable fastBitmapDrawable, Float value) {
Sunny Goyal726bee72018-03-05 12:54:24 -080080 fastBitmapDrawable.mScale = value;
81 fastBitmapDrawable.invalidateSelf();
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080082 }
83 };
Sunny Goyal726bee72018-03-05 12:54:24 -080084 private ObjectAnimator mScaleAnimation;
85 private float mScale = 1;
86
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080087
Winsonc0880492015-08-21 11:16:27 -070088 // The saturation and brightness are values that are mapped to REDUCED_FILTER_VALUE_SPACE and
89 // as a result, can be used to compose the key for the cached ColorMatrixColorFilters
90 private int mDesaturation = 0;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070091 private int mBrightness = 0;
Winsonc0880492015-08-21 11:16:27 -070092 private int mAlpha = 255;
93 private int mPrevUpdateKey = Integer.MAX_VALUE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094
Hyunyoung Song3f471442015-04-08 19:01:34 -070095 public FastBitmapDrawable(Bitmap b) {
Sunny Goyal179249d2017-12-19 16:49:24 -080096 this(b, Color.TRANSPARENT);
97 }
98
99 public FastBitmapDrawable(BitmapInfo info) {
100 this(info.icon, info.color);
101 }
102
Sunny Goyal179249d2017-12-19 16:49:24 -0800103 protected FastBitmapDrawable(Bitmap b, int iconColor) {
Jon Mirandab6d686d2019-03-29 10:49:43 -0700104 this(b, iconColor, false);
105 }
106
107 protected FastBitmapDrawable(Bitmap b, int iconColor, boolean isDisabled) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108 mBitmap = b;
Sunny Goyal179249d2017-12-19 16:49:24 -0800109 mIconColor = iconColor;
Sunny Goyal96ac68a2017-02-02 16:37:21 -0800110 setFilterBitmap(true);
Jon Mirandab6d686d2019-03-29 10:49:43 -0700111 setIsDisabled(isDisabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112 }
113
114 @Override
Sunny Goyal726bee72018-03-05 12:54:24 -0800115 public final void draw(Canvas canvas) {
Matthew Ngeb9cc9d2018-06-25 15:32:24 -0700116 if (mScale != 1f) {
Sunny Goyal726bee72018-03-05 12:54:24 -0800117 int count = canvas.save();
118 Rect bounds = getBounds();
119 canvas.scale(mScale, mScale, bounds.exactCenterX(), bounds.exactCenterY());
120 drawInternal(canvas, bounds);
121 canvas.restoreToCount(count);
122 } else {
123 drawInternal(canvas, getBounds());
124 }
125 }
126
127 protected void drawInternal(Canvas canvas, Rect bounds) {
128 canvas.drawBitmap(mBitmap, null, bounds, mPaint);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129 }
130
131 @Override
Adam Cohenbadf71e2011-05-26 19:08:29 -0700132 public void setColorFilter(ColorFilter cf) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700133 // No op
Adam Cohenbadf71e2011-05-26 19:08:29 -0700134 }
135
136 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 public int getOpacity() {
138 return PixelFormat.TRANSLUCENT;
139 }
140
141 @Override
142 public void setAlpha(int alpha) {
Jon Mirandadff0de42019-08-30 18:42:01 -0700143 if (mAlpha != alpha) {
144 mAlpha = alpha;
145 mPaint.setAlpha(alpha);
146 invalidateSelf();
147 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148 }
149
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700150 @Override
Adam Cohen76fc0852011-06-17 13:26:23 -0700151 public void setFilterBitmap(boolean filterBitmap) {
152 mPaint.setFilterBitmap(filterBitmap);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700153 mPaint.setAntiAlias(filterBitmap);
Adam Cohen76fc0852011-06-17 13:26:23 -0700154 }
155
Winson Chung29d6fea2010-12-01 15:47:31 -0800156 public int getAlpha() {
157 return mAlpha;
158 }
159
Matthew Ngeb9cc9d2018-06-25 15:32:24 -0700160 public void setScale(float scale) {
161 if (mScaleAnimation != null) {
162 mScaleAnimation.cancel();
163 mScaleAnimation = null;
164 }
165 mScale = scale;
166 invalidateSelf();
167 }
168
Sunny Goyal726bee72018-03-05 12:54:24 -0800169 public float getAnimatedScale() {
170 return mScaleAnimation == null ? 1 : mScale;
171 }
172
Matthew Ngeb9cc9d2018-06-25 15:32:24 -0700173 public float getScale() {
174 return mScale;
175 }
176
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 public int getIntrinsicWidth() {
Sunny Goyalc424f222014-09-05 07:04:59 -0700179 return mBitmap.getWidth();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 }
181
182 @Override
183 public int getIntrinsicHeight() {
Sunny Goyalc424f222014-09-05 07:04:59 -0700184 return mBitmap.getHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 }
186
187 @Override
188 public int getMinimumWidth() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800189 return getBounds().width();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 }
191
192 @Override
193 public int getMinimumHeight() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800194 return getBounds().height();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800195 }
196
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800197 @Override
198 public boolean isStateful() {
199 return true;
Winsonc0880492015-08-21 11:16:27 -0700200 }
201
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800202 @Override
Sunny Goyal28141122017-06-21 17:28:23 -0700203 public ColorFilter getColorFilter() {
204 return mPaint.getColorFilter();
205 }
206
207 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800208 protected boolean onStateChange(int[] state) {
209 boolean isPressed = false;
210 for (int s : state) {
211 if (s == android.R.attr.state_pressed) {
212 isPressed = true;
213 break;
214 }
215 }
216 if (mIsPressed != isPressed) {
217 mIsPressed = isPressed;
Winsonc0880492015-08-21 11:16:27 -0700218
Sunny Goyal726bee72018-03-05 12:54:24 -0800219 if (mScaleAnimation != null) {
220 mScaleAnimation.cancel();
221 mScaleAnimation = null;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800222 }
Winsonc0880492015-08-21 11:16:27 -0700223
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800224 if (mIsPressed) {
225 // Animate when going to pressed state
Sunny Goyal726bee72018-03-05 12:54:24 -0800226 mScaleAnimation = ObjectAnimator.ofFloat(this, SCALE, PRESSED_SCALE);
227 mScaleAnimation.setDuration(CLICK_FEEDBACK_DURATION);
228 mScaleAnimation.setInterpolator(ACCEL);
229 mScaleAnimation.start();
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800230 } else {
Hyunyoung Songef468d82019-01-03 01:02:43 -0800231 if (isVisible()) {
232 mScaleAnimation = ObjectAnimator.ofFloat(this, SCALE, 1f);
233 mScaleAnimation.setDuration(CLICK_FEEDBACK_DURATION);
234 mScaleAnimation.setInterpolator(DEACCEL);
235 mScaleAnimation.start();
236 } else {
237 mScale = 1f;
238 invalidateSelf();
239 }
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800240 }
Winsonc0880492015-08-21 11:16:27 -0700241 return true;
242 }
243 return false;
244 }
245
Tony Wickham6b910a22016-11-08 10:40:34 -0800246 private void invalidateDesaturationAndBrightness() {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800247 setDesaturation(mIsDisabled ? DISABLED_DESATURATION : 0);
Sunny Goyal726bee72018-03-05 12:54:24 -0800248 setBrightness(mIsDisabled ? DISABLED_BRIGHTNESS : 0);
Winsonc0880492015-08-21 11:16:27 -0700249 }
250
Tony Wickham6b910a22016-11-08 10:40:34 -0800251 public void setIsDisabled(boolean isDisabled) {
252 if (mIsDisabled != isDisabled) {
253 mIsDisabled = isDisabled;
254 invalidateDesaturationAndBrightness();
255 }
256 }
257
Jon Mirandab6d686d2019-03-29 10:49:43 -0700258 protected boolean isDisabled() {
259 return mIsDisabled;
260 }
261
Winsonc0880492015-08-21 11:16:27 -0700262 /**
Winsonc0880492015-08-21 11:16:27 -0700263 * Sets the saturation of this icon, 0 [full color] -> 1 [desaturated]
264 */
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800265 private void setDesaturation(float desaturation) {
Winsonc0880492015-08-21 11:16:27 -0700266 int newDesaturation = (int) Math.floor(desaturation * REDUCED_FILTER_VALUE_SPACE);
267 if (mDesaturation != newDesaturation) {
268 mDesaturation = newDesaturation;
Sunny Goyal95abbb32014-08-04 10:53:22 -0700269 updateFilter();
270 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700271 }
272
Winsonc0880492015-08-21 11:16:27 -0700273 public float getDesaturation() {
274 return (float) mDesaturation / REDUCED_FILTER_VALUE_SPACE;
Sunny Goyal508da152014-08-14 10:53:27 -0700275 }
276
Winsonc0880492015-08-21 11:16:27 -0700277 /**
278 * Sets the brightness of this icon, 0 [no add. brightness] -> 1 [2bright2furious]
279 */
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800280 private void setBrightness(float brightness) {
Winsonc0880492015-08-21 11:16:27 -0700281 int newBrightness = (int) Math.floor(brightness * REDUCED_FILTER_VALUE_SPACE);
282 if (mBrightness != newBrightness) {
283 mBrightness = newBrightness;
Sunny Goyal95abbb32014-08-04 10:53:22 -0700284 updateFilter();
285 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700286 }
287
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800288 private float getBrightness() {
Winsonc0880492015-08-21 11:16:27 -0700289 return (float) mBrightness / REDUCED_FILTER_VALUE_SPACE;
290 }
291
292 /**
293 * Updates the paint to reflect the current brightness and saturation.
294 */
Sunny Goyal0ffab442018-06-07 17:31:48 -0700295 protected void updateFilter() {
Winsonc0880492015-08-21 11:16:27 -0700296 boolean usePorterDuffFilter = false;
297 int key = -1;
298 if (mDesaturation > 0) {
299 key = (mDesaturation << 16) | mBrightness;
300 } else if (mBrightness > 0) {
301 // Compose a key with a fully saturated icon if we are just animating brightness
302 key = (1 << 16) | mBrightness;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700303
Winsonc0880492015-08-21 11:16:27 -0700304 // We found that in L, ColorFilters cause drawing artifacts with shadows baked into
305 // icons, so just use a PorterDuff filter when we aren't animating saturation
306 usePorterDuffFilter = true;
307 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700308
Winsonc0880492015-08-21 11:16:27 -0700309 // Debounce multiple updates on the same frame
310 if (key == mPrevUpdateKey) {
311 return;
312 }
313 mPrevUpdateKey = key;
314
315 if (key != -1) {
316 ColorFilter filter = sCachedFilter.get(key);
Sunny Goyal508da152014-08-14 10:53:27 -0700317 if (filter == null) {
Winsonc0880492015-08-21 11:16:27 -0700318 float brightnessF = getBrightness();
319 int brightnessI = (int) (255 * brightnessF);
320 if (usePorterDuffFilter) {
321 filter = new PorterDuffColorFilter(Color.argb(brightnessI, 255, 255, 255),
322 PorterDuff.Mode.SRC_ATOP);
323 } else {
324 float saturationF = 1f - getDesaturation();
325 sTempFilterMatrix.setSaturation(saturationF);
326 if (mBrightness > 0) {
327 // Brightness: C-new = C-old*(1-amount) + amount
328 float scale = 1f - brightnessF;
329 float[] mat = sTempBrightnessMatrix.getArray();
330 mat[0] = scale;
331 mat[6] = scale;
332 mat[12] = scale;
333 mat[4] = brightnessI;
334 mat[9] = brightnessI;
335 mat[14] = brightnessI;
336 sTempFilterMatrix.preConcat(sTempBrightnessMatrix);
337 }
338 filter = new ColorMatrixColorFilter(sTempFilterMatrix);
339 }
340 sCachedFilter.append(key, filter);
Sunny Goyal508da152014-08-14 10:53:27 -0700341 }
342 mPaint.setColorFilter(filter);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700343 } else {
344 mPaint.setColorFilter(null);
345 }
Winsonc0880492015-08-21 11:16:27 -0700346 invalidateSelf();
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700347 }
Sunny Goyal338d15d2018-02-23 12:19:44 -0800348
349 @Override
350 public ConstantState getConstantState() {
Jon Mirandab6d686d2019-03-29 10:49:43 -0700351 return new MyConstantState(mBitmap, mIconColor, mIsDisabled);
Sunny Goyal338d15d2018-02-23 12:19:44 -0800352 }
353
Sunny Goyal61e08462018-03-02 17:25:59 -0800354 protected static class MyConstantState extends ConstantState {
355 protected final Bitmap mBitmap;
356 protected final int mIconColor;
Jon Mirandab6d686d2019-03-29 10:49:43 -0700357 protected final boolean mIsDisabled;
Sunny Goyal338d15d2018-02-23 12:19:44 -0800358
Jon Mirandab6d686d2019-03-29 10:49:43 -0700359 public MyConstantState(Bitmap bitmap, int color, boolean isDisabled) {
Sunny Goyal338d15d2018-02-23 12:19:44 -0800360 mBitmap = bitmap;
361 mIconColor = color;
Jon Mirandab6d686d2019-03-29 10:49:43 -0700362 mIsDisabled = isDisabled;
Sunny Goyal338d15d2018-02-23 12:19:44 -0800363 }
364
365 @Override
Sunny Goyal14168432019-10-24 15:59:49 -0700366 public FastBitmapDrawable newDrawable() {
Jon Mirandab6d686d2019-03-29 10:49:43 -0700367 return new FastBitmapDrawable(mBitmap, mIconColor, mIsDisabled);
Sunny Goyal338d15d2018-02-23 12:19:44 -0800368 }
369
370 @Override
371 public int getChangingConfigurations() {
372 return 0;
373 }
374 }
Sunny Goyal14168432019-10-24 15:59:49 -0700375
376 /**
377 * Interface to be implemented by custom {@link BitmapInfo} to handle drawable construction
378 */
379 public interface Factory {
380
381 /**
382 * Called to create a new drawable
383 */
384 FastBitmapDrawable newDrawable();
385 }
386
387 /**
388 * Returns a FastBitmapDrawable with the icon.
389 */
390 public static FastBitmapDrawable newIcon(Context context, ItemInfoWithIcon info) {
391 FastBitmapDrawable drawable = newIcon(context, info.bitmap);
392 drawable.setIsDisabled(info.isDisabled());
393 return drawable;
394 }
395
396 /**
397 * Creates a drawable for the provided BitmapInfo
398 */
399 public static FastBitmapDrawable newIcon(Context context, BitmapInfo info) {
400 if (info instanceof Factory) {
401 return ((Factory) info).newDrawable();
402 } else if (info.isLowRes()) {
403 return new PlaceHolderIconDrawable(info, context);
404 } else {
405 return new FastBitmapDrawable(info);
406 }
407 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408}