blob: c4ec8c9a779fc01fd782af060f1d293bf83f5614 [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 Goyal508da152014-08-14 10:53:27 -070019import android.animation.ObjectAnimator;
20import android.animation.TimeInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.graphics.Bitmap;
22import android.graphics.Canvas;
Sunny Goyal508da152014-08-14 10:53:27 -070023import android.graphics.Color;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.graphics.ColorFilter;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070025import android.graphics.ColorMatrix;
26import android.graphics.ColorMatrixColorFilter;
Winson Chung45e1d6e2010-11-09 17:19:49 -080027import android.graphics.Paint;
28import android.graphics.PixelFormat;
Sunny Goyal508da152014-08-14 10:53:27 -070029import android.graphics.PorterDuff;
30import android.graphics.PorterDuffColorFilter;
Winson Chung45e1d6e2010-11-09 17:19:49 -080031import android.graphics.drawable.Drawable;
Sunny Goyal338d15d2018-02-23 12:19:44 -080032import android.support.annotation.NonNull;
33import android.support.annotation.Nullable;
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 Goyal2a76e3f2017-02-16 13:33:15 -080041 private static final float PRESSED_BRIGHTNESS = 100f / 255f;
Tony Wickham6b910a22016-11-08 10:40:34 -080042 private static final float DISABLED_DESATURATION = 1f;
43 private static final float DISABLED_BRIGHTNESS = 0.5f;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070044
Sunny Goyal179249d2017-12-19 16:49:24 -080045 public static final TimeInterpolator CLICK_FEEDBACK_INTERPOLATOR = (input) ->
46 (input < 0.05f) ? (input / 0.05f) : ((input < 0.3f) ? 1 : (1 - input) / 0.7f);
Sunny Goyal508da152014-08-14 10:53:27 -070047
Winsonc0880492015-08-21 11:16:27 -070048 public static final int CLICK_FEEDBACK_DURATION = 2000;
Sunny Goyal508da152014-08-14 10:53:27 -070049
Winsonc0880492015-08-21 11:16:27 -070050 // Since we don't need 256^2 values for combinations of both the brightness and saturation, we
51 // reduce the value space to a smaller value V, which reduces the number of cached
52 // ColorMatrixColorFilters that we need to keep to V^2
53 private static final int REDUCED_FILTER_VALUE_SPACE = 48;
Sunny Goyal95abbb32014-08-04 10:53:22 -070054
Winsonc0880492015-08-21 11:16:27 -070055 // A cache of ColorFilters for optimizing brightness and saturation animations
56 private static final SparseArray<ColorFilter> sCachedFilter = new SparseArray<>();
Sunny Goyal508da152014-08-14 10:53:27 -070057
Winsonc0880492015-08-21 11:16:27 -070058 // Temporary matrices used for calculation
59 private static final ColorMatrix sTempBrightnessMatrix = new ColorMatrix();
60 private static final ColorMatrix sTempFilterMatrix = new ColorMatrix();
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070061
Sunny Goyal55cb70b2016-11-12 09:58:29 -080062 protected final Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG);
Sunny Goyal508da152014-08-14 10:53:27 -070063 private final Bitmap mBitmap;
Sunny Goyal179249d2017-12-19 16:49:24 -080064 protected final int mIconColor;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080065
66 private boolean mIsPressed;
Tony Wickham6b910a22016-11-08 10:40:34 -080067 private boolean mIsDisabled;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070068
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080069 private static final Property<FastBitmapDrawable, Float> BRIGHTNESS
70 = new Property<FastBitmapDrawable, Float>(Float.TYPE, "brightness") {
71 @Override
72 public Float get(FastBitmapDrawable fastBitmapDrawable) {
73 return fastBitmapDrawable.getBrightness();
74 }
75
76 @Override
77 public void set(FastBitmapDrawable fastBitmapDrawable, Float value) {
78 fastBitmapDrawable.setBrightness(value);
79 }
80 };
81
Winsonc0880492015-08-21 11:16:27 -070082 // The saturation and brightness are values that are mapped to REDUCED_FILTER_VALUE_SPACE and
83 // as a result, can be used to compose the key for the cached ColorMatrixColorFilters
84 private int mDesaturation = 0;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070085 private int mBrightness = 0;
Winsonc0880492015-08-21 11:16:27 -070086 private int mAlpha = 255;
87 private int mPrevUpdateKey = Integer.MAX_VALUE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080089 // Animators for the fast bitmap drawable's brightness
90 private ObjectAnimator mBrightnessAnimator;
Sunny Goyal508da152014-08-14 10:53:27 -070091
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
111 public void draw(Canvas canvas) {
Winsonc0880492015-08-21 11:16:27 -0700112 canvas.drawBitmap(mBitmap, null, getBounds(), mPaint);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113 }
114
115 @Override
Adam Cohenbadf71e2011-05-26 19:08:29 -0700116 public void setColorFilter(ColorFilter cf) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700117 // No op
Adam Cohenbadf71e2011-05-26 19:08:29 -0700118 }
119
120 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 public int getOpacity() {
122 return PixelFormat.TRANSLUCENT;
123 }
124
125 @Override
126 public void setAlpha(int alpha) {
Winson Chung29d6fea2010-12-01 15:47:31 -0800127 mAlpha = alpha;
Winson Chungb3347bb2010-08-19 14:51:28 -0700128 mPaint.setAlpha(alpha);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129 }
130
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700131 @Override
Adam Cohen76fc0852011-06-17 13:26:23 -0700132 public void setFilterBitmap(boolean filterBitmap) {
133 mPaint.setFilterBitmap(filterBitmap);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700134 mPaint.setAntiAlias(filterBitmap);
Adam Cohen76fc0852011-06-17 13:26:23 -0700135 }
136
Winson Chung29d6fea2010-12-01 15:47:31 -0800137 public int getAlpha() {
138 return mAlpha;
139 }
140
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142 public int getIntrinsicWidth() {
Sunny Goyalc424f222014-09-05 07:04:59 -0700143 return mBitmap.getWidth();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 }
145
146 @Override
147 public int getIntrinsicHeight() {
Sunny Goyalc424f222014-09-05 07:04:59 -0700148 return mBitmap.getHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149 }
150
151 @Override
152 public int getMinimumWidth() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800153 return getBounds().width();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 }
155
156 @Override
157 public int getMinimumHeight() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800158 return getBounds().height();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800159 }
160
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 public Bitmap getBitmap() {
162 return mBitmap;
163 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700164
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800165 @Override
166 public boolean isStateful() {
167 return true;
Winsonc0880492015-08-21 11:16:27 -0700168 }
169
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800170 @Override
Sunny Goyal28141122017-06-21 17:28:23 -0700171 public ColorFilter getColorFilter() {
172 return mPaint.getColorFilter();
173 }
174
175 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800176 protected boolean onStateChange(int[] state) {
177 boolean isPressed = false;
178 for (int s : state) {
179 if (s == android.R.attr.state_pressed) {
180 isPressed = true;
181 break;
182 }
183 }
184 if (mIsPressed != isPressed) {
185 mIsPressed = isPressed;
Winsonc0880492015-08-21 11:16:27 -0700186
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800187 if (mBrightnessAnimator != null) {
188 mBrightnessAnimator.cancel();
189 }
Winsonc0880492015-08-21 11:16:27 -0700190
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800191 if (mIsPressed) {
192 // Animate when going to pressed state
193 mBrightnessAnimator = ObjectAnimator.ofFloat(
194 this, BRIGHTNESS, getExpectedBrightness());
195 mBrightnessAnimator.setDuration(CLICK_FEEDBACK_DURATION);
196 mBrightnessAnimator.setInterpolator(CLICK_FEEDBACK_INTERPOLATOR);
197 mBrightnessAnimator.start();
198 } else {
199 setBrightness(getExpectedBrightness());
200 }
Winsonc0880492015-08-21 11:16:27 -0700201 return true;
202 }
203 return false;
204 }
205
Tony Wickham6b910a22016-11-08 10:40:34 -0800206 private void invalidateDesaturationAndBrightness() {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800207 setDesaturation(mIsDisabled ? DISABLED_DESATURATION : 0);
208 setBrightness(getExpectedBrightness());
Tony Wickham6b910a22016-11-08 10:40:34 -0800209 }
210
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800211 private float getExpectedBrightness() {
212 return mIsDisabled ? DISABLED_BRIGHTNESS :
213 (mIsPressed ? PRESSED_BRIGHTNESS : 0);
Winsonc0880492015-08-21 11:16:27 -0700214 }
215
Tony Wickham6b910a22016-11-08 10:40:34 -0800216 public void setIsDisabled(boolean isDisabled) {
217 if (mIsDisabled != isDisabled) {
218 mIsDisabled = isDisabled;
219 invalidateDesaturationAndBrightness();
220 }
221 }
222
Winsonc0880492015-08-21 11:16:27 -0700223 /**
Winsonc0880492015-08-21 11:16:27 -0700224 * Sets the saturation of this icon, 0 [full color] -> 1 [desaturated]
225 */
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800226 private void setDesaturation(float desaturation) {
Winsonc0880492015-08-21 11:16:27 -0700227 int newDesaturation = (int) Math.floor(desaturation * REDUCED_FILTER_VALUE_SPACE);
228 if (mDesaturation != newDesaturation) {
229 mDesaturation = newDesaturation;
Sunny Goyal95abbb32014-08-04 10:53:22 -0700230 updateFilter();
231 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700232 }
233
Winsonc0880492015-08-21 11:16:27 -0700234 public float getDesaturation() {
235 return (float) mDesaturation / REDUCED_FILTER_VALUE_SPACE;
Sunny Goyal508da152014-08-14 10:53:27 -0700236 }
237
Winsonc0880492015-08-21 11:16:27 -0700238 /**
239 * Sets the brightness of this icon, 0 [no add. brightness] -> 1 [2bright2furious]
240 */
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800241 private void setBrightness(float brightness) {
Winsonc0880492015-08-21 11:16:27 -0700242 int newBrightness = (int) Math.floor(brightness * REDUCED_FILTER_VALUE_SPACE);
243 if (mBrightness != newBrightness) {
244 mBrightness = newBrightness;
Sunny Goyal95abbb32014-08-04 10:53:22 -0700245 updateFilter();
246 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700247 }
248
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800249 private float getBrightness() {
Winsonc0880492015-08-21 11:16:27 -0700250 return (float) mBrightness / REDUCED_FILTER_VALUE_SPACE;
251 }
252
253 /**
254 * Updates the paint to reflect the current brightness and saturation.
255 */
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700256 private void updateFilter() {
Winsonc0880492015-08-21 11:16:27 -0700257 boolean usePorterDuffFilter = false;
258 int key = -1;
259 if (mDesaturation > 0) {
260 key = (mDesaturation << 16) | mBrightness;
261 } else if (mBrightness > 0) {
262 // Compose a key with a fully saturated icon if we are just animating brightness
263 key = (1 << 16) | mBrightness;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700264
Winsonc0880492015-08-21 11:16:27 -0700265 // We found that in L, ColorFilters cause drawing artifacts with shadows baked into
266 // icons, so just use a PorterDuff filter when we aren't animating saturation
267 usePorterDuffFilter = true;
268 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700269
Winsonc0880492015-08-21 11:16:27 -0700270 // Debounce multiple updates on the same frame
271 if (key == mPrevUpdateKey) {
272 return;
273 }
274 mPrevUpdateKey = key;
275
276 if (key != -1) {
277 ColorFilter filter = sCachedFilter.get(key);
Sunny Goyal508da152014-08-14 10:53:27 -0700278 if (filter == null) {
Winsonc0880492015-08-21 11:16:27 -0700279 float brightnessF = getBrightness();
280 int brightnessI = (int) (255 * brightnessF);
281 if (usePorterDuffFilter) {
282 filter = new PorterDuffColorFilter(Color.argb(brightnessI, 255, 255, 255),
283 PorterDuff.Mode.SRC_ATOP);
284 } else {
285 float saturationF = 1f - getDesaturation();
286 sTempFilterMatrix.setSaturation(saturationF);
287 if (mBrightness > 0) {
288 // Brightness: C-new = C-old*(1-amount) + amount
289 float scale = 1f - brightnessF;
290 float[] mat = sTempBrightnessMatrix.getArray();
291 mat[0] = scale;
292 mat[6] = scale;
293 mat[12] = scale;
294 mat[4] = brightnessI;
295 mat[9] = brightnessI;
296 mat[14] = brightnessI;
297 sTempFilterMatrix.preConcat(sTempBrightnessMatrix);
298 }
299 filter = new ColorMatrixColorFilter(sTempFilterMatrix);
300 }
301 sCachedFilter.append(key, filter);
Sunny Goyal508da152014-08-14 10:53:27 -0700302 }
303 mPaint.setColorFilter(filter);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700304 } else {
305 mPaint.setColorFilter(null);
306 }
Winsonc0880492015-08-21 11:16:27 -0700307 invalidateSelf();
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700308 }
Sunny Goyal338d15d2018-02-23 12:19:44 -0800309
310 @Override
311 public ConstantState getConstantState() {
312 return new MyConstantState(mBitmap, mIconColor);
313 }
314
315 private static class MyConstantState extends ConstantState {
316 private final Bitmap mBitmap;
317 private final int mIconColor;
318
319
320 public MyConstantState(Bitmap bitmap, int color) {
321 mBitmap = bitmap;
322 mIconColor = color;
323 }
324
325 @Override
326 public Drawable newDrawable() {
327 return new FastBitmapDrawable(mBitmap, mIconColor);
328 }
329
330 @Override
331 public int getChangingConfigurations() {
332 return 0;
333 }
334 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800335}