blob: 9217ca987650fe2ebe95c0996681095964992e08 [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;
20
Sunny Goyal508da152014-08-14 10:53:27 -070021import android.animation.ObjectAnimator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080022import android.graphics.Bitmap;
23import android.graphics.Canvas;
Sunny Goyal508da152014-08-14 10:53:27 -070024import android.graphics.Color;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.graphics.ColorFilter;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070026import android.graphics.ColorMatrix;
27import android.graphics.ColorMatrixColorFilter;
Winson Chung45e1d6e2010-11-09 17:19:49 -080028import android.graphics.Paint;
29import android.graphics.PixelFormat;
Sunny Goyal508da152014-08-14 10:53:27 -070030import android.graphics.PorterDuff;
31import android.graphics.PorterDuffColorFilter;
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;
Sunny Goyal508da152014-08-14 10:53:27 -070035import android.util.SparseArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036
Sunny Goyal179249d2017-12-19 16:49:24 -080037import com.android.launcher3.graphics.BitmapInfo;
Tony Wickham9a8d11f2017-01-11 09:53:12 -080038
Hyunyoung Song3f471442015-04-08 19:01:34 -070039public class FastBitmapDrawable extends Drawable {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080040
Sunny Goyal726bee72018-03-05 12:54:24 -080041 private static final float PRESSED_SCALE = 1.1f;
42
Tony Wickham6b910a22016-11-08 10:40:34 -080043 private static final float DISABLED_DESATURATION = 1f;
44 private static final float DISABLED_BRIGHTNESS = 0.5f;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070045
Sunny Goyal726bee72018-03-05 12:54:24 -080046 public static final int CLICK_FEEDBACK_DURATION = 200;
Sunny Goyal508da152014-08-14 10:53:27 -070047
Winsonc0880492015-08-21 11:16:27 -070048 // Since we don't need 256^2 values for combinations of both the brightness and saturation, we
49 // reduce the value space to a smaller value V, which reduces the number of cached
50 // ColorMatrixColorFilters that we need to keep to V^2
51 private static final int REDUCED_FILTER_VALUE_SPACE = 48;
Sunny Goyal95abbb32014-08-04 10:53:22 -070052
Winsonc0880492015-08-21 11:16:27 -070053 // A cache of ColorFilters for optimizing brightness and saturation animations
54 private static final SparseArray<ColorFilter> sCachedFilter = new SparseArray<>();
Sunny Goyal508da152014-08-14 10:53:27 -070055
Winsonc0880492015-08-21 11:16:27 -070056 // Temporary matrices used for calculation
57 private static final ColorMatrix sTempBrightnessMatrix = new ColorMatrix();
58 private static final ColorMatrix sTempFilterMatrix = new ColorMatrix();
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070059
Sunny Goyal55cb70b2016-11-12 09:58:29 -080060 protected final Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG);
Sunny Goyal61e08462018-03-02 17:25:59 -080061 protected Bitmap mBitmap;
Sunny Goyal179249d2017-12-19 16:49:24 -080062 protected final int mIconColor;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080063
64 private boolean mIsPressed;
Tony Wickham6b910a22016-11-08 10:40:34 -080065 private boolean mIsDisabled;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070066
Sunny Goyal726bee72018-03-05 12:54:24 -080067 // Animator and properties for the fast bitmap drawable's scale
68 private static final Property<FastBitmapDrawable, Float> SCALE
69 = new Property<FastBitmapDrawable, Float>(Float.TYPE, "scale") {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080070 @Override
71 public Float get(FastBitmapDrawable fastBitmapDrawable) {
Sunny Goyal726bee72018-03-05 12:54:24 -080072 return fastBitmapDrawable.mScale;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080073 }
74
75 @Override
76 public void set(FastBitmapDrawable fastBitmapDrawable, Float value) {
Sunny Goyal726bee72018-03-05 12:54:24 -080077 fastBitmapDrawable.mScale = value;
78 fastBitmapDrawable.invalidateSelf();
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080079 }
80 };
Sunny Goyal726bee72018-03-05 12:54:24 -080081 private ObjectAnimator mScaleAnimation;
82 private float mScale = 1;
83
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080084
Winsonc0880492015-08-21 11:16:27 -070085 // The saturation and brightness are values that are mapped to REDUCED_FILTER_VALUE_SPACE and
86 // as a result, can be used to compose the key for the cached ColorMatrixColorFilters
87 private int mDesaturation = 0;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070088 private int mBrightness = 0;
Winsonc0880492015-08-21 11:16:27 -070089 private int mAlpha = 255;
90 private int mPrevUpdateKey = Integer.MAX_VALUE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091
Hyunyoung Song3f471442015-04-08 19:01:34 -070092 public FastBitmapDrawable(Bitmap b) {
Sunny Goyal179249d2017-12-19 16:49:24 -080093 this(b, Color.TRANSPARENT);
94 }
95
96 public FastBitmapDrawable(BitmapInfo info) {
97 this(info.icon, info.color);
98 }
99
100 public FastBitmapDrawable(ItemInfoWithIcon info) {
101 this(info.iconBitmap, info.iconColor);
102 }
103
104 protected FastBitmapDrawable(Bitmap b, int iconColor) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800105 mBitmap = b;
Sunny Goyal179249d2017-12-19 16:49:24 -0800106 mIconColor = iconColor;
Sunny Goyal96ac68a2017-02-02 16:37:21 -0800107 setFilterBitmap(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108 }
109
110 @Override
Sunny Goyal726bee72018-03-05 12:54:24 -0800111 public final void draw(Canvas canvas) {
112 if (mScaleAnimation != null) {
113 int count = canvas.save();
114 Rect bounds = getBounds();
115 canvas.scale(mScale, mScale, bounds.exactCenterX(), bounds.exactCenterY());
116 drawInternal(canvas, bounds);
117 canvas.restoreToCount(count);
118 } else {
119 drawInternal(canvas, getBounds());
120 }
121 }
122
123 protected void drawInternal(Canvas canvas, Rect bounds) {
124 canvas.drawBitmap(mBitmap, null, bounds, mPaint);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125 }
126
127 @Override
Adam Cohenbadf71e2011-05-26 19:08:29 -0700128 public void setColorFilter(ColorFilter cf) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700129 // No op
Adam Cohenbadf71e2011-05-26 19:08:29 -0700130 }
131
132 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133 public int getOpacity() {
134 return PixelFormat.TRANSLUCENT;
135 }
136
137 @Override
138 public void setAlpha(int alpha) {
Winson Chung29d6fea2010-12-01 15:47:31 -0800139 mAlpha = alpha;
Winson Chungb3347bb2010-08-19 14:51:28 -0700140 mPaint.setAlpha(alpha);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141 }
142
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700143 @Override
Adam Cohen76fc0852011-06-17 13:26:23 -0700144 public void setFilterBitmap(boolean filterBitmap) {
145 mPaint.setFilterBitmap(filterBitmap);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700146 mPaint.setAntiAlias(filterBitmap);
Adam Cohen76fc0852011-06-17 13:26:23 -0700147 }
148
Winson Chung29d6fea2010-12-01 15:47:31 -0800149 public int getAlpha() {
150 return mAlpha;
151 }
152
Sunny Goyal726bee72018-03-05 12:54:24 -0800153 public float getAnimatedScale() {
154 return mScaleAnimation == null ? 1 : mScale;
155 }
156
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 public int getIntrinsicWidth() {
Sunny Goyalc424f222014-09-05 07:04:59 -0700159 return mBitmap.getWidth();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 }
161
162 @Override
163 public int getIntrinsicHeight() {
Sunny Goyalc424f222014-09-05 07:04:59 -0700164 return mBitmap.getHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 }
166
167 @Override
168 public int getMinimumWidth() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800169 return getBounds().width();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170 }
171
172 @Override
173 public int getMinimumHeight() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800174 return getBounds().height();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800175 }
176
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800177 @Override
178 public boolean isStateful() {
179 return true;
Winsonc0880492015-08-21 11:16:27 -0700180 }
181
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800182 @Override
Sunny Goyal28141122017-06-21 17:28:23 -0700183 public ColorFilter getColorFilter() {
184 return mPaint.getColorFilter();
185 }
186
187 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800188 protected boolean onStateChange(int[] state) {
189 boolean isPressed = false;
190 for (int s : state) {
191 if (s == android.R.attr.state_pressed) {
192 isPressed = true;
193 break;
194 }
195 }
196 if (mIsPressed != isPressed) {
197 mIsPressed = isPressed;
Winsonc0880492015-08-21 11:16:27 -0700198
Sunny Goyal726bee72018-03-05 12:54:24 -0800199 if (mScaleAnimation != null) {
200 mScaleAnimation.cancel();
201 mScaleAnimation = null;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800202 }
Winsonc0880492015-08-21 11:16:27 -0700203
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800204 if (mIsPressed) {
205 // Animate when going to pressed state
Sunny Goyal726bee72018-03-05 12:54:24 -0800206 mScaleAnimation = ObjectAnimator.ofFloat(this, SCALE, PRESSED_SCALE);
207 mScaleAnimation.setDuration(CLICK_FEEDBACK_DURATION);
208 mScaleAnimation.setInterpolator(ACCEL);
209 mScaleAnimation.start();
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800210 } else {
Sunny Goyal726bee72018-03-05 12:54:24 -0800211 mScale = 1f;
212 invalidateSelf();
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800213 }
Winsonc0880492015-08-21 11:16:27 -0700214 return true;
215 }
216 return false;
217 }
218
Tony Wickham6b910a22016-11-08 10:40:34 -0800219 private void invalidateDesaturationAndBrightness() {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800220 setDesaturation(mIsDisabled ? DISABLED_DESATURATION : 0);
Sunny Goyal726bee72018-03-05 12:54:24 -0800221 setBrightness(mIsDisabled ? DISABLED_BRIGHTNESS : 0);
Winsonc0880492015-08-21 11:16:27 -0700222 }
223
Tony Wickham6b910a22016-11-08 10:40:34 -0800224 public void setIsDisabled(boolean isDisabled) {
225 if (mIsDisabled != isDisabled) {
226 mIsDisabled = isDisabled;
227 invalidateDesaturationAndBrightness();
228 }
229 }
230
Winsonc0880492015-08-21 11:16:27 -0700231 /**
Winsonc0880492015-08-21 11:16:27 -0700232 * Sets the saturation of this icon, 0 [full color] -> 1 [desaturated]
233 */
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800234 private void setDesaturation(float desaturation) {
Winsonc0880492015-08-21 11:16:27 -0700235 int newDesaturation = (int) Math.floor(desaturation * REDUCED_FILTER_VALUE_SPACE);
236 if (mDesaturation != newDesaturation) {
237 mDesaturation = newDesaturation;
Sunny Goyal95abbb32014-08-04 10:53:22 -0700238 updateFilter();
239 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700240 }
241
Winsonc0880492015-08-21 11:16:27 -0700242 public float getDesaturation() {
243 return (float) mDesaturation / REDUCED_FILTER_VALUE_SPACE;
Sunny Goyal508da152014-08-14 10:53:27 -0700244 }
245
Winsonc0880492015-08-21 11:16:27 -0700246 /**
247 * Sets the brightness of this icon, 0 [no add. brightness] -> 1 [2bright2furious]
248 */
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800249 private void setBrightness(float brightness) {
Winsonc0880492015-08-21 11:16:27 -0700250 int newBrightness = (int) Math.floor(brightness * REDUCED_FILTER_VALUE_SPACE);
251 if (mBrightness != newBrightness) {
252 mBrightness = newBrightness;
Sunny Goyal95abbb32014-08-04 10:53:22 -0700253 updateFilter();
254 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700255 }
256
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800257 private float getBrightness() {
Winsonc0880492015-08-21 11:16:27 -0700258 return (float) mBrightness / REDUCED_FILTER_VALUE_SPACE;
259 }
260
261 /**
262 * Updates the paint to reflect the current brightness and saturation.
263 */
Sunny Goyal0ffab442018-06-07 17:31:48 -0700264 protected void updateFilter() {
Winsonc0880492015-08-21 11:16:27 -0700265 boolean usePorterDuffFilter = false;
266 int key = -1;
267 if (mDesaturation > 0) {
268 key = (mDesaturation << 16) | mBrightness;
269 } else if (mBrightness > 0) {
270 // Compose a key with a fully saturated icon if we are just animating brightness
271 key = (1 << 16) | mBrightness;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700272
Winsonc0880492015-08-21 11:16:27 -0700273 // We found that in L, ColorFilters cause drawing artifacts with shadows baked into
274 // icons, so just use a PorterDuff filter when we aren't animating saturation
275 usePorterDuffFilter = true;
276 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700277
Winsonc0880492015-08-21 11:16:27 -0700278 // Debounce multiple updates on the same frame
279 if (key == mPrevUpdateKey) {
280 return;
281 }
282 mPrevUpdateKey = key;
283
284 if (key != -1) {
285 ColorFilter filter = sCachedFilter.get(key);
Sunny Goyal508da152014-08-14 10:53:27 -0700286 if (filter == null) {
Winsonc0880492015-08-21 11:16:27 -0700287 float brightnessF = getBrightness();
288 int brightnessI = (int) (255 * brightnessF);
289 if (usePorterDuffFilter) {
290 filter = new PorterDuffColorFilter(Color.argb(brightnessI, 255, 255, 255),
291 PorterDuff.Mode.SRC_ATOP);
292 } else {
293 float saturationF = 1f - getDesaturation();
294 sTempFilterMatrix.setSaturation(saturationF);
295 if (mBrightness > 0) {
296 // Brightness: C-new = C-old*(1-amount) + amount
297 float scale = 1f - brightnessF;
298 float[] mat = sTempBrightnessMatrix.getArray();
299 mat[0] = scale;
300 mat[6] = scale;
301 mat[12] = scale;
302 mat[4] = brightnessI;
303 mat[9] = brightnessI;
304 mat[14] = brightnessI;
305 sTempFilterMatrix.preConcat(sTempBrightnessMatrix);
306 }
307 filter = new ColorMatrixColorFilter(sTempFilterMatrix);
308 }
309 sCachedFilter.append(key, filter);
Sunny Goyal508da152014-08-14 10:53:27 -0700310 }
311 mPaint.setColorFilter(filter);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700312 } else {
313 mPaint.setColorFilter(null);
314 }
Winsonc0880492015-08-21 11:16:27 -0700315 invalidateSelf();
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700316 }
Sunny Goyal338d15d2018-02-23 12:19:44 -0800317
318 @Override
319 public ConstantState getConstantState() {
320 return new MyConstantState(mBitmap, mIconColor);
321 }
322
Sunny Goyal61e08462018-03-02 17:25:59 -0800323 protected static class MyConstantState extends ConstantState {
324 protected final Bitmap mBitmap;
325 protected final int mIconColor;
Sunny Goyal338d15d2018-02-23 12:19:44 -0800326
327 public MyConstantState(Bitmap bitmap, int color) {
328 mBitmap = bitmap;
329 mIconColor = color;
330 }
331
332 @Override
333 public Drawable newDrawable() {
334 return new FastBitmapDrawable(mBitmap, mIconColor);
335 }
336
337 @Override
338 public int getChangingConfigurations() {
339 return 0;
340 }
341 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800342}