blob: 2ec6b4c811de15ea60b1038e93e3d0415cfe9301 [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
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080039 private static final float PRESSED_BRIGHTNESS = 100f / 255f;
Tony Wickham6b910a22016-11-08 10:40:34 -080040 private static final float DISABLED_DESATURATION = 1f;
41 private static final float DISABLED_BRIGHTNESS = 0.5f;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070042
Winsonc0880492015-08-21 11:16:27 -070043 public static final TimeInterpolator CLICK_FEEDBACK_INTERPOLATOR = new TimeInterpolator() {
Sunny Goyal508da152014-08-14 10:53:27 -070044
45 @Override
46 public float getInterpolation(float input) {
47 if (input < 0.05f) {
48 return input / 0.05f;
49 } else if (input < 0.3f){
50 return 1;
51 } else {
52 return (1 - input) / 0.7f;
53 }
54 }
55 };
Winsonc0880492015-08-21 11:16:27 -070056 public static final int CLICK_FEEDBACK_DURATION = 2000;
Sunny Goyal508da152014-08-14 10:53:27 -070057
Winsonc0880492015-08-21 11:16:27 -070058 // Since we don't need 256^2 values for combinations of both the brightness and saturation, we
59 // reduce the value space to a smaller value V, which reduces the number of cached
60 // ColorMatrixColorFilters that we need to keep to V^2
61 private static final int REDUCED_FILTER_VALUE_SPACE = 48;
Sunny Goyal95abbb32014-08-04 10:53:22 -070062
Winsonc0880492015-08-21 11:16:27 -070063 // A cache of ColorFilters for optimizing brightness and saturation animations
64 private static final SparseArray<ColorFilter> sCachedFilter = new SparseArray<>();
Sunny Goyal508da152014-08-14 10:53:27 -070065
Winsonc0880492015-08-21 11:16:27 -070066 // Temporary matrices used for calculation
67 private static final ColorMatrix sTempBrightnessMatrix = new ColorMatrix();
68 private static final ColorMatrix sTempFilterMatrix = new ColorMatrix();
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070069
Sunny Goyal55cb70b2016-11-12 09:58:29 -080070 protected final Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG);
Sunny Goyal508da152014-08-14 10:53:27 -070071 private final Bitmap mBitmap;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080072
73 private boolean mIsPressed;
Tony Wickham6b910a22016-11-08 10:40:34 -080074 private boolean mIsDisabled;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070075
Tony Wickham9a8d11f2017-01-11 09:53:12 -080076 private IconPalette mIconPalette;
77
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080078 private static final Property<FastBitmapDrawable, Float> BRIGHTNESS
79 = new Property<FastBitmapDrawable, Float>(Float.TYPE, "brightness") {
80 @Override
81 public Float get(FastBitmapDrawable fastBitmapDrawable) {
82 return fastBitmapDrawable.getBrightness();
83 }
84
85 @Override
86 public void set(FastBitmapDrawable fastBitmapDrawable, Float value) {
87 fastBitmapDrawable.setBrightness(value);
88 }
89 };
90
Winsonc0880492015-08-21 11:16:27 -070091 // The saturation and brightness are values that are mapped to REDUCED_FILTER_VALUE_SPACE and
92 // as a result, can be used to compose the key for the cached ColorMatrixColorFilters
93 private int mDesaturation = 0;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070094 private int mBrightness = 0;
Winsonc0880492015-08-21 11:16:27 -070095 private int mAlpha = 255;
96 private int mPrevUpdateKey = Integer.MAX_VALUE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080098 // Animators for the fast bitmap drawable's brightness
99 private ObjectAnimator mBrightnessAnimator;
Sunny Goyal508da152014-08-14 10:53:27 -0700100
Hyunyoung Song3f471442015-04-08 19:01:34 -0700101 public FastBitmapDrawable(Bitmap b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102 mBitmap = b;
Sunny Goyal96ac68a2017-02-02 16:37:21 -0800103 setFilterBitmap(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800104 }
105
106 @Override
107 public void draw(Canvas canvas) {
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800108 drawInternal(canvas);
109 }
110
111 public void drawWithBrightness(Canvas canvas, float brightness) {
112 float oldBrightness = getBrightness();
113 setBrightness(brightness);
114 drawInternal(canvas);
115 setBrightness(oldBrightness);
116 }
117
118 protected void drawInternal(Canvas canvas) {
Winsonc0880492015-08-21 11:16:27 -0700119 canvas.drawBitmap(mBitmap, null, getBounds(), mPaint);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120 }
121
Tony Wickhamce445162017-04-10 14:05:34 -0700122 public IconPalette getIconPalette() {
Tony Wickham9438ed42017-01-20 09:38:25 -0800123 if (mIconPalette == null) {
124 mIconPalette = IconPalette.fromDominantColor(Utilities
Tony Wickham7092db02017-06-07 14:32:23 -0700125 .findDominantColorByHue(mBitmap, 20), true /* desaturateBackground */);
Tony Wickham9438ed42017-01-20 09:38:25 -0800126 }
127 return mIconPalette;
128 }
129
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130 @Override
Adam Cohenbadf71e2011-05-26 19:08:29 -0700131 public void setColorFilter(ColorFilter cf) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700132 // No op
Adam Cohenbadf71e2011-05-26 19:08:29 -0700133 }
134
135 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136 public int getOpacity() {
137 return PixelFormat.TRANSLUCENT;
138 }
139
140 @Override
141 public void setAlpha(int alpha) {
Winson Chung29d6fea2010-12-01 15:47:31 -0800142 mAlpha = alpha;
Winson Chungb3347bb2010-08-19 14:51:28 -0700143 mPaint.setAlpha(alpha);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 }
145
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700146 @Override
Adam Cohen76fc0852011-06-17 13:26:23 -0700147 public void setFilterBitmap(boolean filterBitmap) {
148 mPaint.setFilterBitmap(filterBitmap);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700149 mPaint.setAntiAlias(filterBitmap);
Adam Cohen76fc0852011-06-17 13:26:23 -0700150 }
151
Winson Chung29d6fea2010-12-01 15:47:31 -0800152 public int getAlpha() {
153 return mAlpha;
154 }
155
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 public int getIntrinsicWidth() {
Sunny Goyalc424f222014-09-05 07:04:59 -0700158 return mBitmap.getWidth();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 }
160
161 @Override
162 public int getIntrinsicHeight() {
Sunny Goyalc424f222014-09-05 07:04:59 -0700163 return mBitmap.getHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 }
165
166 @Override
167 public int getMinimumWidth() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800168 return getBounds().width();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 }
170
171 @Override
172 public int getMinimumHeight() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800173 return getBounds().height();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800174 }
175
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 public Bitmap getBitmap() {
177 return mBitmap;
178 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700179
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800180 @Override
181 public boolean isStateful() {
182 return true;
Winsonc0880492015-08-21 11:16:27 -0700183 }
184
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800185 @Override
Sunny Goyal28141122017-06-21 17:28:23 -0700186 public ColorFilter getColorFilter() {
187 return mPaint.getColorFilter();
188 }
189
190 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800191 protected boolean onStateChange(int[] state) {
192 boolean isPressed = false;
193 for (int s : state) {
194 if (s == android.R.attr.state_pressed) {
195 isPressed = true;
196 break;
197 }
198 }
199 if (mIsPressed != isPressed) {
200 mIsPressed = isPressed;
Winsonc0880492015-08-21 11:16:27 -0700201
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800202 if (mBrightnessAnimator != null) {
203 mBrightnessAnimator.cancel();
204 }
Winsonc0880492015-08-21 11:16:27 -0700205
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800206 if (mIsPressed) {
207 // Animate when going to pressed state
208 mBrightnessAnimator = ObjectAnimator.ofFloat(
209 this, BRIGHTNESS, getExpectedBrightness());
210 mBrightnessAnimator.setDuration(CLICK_FEEDBACK_DURATION);
211 mBrightnessAnimator.setInterpolator(CLICK_FEEDBACK_INTERPOLATOR);
212 mBrightnessAnimator.start();
213 } else {
214 setBrightness(getExpectedBrightness());
215 }
Winsonc0880492015-08-21 11:16:27 -0700216 return true;
217 }
218 return false;
219 }
220
Tony Wickham6b910a22016-11-08 10:40:34 -0800221 private void invalidateDesaturationAndBrightness() {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800222 setDesaturation(mIsDisabled ? DISABLED_DESATURATION : 0);
223 setBrightness(getExpectedBrightness());
Tony Wickham6b910a22016-11-08 10:40:34 -0800224 }
225
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800226 private float getExpectedBrightness() {
227 return mIsDisabled ? DISABLED_BRIGHTNESS :
228 (mIsPressed ? PRESSED_BRIGHTNESS : 0);
Winsonc0880492015-08-21 11:16:27 -0700229 }
230
Tony Wickham6b910a22016-11-08 10:40:34 -0800231 public void setIsDisabled(boolean isDisabled) {
232 if (mIsDisabled != isDisabled) {
233 mIsDisabled = isDisabled;
234 invalidateDesaturationAndBrightness();
235 }
236 }
237
Winsonc0880492015-08-21 11:16:27 -0700238 /**
Winsonc0880492015-08-21 11:16:27 -0700239 * Sets the saturation of this icon, 0 [full color] -> 1 [desaturated]
240 */
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800241 private void setDesaturation(float desaturation) {
Winsonc0880492015-08-21 11:16:27 -0700242 int newDesaturation = (int) Math.floor(desaturation * REDUCED_FILTER_VALUE_SPACE);
243 if (mDesaturation != newDesaturation) {
244 mDesaturation = newDesaturation;
Sunny Goyal95abbb32014-08-04 10:53:22 -0700245 updateFilter();
246 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700247 }
248
Winsonc0880492015-08-21 11:16:27 -0700249 public float getDesaturation() {
250 return (float) mDesaturation / REDUCED_FILTER_VALUE_SPACE;
Sunny Goyal508da152014-08-14 10:53:27 -0700251 }
252
Winsonc0880492015-08-21 11:16:27 -0700253 /**
254 * Sets the brightness of this icon, 0 [no add. brightness] -> 1 [2bright2furious]
255 */
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800256 private void setBrightness(float brightness) {
Winsonc0880492015-08-21 11:16:27 -0700257 int newBrightness = (int) Math.floor(brightness * REDUCED_FILTER_VALUE_SPACE);
258 if (mBrightness != newBrightness) {
259 mBrightness = newBrightness;
Sunny Goyal95abbb32014-08-04 10:53:22 -0700260 updateFilter();
261 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700262 }
263
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800264 private float getBrightness() {
Winsonc0880492015-08-21 11:16:27 -0700265 return (float) mBrightness / REDUCED_FILTER_VALUE_SPACE;
266 }
267
268 /**
269 * Updates the paint to reflect the current brightness and saturation.
270 */
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700271 private void updateFilter() {
Winsonc0880492015-08-21 11:16:27 -0700272 boolean usePorterDuffFilter = false;
273 int key = -1;
274 if (mDesaturation > 0) {
275 key = (mDesaturation << 16) | mBrightness;
276 } else if (mBrightness > 0) {
277 // Compose a key with a fully saturated icon if we are just animating brightness
278 key = (1 << 16) | mBrightness;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700279
Winsonc0880492015-08-21 11:16:27 -0700280 // We found that in L, ColorFilters cause drawing artifacts with shadows baked into
281 // icons, so just use a PorterDuff filter when we aren't animating saturation
282 usePorterDuffFilter = true;
283 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700284
Winsonc0880492015-08-21 11:16:27 -0700285 // Debounce multiple updates on the same frame
286 if (key == mPrevUpdateKey) {
287 return;
288 }
289 mPrevUpdateKey = key;
290
291 if (key != -1) {
292 ColorFilter filter = sCachedFilter.get(key);
Sunny Goyal508da152014-08-14 10:53:27 -0700293 if (filter == null) {
Winsonc0880492015-08-21 11:16:27 -0700294 float brightnessF = getBrightness();
295 int brightnessI = (int) (255 * brightnessF);
296 if (usePorterDuffFilter) {
297 filter = new PorterDuffColorFilter(Color.argb(brightnessI, 255, 255, 255),
298 PorterDuff.Mode.SRC_ATOP);
299 } else {
300 float saturationF = 1f - getDesaturation();
301 sTempFilterMatrix.setSaturation(saturationF);
302 if (mBrightness > 0) {
303 // Brightness: C-new = C-old*(1-amount) + amount
304 float scale = 1f - brightnessF;
305 float[] mat = sTempBrightnessMatrix.getArray();
306 mat[0] = scale;
307 mat[6] = scale;
308 mat[12] = scale;
309 mat[4] = brightnessI;
310 mat[9] = brightnessI;
311 mat[14] = brightnessI;
312 sTempFilterMatrix.preConcat(sTempBrightnessMatrix);
313 }
314 filter = new ColorMatrixColorFilter(sTempFilterMatrix);
315 }
316 sCachedFilter.append(key, filter);
Sunny Goyal508da152014-08-14 10:53:27 -0700317 }
318 mPaint.setColorFilter(filter);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700319 } else {
320 mPaint.setColorFilter(null);
321 }
Winsonc0880492015-08-21 11:16:27 -0700322 invalidateSelf();
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700323 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800324}