blob: 199baaf588525cfe9373fd544e73b5f9c03eb84c [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;
Tony Wickham1e618492017-02-02 12:57:18 -080032import android.util.Property;
Sunny Goyal508da152014-08-14 10:53:27 -070033import android.util.SparseArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034
Tony Wickham010d2552017-01-20 08:15:28 -080035import com.android.launcher3.graphics.IconPalette;
Tony Wickham9a8d11f2017-01-11 09:53:12 -080036
Hyunyoung Song3f471442015-04-08 19:01:34 -070037public class FastBitmapDrawable extends Drawable {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080038
39 private static final int[] STATE_PRESSED = new int[] {android.R.attr.state_pressed};
40
41 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
Winsonc0880492015-08-21 11:16:27 -070045 public static final TimeInterpolator CLICK_FEEDBACK_INTERPOLATOR = new TimeInterpolator() {
Sunny Goyal508da152014-08-14 10:53:27 -070046
47 @Override
48 public float getInterpolation(float input) {
49 if (input < 0.05f) {
50 return input / 0.05f;
51 } else if (input < 0.3f){
52 return 1;
53 } else {
54 return (1 - input) / 0.7f;
55 }
56 }
57 };
Winsonc0880492015-08-21 11:16:27 -070058 public static final int CLICK_FEEDBACK_DURATION = 2000;
Sunny Goyal508da152014-08-14 10:53:27 -070059
Winsonc0880492015-08-21 11:16:27 -070060 // Since we don't need 256^2 values for combinations of both the brightness and saturation, we
61 // reduce the value space to a smaller value V, which reduces the number of cached
62 // ColorMatrixColorFilters that we need to keep to V^2
63 private static final int REDUCED_FILTER_VALUE_SPACE = 48;
Sunny Goyal95abbb32014-08-04 10:53:22 -070064
Winsonc0880492015-08-21 11:16:27 -070065 // A cache of ColorFilters for optimizing brightness and saturation animations
66 private static final SparseArray<ColorFilter> sCachedFilter = new SparseArray<>();
Sunny Goyal508da152014-08-14 10:53:27 -070067
Winsonc0880492015-08-21 11:16:27 -070068 // Temporary matrices used for calculation
69 private static final ColorMatrix sTempBrightnessMatrix = new ColorMatrix();
70 private static final ColorMatrix sTempFilterMatrix = new ColorMatrix();
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070071
Sunny Goyal55cb70b2016-11-12 09:58:29 -080072 protected final Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG);
Sunny Goyal508da152014-08-14 10:53:27 -070073 private final Bitmap mBitmap;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080074
75 private boolean mIsPressed;
Tony Wickham6b910a22016-11-08 10:40:34 -080076 private boolean mIsDisabled;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070077
Tony Wickham9a8d11f2017-01-11 09:53:12 -080078 private IconPalette mIconPalette;
79
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080080 private static final Property<FastBitmapDrawable, Float> BRIGHTNESS
81 = new Property<FastBitmapDrawable, Float>(Float.TYPE, "brightness") {
82 @Override
83 public Float get(FastBitmapDrawable fastBitmapDrawable) {
84 return fastBitmapDrawable.getBrightness();
85 }
86
87 @Override
88 public void set(FastBitmapDrawable fastBitmapDrawable, Float value) {
89 fastBitmapDrawable.setBrightness(value);
90 }
91 };
92
Winsonc0880492015-08-21 11:16:27 -070093 // The saturation and brightness are values that are mapped to REDUCED_FILTER_VALUE_SPACE and
94 // as a result, can be used to compose the key for the cached ColorMatrixColorFilters
95 private int mDesaturation = 0;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070096 private int mBrightness = 0;
Winsonc0880492015-08-21 11:16:27 -070097 private int mAlpha = 255;
98 private int mPrevUpdateKey = Integer.MAX_VALUE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080099
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800100 // Animators for the fast bitmap drawable's brightness
101 private ObjectAnimator mBrightnessAnimator;
Sunny Goyal508da152014-08-14 10:53:27 -0700102
Hyunyoung Song3f471442015-04-08 19:01:34 -0700103 public FastBitmapDrawable(Bitmap b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800104 mBitmap = b;
Sunny Goyal96ac68a2017-02-02 16:37:21 -0800105 setFilterBitmap(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106 }
107
108 @Override
109 public void draw(Canvas canvas) {
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800110 drawInternal(canvas);
111 }
112
113 public void drawWithBrightness(Canvas canvas, float brightness) {
114 float oldBrightness = getBrightness();
115 setBrightness(brightness);
116 drawInternal(canvas);
117 setBrightness(oldBrightness);
118 }
119
120 protected void drawInternal(Canvas canvas) {
Winsonc0880492015-08-21 11:16:27 -0700121 canvas.drawBitmap(mBitmap, null, getBounds(), mPaint);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122 }
123
Tony Wickhamce445162017-04-10 14:05:34 -0700124 public IconPalette getIconPalette() {
Tony Wickham9438ed42017-01-20 09:38:25 -0800125 if (mIconPalette == null) {
126 mIconPalette = IconPalette.fromDominantColor(Utilities
Tony Wickham7092db02017-06-07 14:32:23 -0700127 .findDominantColorByHue(mBitmap, 20), true /* desaturateBackground */);
Tony Wickham9438ed42017-01-20 09:38:25 -0800128 }
129 return mIconPalette;
130 }
131
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 @Override
Adam Cohenbadf71e2011-05-26 19:08:29 -0700133 public void setColorFilter(ColorFilter cf) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700134 // No op
Adam Cohenbadf71e2011-05-26 19:08:29 -0700135 }
136
137 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138 public int getOpacity() {
139 return PixelFormat.TRANSLUCENT;
140 }
141
142 @Override
143 public void setAlpha(int alpha) {
Winson Chung29d6fea2010-12-01 15:47:31 -0800144 mAlpha = alpha;
Winson Chungb3347bb2010-08-19 14:51:28 -0700145 mPaint.setAlpha(alpha);
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
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 public int getIntrinsicWidth() {
Sunny Goyalc424f222014-09-05 07:04:59 -0700160 return mBitmap.getWidth();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 }
162
163 @Override
164 public int getIntrinsicHeight() {
Sunny Goyalc424f222014-09-05 07:04:59 -0700165 return mBitmap.getHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 }
167
168 @Override
169 public int getMinimumWidth() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800170 return getBounds().width();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 }
172
173 @Override
174 public int getMinimumHeight() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800175 return getBounds().height();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800176 }
177
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 public Bitmap getBitmap() {
179 return mBitmap;
180 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700181
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
188 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 Goyal2a76e3f2017-02-16 13:33:15 -0800199 if (mBrightnessAnimator != null) {
200 mBrightnessAnimator.cancel();
201 }
Winsonc0880492015-08-21 11:16:27 -0700202
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800203 if (mIsPressed) {
204 // Animate when going to pressed state
205 mBrightnessAnimator = ObjectAnimator.ofFloat(
206 this, BRIGHTNESS, getExpectedBrightness());
207 mBrightnessAnimator.setDuration(CLICK_FEEDBACK_DURATION);
208 mBrightnessAnimator.setInterpolator(CLICK_FEEDBACK_INTERPOLATOR);
209 mBrightnessAnimator.start();
210 } else {
211 setBrightness(getExpectedBrightness());
212 }
Winsonc0880492015-08-21 11:16:27 -0700213 return true;
214 }
215 return false;
216 }
217
Tony Wickham6b910a22016-11-08 10:40:34 -0800218 private void invalidateDesaturationAndBrightness() {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800219 setDesaturation(mIsDisabled ? DISABLED_DESATURATION : 0);
220 setBrightness(getExpectedBrightness());
Tony Wickham6b910a22016-11-08 10:40:34 -0800221 }
222
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800223 private float getExpectedBrightness() {
224 return mIsDisabled ? DISABLED_BRIGHTNESS :
225 (mIsPressed ? PRESSED_BRIGHTNESS : 0);
Winsonc0880492015-08-21 11:16:27 -0700226 }
227
Tony Wickham6b910a22016-11-08 10:40:34 -0800228 public void setIsDisabled(boolean isDisabled) {
229 if (mIsDisabled != isDisabled) {
230 mIsDisabled = isDisabled;
231 invalidateDesaturationAndBrightness();
232 }
233 }
234
Winsonc0880492015-08-21 11:16:27 -0700235 /**
Winsonc0880492015-08-21 11:16:27 -0700236 * Sets the saturation of this icon, 0 [full color] -> 1 [desaturated]
237 */
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800238 private void setDesaturation(float desaturation) {
Winsonc0880492015-08-21 11:16:27 -0700239 int newDesaturation = (int) Math.floor(desaturation * REDUCED_FILTER_VALUE_SPACE);
240 if (mDesaturation != newDesaturation) {
241 mDesaturation = newDesaturation;
Sunny Goyal95abbb32014-08-04 10:53:22 -0700242 updateFilter();
243 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700244 }
245
Winsonc0880492015-08-21 11:16:27 -0700246 public float getDesaturation() {
247 return (float) mDesaturation / REDUCED_FILTER_VALUE_SPACE;
Sunny Goyal508da152014-08-14 10:53:27 -0700248 }
249
Winsonc0880492015-08-21 11:16:27 -0700250 /**
251 * Sets the brightness of this icon, 0 [no add. brightness] -> 1 [2bright2furious]
252 */
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800253 private void setBrightness(float brightness) {
Winsonc0880492015-08-21 11:16:27 -0700254 int newBrightness = (int) Math.floor(brightness * REDUCED_FILTER_VALUE_SPACE);
255 if (mBrightness != newBrightness) {
256 mBrightness = newBrightness;
Sunny Goyal95abbb32014-08-04 10:53:22 -0700257 updateFilter();
258 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700259 }
260
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800261 private float getBrightness() {
Winsonc0880492015-08-21 11:16:27 -0700262 return (float) mBrightness / REDUCED_FILTER_VALUE_SPACE;
263 }
264
265 /**
266 * Updates the paint to reflect the current brightness and saturation.
267 */
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700268 private void updateFilter() {
Winsonc0880492015-08-21 11:16:27 -0700269 boolean usePorterDuffFilter = false;
270 int key = -1;
271 if (mDesaturation > 0) {
272 key = (mDesaturation << 16) | mBrightness;
273 } else if (mBrightness > 0) {
274 // Compose a key with a fully saturated icon if we are just animating brightness
275 key = (1 << 16) | mBrightness;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700276
Winsonc0880492015-08-21 11:16:27 -0700277 // We found that in L, ColorFilters cause drawing artifacts with shadows baked into
278 // icons, so just use a PorterDuff filter when we aren't animating saturation
279 usePorterDuffFilter = true;
280 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700281
Winsonc0880492015-08-21 11:16:27 -0700282 // Debounce multiple updates on the same frame
283 if (key == mPrevUpdateKey) {
284 return;
285 }
286 mPrevUpdateKey = key;
287
288 if (key != -1) {
289 ColorFilter filter = sCachedFilter.get(key);
Sunny Goyal508da152014-08-14 10:53:27 -0700290 if (filter == null) {
Winsonc0880492015-08-21 11:16:27 -0700291 float brightnessF = getBrightness();
292 int brightnessI = (int) (255 * brightnessF);
293 if (usePorterDuffFilter) {
294 filter = new PorterDuffColorFilter(Color.argb(brightnessI, 255, 255, 255),
295 PorterDuff.Mode.SRC_ATOP);
296 } else {
297 float saturationF = 1f - getDesaturation();
298 sTempFilterMatrix.setSaturation(saturationF);
299 if (mBrightness > 0) {
300 // Brightness: C-new = C-old*(1-amount) + amount
301 float scale = 1f - brightnessF;
302 float[] mat = sTempBrightnessMatrix.getArray();
303 mat[0] = scale;
304 mat[6] = scale;
305 mat[12] = scale;
306 mat[4] = brightnessI;
307 mat[9] = brightnessI;
308 mat[14] = brightnessI;
309 sTempFilterMatrix.preConcat(sTempBrightnessMatrix);
310 }
311 filter = new ColorMatrixColorFilter(sTempFilterMatrix);
312 }
313 sCachedFilter.append(key, filter);
Sunny Goyal508da152014-08-14 10:53:27 -0700314 }
315 mPaint.setColorFilter(filter);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700316 } else {
317 mPaint.setColorFilter(null);
318 }
Winsonc0880492015-08-21 11:16:27 -0700319 invalidateSelf();
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700320 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800321}