blob: 2e57f71412f87bb2b11ed3a976a0511118d4600d [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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.graphics.Bitmap;
24import android.graphics.Canvas;
Sunny Goyal508da152014-08-14 10:53:27 -070025import android.graphics.Color;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.graphics.ColorFilter;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070027import android.graphics.ColorMatrix;
28import android.graphics.ColorMatrixColorFilter;
Winson Chung45e1d6e2010-11-09 17:19:49 -080029import android.graphics.Paint;
30import android.graphics.PixelFormat;
Sunny Goyal508da152014-08-14 10:53:27 -070031import android.graphics.PorterDuff;
32import android.graphics.PorterDuffColorFilter;
Sunny Goyal726bee72018-03-05 12:54:24 -080033import android.graphics.Rect;
Winson Chung45e1d6e2010-11-09 17:19:49 -080034import android.graphics.drawable.Drawable;
Tony Wickham1e618492017-02-02 12:57:18 -080035import android.util.Property;
Sunny Goyal508da152014-08-14 10:53:27 -070036import android.util.SparseArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037
Hyunyoung Song48cb7bc2018-09-25 17:03:34 -070038import com.android.launcher3.icons.BitmapInfo;
Tony Wickham9a8d11f2017-01-11 09:53:12 -080039
Hyunyoung Song3f471442015-04-08 19:01:34 -070040public class FastBitmapDrawable extends Drawable {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080041
Sunny Goyal726bee72018-03-05 12:54:24 -080042 private static final float PRESSED_SCALE = 1.1f;
43
Tony Wickham6b910a22016-11-08 10:40:34 -080044 private static final float DISABLED_DESATURATION = 1f;
45 private static final float DISABLED_BRIGHTNESS = 0.5f;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070046
Sunny Goyal726bee72018-03-05 12:54:24 -080047 public static final int CLICK_FEEDBACK_DURATION = 200;
Sunny Goyal508da152014-08-14 10:53:27 -070048
Winsonc0880492015-08-21 11:16:27 -070049 // Since we don't need 256^2 values for combinations of both the brightness and saturation, we
50 // reduce the value space to a smaller value V, which reduces the number of cached
51 // ColorMatrixColorFilters that we need to keep to V^2
52 private static final int REDUCED_FILTER_VALUE_SPACE = 48;
Sunny Goyal95abbb32014-08-04 10:53:22 -070053
Winsonc0880492015-08-21 11:16:27 -070054 // A cache of ColorFilters for optimizing brightness and saturation animations
55 private static final SparseArray<ColorFilter> sCachedFilter = new SparseArray<>();
Sunny Goyal508da152014-08-14 10:53:27 -070056
Winsonc0880492015-08-21 11:16:27 -070057 // Temporary matrices used for calculation
58 private static final ColorMatrix sTempBrightnessMatrix = new ColorMatrix();
59 private static final ColorMatrix sTempFilterMatrix = new ColorMatrix();
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070060
Sunny Goyal55cb70b2016-11-12 09:58:29 -080061 protected final Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG);
Sunny Goyal61e08462018-03-02 17:25:59 -080062 protected Bitmap mBitmap;
Sunny Goyal179249d2017-12-19 16:49:24 -080063 protected final int mIconColor;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080064
65 private boolean mIsPressed;
Tony Wickham6b910a22016-11-08 10:40:34 -080066 private boolean mIsDisabled;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070067
Sunny Goyal726bee72018-03-05 12:54:24 -080068 // Animator and properties for the fast bitmap drawable's scale
69 private static final Property<FastBitmapDrawable, Float> SCALE
70 = new Property<FastBitmapDrawable, Float>(Float.TYPE, "scale") {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080071 @Override
72 public Float get(FastBitmapDrawable fastBitmapDrawable) {
Sunny Goyal726bee72018-03-05 12:54:24 -080073 return fastBitmapDrawable.mScale;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080074 }
75
76 @Override
77 public void set(FastBitmapDrawable fastBitmapDrawable, Float value) {
Sunny Goyal726bee72018-03-05 12:54:24 -080078 fastBitmapDrawable.mScale = value;
79 fastBitmapDrawable.invalidateSelf();
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080080 }
81 };
Sunny Goyal726bee72018-03-05 12:54:24 -080082 private ObjectAnimator mScaleAnimation;
83 private float mScale = 1;
84
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080085
Winsonc0880492015-08-21 11:16:27 -070086 // The saturation and brightness are values that are mapped to REDUCED_FILTER_VALUE_SPACE and
87 // as a result, can be used to compose the key for the cached ColorMatrixColorFilters
88 private int mDesaturation = 0;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070089 private int mBrightness = 0;
Winsonc0880492015-08-21 11:16:27 -070090 private int mAlpha = 255;
91 private int mPrevUpdateKey = Integer.MAX_VALUE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092
Hyunyoung Song3f471442015-04-08 19:01:34 -070093 public FastBitmapDrawable(Bitmap b) {
Sunny Goyal179249d2017-12-19 16:49:24 -080094 this(b, Color.TRANSPARENT);
95 }
96
97 public FastBitmapDrawable(BitmapInfo info) {
98 this(info.icon, info.color);
99 }
100
Sunny Goyal179249d2017-12-19 16:49:24 -0800101 protected FastBitmapDrawable(Bitmap b, int iconColor) {
Jon Mirandab6d686d2019-03-29 10:49:43 -0700102 this(b, iconColor, false);
103 }
104
105 protected FastBitmapDrawable(Bitmap b, int iconColor, boolean isDisabled) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106 mBitmap = b;
Sunny Goyal179249d2017-12-19 16:49:24 -0800107 mIconColor = iconColor;
Sunny Goyal96ac68a2017-02-02 16:37:21 -0800108 setFilterBitmap(true);
Jon Mirandab6d686d2019-03-29 10:49:43 -0700109 setIsDisabled(isDisabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110 }
111
112 @Override
Sunny Goyal726bee72018-03-05 12:54:24 -0800113 public final void draw(Canvas canvas) {
Matthew Ngeb9cc9d2018-06-25 15:32:24 -0700114 if (mScale != 1f) {
Sunny Goyal726bee72018-03-05 12:54:24 -0800115 int count = canvas.save();
116 Rect bounds = getBounds();
117 canvas.scale(mScale, mScale, bounds.exactCenterX(), bounds.exactCenterY());
118 drawInternal(canvas, bounds);
119 canvas.restoreToCount(count);
120 } else {
121 drawInternal(canvas, getBounds());
122 }
123 }
124
125 protected void drawInternal(Canvas canvas, Rect bounds) {
126 canvas.drawBitmap(mBitmap, null, bounds, mPaint);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127 }
128
129 @Override
Adam Cohenbadf71e2011-05-26 19:08:29 -0700130 public void setColorFilter(ColorFilter cf) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700131 // No op
Adam Cohenbadf71e2011-05-26 19:08:29 -0700132 }
133
134 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 public int getOpacity() {
136 return PixelFormat.TRANSLUCENT;
137 }
138
139 @Override
140 public void setAlpha(int alpha) {
Jon Mirandadff0de42019-08-30 18:42:01 -0700141 if (mAlpha != alpha) {
142 mAlpha = alpha;
143 mPaint.setAlpha(alpha);
144 invalidateSelf();
145 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146 }
147
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700148 @Override
Adam Cohen76fc0852011-06-17 13:26:23 -0700149 public void setFilterBitmap(boolean filterBitmap) {
150 mPaint.setFilterBitmap(filterBitmap);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700151 mPaint.setAntiAlias(filterBitmap);
Adam Cohen76fc0852011-06-17 13:26:23 -0700152 }
153
Winson Chung29d6fea2010-12-01 15:47:31 -0800154 public int getAlpha() {
155 return mAlpha;
156 }
157
Matthew Ngeb9cc9d2018-06-25 15:32:24 -0700158 public void setScale(float scale) {
159 if (mScaleAnimation != null) {
160 mScaleAnimation.cancel();
161 mScaleAnimation = null;
162 }
163 mScale = scale;
164 invalidateSelf();
165 }
166
Sunny Goyal726bee72018-03-05 12:54:24 -0800167 public float getAnimatedScale() {
168 return mScaleAnimation == null ? 1 : mScale;
169 }
170
Matthew Ngeb9cc9d2018-06-25 15:32:24 -0700171 public float getScale() {
172 return mScale;
173 }
174
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 public int getIntrinsicWidth() {
Sunny Goyalc424f222014-09-05 07:04:59 -0700177 return mBitmap.getWidth();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 }
179
180 @Override
181 public int getIntrinsicHeight() {
Sunny Goyalc424f222014-09-05 07:04:59 -0700182 return mBitmap.getHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 }
184
185 @Override
186 public int getMinimumWidth() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800187 return getBounds().width();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 }
189
190 @Override
191 public int getMinimumHeight() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800192 return getBounds().height();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800193 }
194
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800195 @Override
196 public boolean isStateful() {
197 return true;
Winsonc0880492015-08-21 11:16:27 -0700198 }
199
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800200 @Override
Sunny Goyal28141122017-06-21 17:28:23 -0700201 public ColorFilter getColorFilter() {
202 return mPaint.getColorFilter();
203 }
204
205 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800206 protected boolean onStateChange(int[] state) {
207 boolean isPressed = false;
208 for (int s : state) {
209 if (s == android.R.attr.state_pressed) {
210 isPressed = true;
211 break;
212 }
213 }
214 if (mIsPressed != isPressed) {
215 mIsPressed = isPressed;
Winsonc0880492015-08-21 11:16:27 -0700216
Sunny Goyal726bee72018-03-05 12:54:24 -0800217 if (mScaleAnimation != null) {
218 mScaleAnimation.cancel();
219 mScaleAnimation = null;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800220 }
Winsonc0880492015-08-21 11:16:27 -0700221
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800222 if (mIsPressed) {
223 // Animate when going to pressed state
Sunny Goyal726bee72018-03-05 12:54:24 -0800224 mScaleAnimation = ObjectAnimator.ofFloat(this, SCALE, PRESSED_SCALE);
225 mScaleAnimation.setDuration(CLICK_FEEDBACK_DURATION);
226 mScaleAnimation.setInterpolator(ACCEL);
227 mScaleAnimation.start();
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800228 } else {
Hyunyoung Songef468d82019-01-03 01:02:43 -0800229 if (isVisible()) {
230 mScaleAnimation = ObjectAnimator.ofFloat(this, SCALE, 1f);
231 mScaleAnimation.setDuration(CLICK_FEEDBACK_DURATION);
232 mScaleAnimation.setInterpolator(DEACCEL);
233 mScaleAnimation.start();
234 } else {
235 mScale = 1f;
236 invalidateSelf();
237 }
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800238 }
Winsonc0880492015-08-21 11:16:27 -0700239 return true;
240 }
241 return false;
242 }
243
Tony Wickham6b910a22016-11-08 10:40:34 -0800244 private void invalidateDesaturationAndBrightness() {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800245 setDesaturation(mIsDisabled ? DISABLED_DESATURATION : 0);
Sunny Goyal726bee72018-03-05 12:54:24 -0800246 setBrightness(mIsDisabled ? DISABLED_BRIGHTNESS : 0);
Winsonc0880492015-08-21 11:16:27 -0700247 }
248
Tony Wickham6b910a22016-11-08 10:40:34 -0800249 public void setIsDisabled(boolean isDisabled) {
250 if (mIsDisabled != isDisabled) {
251 mIsDisabled = isDisabled;
252 invalidateDesaturationAndBrightness();
253 }
254 }
255
Jon Mirandab6d686d2019-03-29 10:49:43 -0700256 protected boolean isDisabled() {
257 return mIsDisabled;
258 }
259
Winsonc0880492015-08-21 11:16:27 -0700260 /**
Winsonc0880492015-08-21 11:16:27 -0700261 * Sets the saturation of this icon, 0 [full color] -> 1 [desaturated]
262 */
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800263 private void setDesaturation(float desaturation) {
Winsonc0880492015-08-21 11:16:27 -0700264 int newDesaturation = (int) Math.floor(desaturation * REDUCED_FILTER_VALUE_SPACE);
265 if (mDesaturation != newDesaturation) {
266 mDesaturation = newDesaturation;
Sunny Goyal95abbb32014-08-04 10:53:22 -0700267 updateFilter();
268 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700269 }
270
Winsonc0880492015-08-21 11:16:27 -0700271 public float getDesaturation() {
272 return (float) mDesaturation / REDUCED_FILTER_VALUE_SPACE;
Sunny Goyal508da152014-08-14 10:53:27 -0700273 }
274
Winsonc0880492015-08-21 11:16:27 -0700275 /**
276 * Sets the brightness of this icon, 0 [no add. brightness] -> 1 [2bright2furious]
277 */
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800278 private void setBrightness(float brightness) {
Winsonc0880492015-08-21 11:16:27 -0700279 int newBrightness = (int) Math.floor(brightness * REDUCED_FILTER_VALUE_SPACE);
280 if (mBrightness != newBrightness) {
281 mBrightness = newBrightness;
Sunny Goyal95abbb32014-08-04 10:53:22 -0700282 updateFilter();
283 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700284 }
285
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800286 private float getBrightness() {
Winsonc0880492015-08-21 11:16:27 -0700287 return (float) mBrightness / REDUCED_FILTER_VALUE_SPACE;
288 }
289
290 /**
291 * Updates the paint to reflect the current brightness and saturation.
292 */
Sunny Goyal0ffab442018-06-07 17:31:48 -0700293 protected void updateFilter() {
Winsonc0880492015-08-21 11:16:27 -0700294 boolean usePorterDuffFilter = false;
295 int key = -1;
296 if (mDesaturation > 0) {
297 key = (mDesaturation << 16) | mBrightness;
298 } else if (mBrightness > 0) {
299 // Compose a key with a fully saturated icon if we are just animating brightness
300 key = (1 << 16) | mBrightness;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700301
Winsonc0880492015-08-21 11:16:27 -0700302 // We found that in L, ColorFilters cause drawing artifacts with shadows baked into
303 // icons, so just use a PorterDuff filter when we aren't animating saturation
304 usePorterDuffFilter = true;
305 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700306
Winsonc0880492015-08-21 11:16:27 -0700307 // Debounce multiple updates on the same frame
308 if (key == mPrevUpdateKey) {
309 return;
310 }
311 mPrevUpdateKey = key;
312
313 if (key != -1) {
314 ColorFilter filter = sCachedFilter.get(key);
Sunny Goyal508da152014-08-14 10:53:27 -0700315 if (filter == null) {
Winsonc0880492015-08-21 11:16:27 -0700316 float brightnessF = getBrightness();
317 int brightnessI = (int) (255 * brightnessF);
318 if (usePorterDuffFilter) {
319 filter = new PorterDuffColorFilter(Color.argb(brightnessI, 255, 255, 255),
320 PorterDuff.Mode.SRC_ATOP);
321 } else {
322 float saturationF = 1f - getDesaturation();
323 sTempFilterMatrix.setSaturation(saturationF);
324 if (mBrightness > 0) {
325 // Brightness: C-new = C-old*(1-amount) + amount
326 float scale = 1f - brightnessF;
327 float[] mat = sTempBrightnessMatrix.getArray();
328 mat[0] = scale;
329 mat[6] = scale;
330 mat[12] = scale;
331 mat[4] = brightnessI;
332 mat[9] = brightnessI;
333 mat[14] = brightnessI;
334 sTempFilterMatrix.preConcat(sTempBrightnessMatrix);
335 }
336 filter = new ColorMatrixColorFilter(sTempFilterMatrix);
337 }
338 sCachedFilter.append(key, filter);
Sunny Goyal508da152014-08-14 10:53:27 -0700339 }
340 mPaint.setColorFilter(filter);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700341 } else {
342 mPaint.setColorFilter(null);
343 }
Winsonc0880492015-08-21 11:16:27 -0700344 invalidateSelf();
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700345 }
Sunny Goyal338d15d2018-02-23 12:19:44 -0800346
347 @Override
348 public ConstantState getConstantState() {
Jon Mirandab6d686d2019-03-29 10:49:43 -0700349 return new MyConstantState(mBitmap, mIconColor, mIsDisabled);
Sunny Goyal338d15d2018-02-23 12:19:44 -0800350 }
351
Sunny Goyal61e08462018-03-02 17:25:59 -0800352 protected static class MyConstantState extends ConstantState {
353 protected final Bitmap mBitmap;
354 protected final int mIconColor;
Jon Mirandab6d686d2019-03-29 10:49:43 -0700355 protected final boolean mIsDisabled;
Sunny Goyal338d15d2018-02-23 12:19:44 -0800356
Jon Mirandab6d686d2019-03-29 10:49:43 -0700357 public MyConstantState(Bitmap bitmap, int color, boolean isDisabled) {
Sunny Goyal338d15d2018-02-23 12:19:44 -0800358 mBitmap = bitmap;
359 mIconColor = color;
Jon Mirandab6d686d2019-03-29 10:49:43 -0700360 mIsDisabled = isDisabled;
Sunny Goyal338d15d2018-02-23 12:19:44 -0800361 }
362
363 @Override
364 public Drawable newDrawable() {
Jon Mirandab6d686d2019-03-29 10:49:43 -0700365 return new FastBitmapDrawable(mBitmap, mIconColor, mIsDisabled);
Sunny Goyal338d15d2018-02-23 12:19:44 -0800366 }
367
368 @Override
369 public int getChangingConfigurations() {
370 return 0;
371 }
372 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800373}