blob: bd19dfa3ca28c7404cee4761c70d7e3763297a11 [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
Sunny Goyal179249d2017-12-19 16:49:24 -080035import com.android.launcher3.graphics.BitmapInfo;
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
Sunny Goyal179249d2017-12-19 16:49:24 -080043 public static final TimeInterpolator CLICK_FEEDBACK_INTERPOLATOR = (input) ->
44 (input < 0.05f) ? (input / 0.05f) : ((input < 0.3f) ? 1 : (1 - input) / 0.7f);
Sunny Goyal508da152014-08-14 10:53:27 -070045
Winsonc0880492015-08-21 11:16:27 -070046 public static final int CLICK_FEEDBACK_DURATION = 2000;
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 Goyal508da152014-08-14 10:53:27 -070061 private final 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 Goyal2a76e3f2017-02-16 13:33:15 -080067 private static final Property<FastBitmapDrawable, Float> BRIGHTNESS
68 = new Property<FastBitmapDrawable, Float>(Float.TYPE, "brightness") {
69 @Override
70 public Float get(FastBitmapDrawable fastBitmapDrawable) {
71 return fastBitmapDrawable.getBrightness();
72 }
73
74 @Override
75 public void set(FastBitmapDrawable fastBitmapDrawable, Float value) {
76 fastBitmapDrawable.setBrightness(value);
77 }
78 };
79
Winsonc0880492015-08-21 11:16:27 -070080 // The saturation and brightness are values that are mapped to REDUCED_FILTER_VALUE_SPACE and
81 // as a result, can be used to compose the key for the cached ColorMatrixColorFilters
82 private int mDesaturation = 0;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070083 private int mBrightness = 0;
Winsonc0880492015-08-21 11:16:27 -070084 private int mAlpha = 255;
85 private int mPrevUpdateKey = Integer.MAX_VALUE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080087 // Animators for the fast bitmap drawable's brightness
88 private ObjectAnimator mBrightnessAnimator;
Sunny Goyal508da152014-08-14 10:53:27 -070089
Hyunyoung Song3f471442015-04-08 19:01:34 -070090 public FastBitmapDrawable(Bitmap b) {
Sunny Goyal179249d2017-12-19 16:49:24 -080091 this(b, Color.TRANSPARENT);
92 }
93
94 public FastBitmapDrawable(BitmapInfo info) {
95 this(info.icon, info.color);
96 }
97
98 public FastBitmapDrawable(ItemInfoWithIcon info) {
99 this(info.iconBitmap, info.iconColor);
100 }
101
102 protected FastBitmapDrawable(Bitmap b, int iconColor) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800103 mBitmap = b;
Sunny Goyal179249d2017-12-19 16:49:24 -0800104 mIconColor = iconColor;
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) {
Winsonc0880492015-08-21 11:16:27 -0700110 canvas.drawBitmap(mBitmap, null, getBounds(), mPaint);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111 }
112
113 @Override
Adam Cohenbadf71e2011-05-26 19:08:29 -0700114 public void setColorFilter(ColorFilter cf) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700115 // No op
Adam Cohenbadf71e2011-05-26 19:08:29 -0700116 }
117
118 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119 public int getOpacity() {
120 return PixelFormat.TRANSLUCENT;
121 }
122
123 @Override
124 public void setAlpha(int alpha) {
Winson Chung29d6fea2010-12-01 15:47:31 -0800125 mAlpha = alpha;
Winson Chungb3347bb2010-08-19 14:51:28 -0700126 mPaint.setAlpha(alpha);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127 }
128
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700129 @Override
Adam Cohen76fc0852011-06-17 13:26:23 -0700130 public void setFilterBitmap(boolean filterBitmap) {
131 mPaint.setFilterBitmap(filterBitmap);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700132 mPaint.setAntiAlias(filterBitmap);
Adam Cohen76fc0852011-06-17 13:26:23 -0700133 }
134
Winson Chung29d6fea2010-12-01 15:47:31 -0800135 public int getAlpha() {
136 return mAlpha;
137 }
138
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140 public int getIntrinsicWidth() {
Sunny Goyalc424f222014-09-05 07:04:59 -0700141 return mBitmap.getWidth();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142 }
143
144 @Override
145 public int getIntrinsicHeight() {
Sunny Goyalc424f222014-09-05 07:04:59 -0700146 return mBitmap.getHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 }
148
149 @Override
150 public int getMinimumWidth() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800151 return getBounds().width();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152 }
153
154 @Override
155 public int getMinimumHeight() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800156 return getBounds().height();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800157 }
158
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 public Bitmap getBitmap() {
160 return mBitmap;
161 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700162
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800163 @Override
164 public boolean isStateful() {
165 return true;
Winsonc0880492015-08-21 11:16:27 -0700166 }
167
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800168 @Override
Sunny Goyal28141122017-06-21 17:28:23 -0700169 public ColorFilter getColorFilter() {
170 return mPaint.getColorFilter();
171 }
172
173 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800174 protected boolean onStateChange(int[] state) {
175 boolean isPressed = false;
176 for (int s : state) {
177 if (s == android.R.attr.state_pressed) {
178 isPressed = true;
179 break;
180 }
181 }
182 if (mIsPressed != isPressed) {
183 mIsPressed = isPressed;
Winsonc0880492015-08-21 11:16:27 -0700184
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800185 if (mBrightnessAnimator != null) {
186 mBrightnessAnimator.cancel();
187 }
Winsonc0880492015-08-21 11:16:27 -0700188
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800189 if (mIsPressed) {
190 // Animate when going to pressed state
191 mBrightnessAnimator = ObjectAnimator.ofFloat(
192 this, BRIGHTNESS, getExpectedBrightness());
193 mBrightnessAnimator.setDuration(CLICK_FEEDBACK_DURATION);
194 mBrightnessAnimator.setInterpolator(CLICK_FEEDBACK_INTERPOLATOR);
195 mBrightnessAnimator.start();
196 } else {
197 setBrightness(getExpectedBrightness());
198 }
Winsonc0880492015-08-21 11:16:27 -0700199 return true;
200 }
201 return false;
202 }
203
Tony Wickham6b910a22016-11-08 10:40:34 -0800204 private void invalidateDesaturationAndBrightness() {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800205 setDesaturation(mIsDisabled ? DISABLED_DESATURATION : 0);
206 setBrightness(getExpectedBrightness());
Tony Wickham6b910a22016-11-08 10:40:34 -0800207 }
208
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800209 private float getExpectedBrightness() {
210 return mIsDisabled ? DISABLED_BRIGHTNESS :
211 (mIsPressed ? PRESSED_BRIGHTNESS : 0);
Winsonc0880492015-08-21 11:16:27 -0700212 }
213
Tony Wickham6b910a22016-11-08 10:40:34 -0800214 public void setIsDisabled(boolean isDisabled) {
215 if (mIsDisabled != isDisabled) {
216 mIsDisabled = isDisabled;
217 invalidateDesaturationAndBrightness();
218 }
219 }
220
Winsonc0880492015-08-21 11:16:27 -0700221 /**
Winsonc0880492015-08-21 11:16:27 -0700222 * Sets the saturation of this icon, 0 [full color] -> 1 [desaturated]
223 */
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800224 private void setDesaturation(float desaturation) {
Winsonc0880492015-08-21 11:16:27 -0700225 int newDesaturation = (int) Math.floor(desaturation * REDUCED_FILTER_VALUE_SPACE);
226 if (mDesaturation != newDesaturation) {
227 mDesaturation = newDesaturation;
Sunny Goyal95abbb32014-08-04 10:53:22 -0700228 updateFilter();
229 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700230 }
231
Winsonc0880492015-08-21 11:16:27 -0700232 public float getDesaturation() {
233 return (float) mDesaturation / REDUCED_FILTER_VALUE_SPACE;
Sunny Goyal508da152014-08-14 10:53:27 -0700234 }
235
Winsonc0880492015-08-21 11:16:27 -0700236 /**
237 * Sets the brightness of this icon, 0 [no add. brightness] -> 1 [2bright2furious]
238 */
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800239 private void setBrightness(float brightness) {
Winsonc0880492015-08-21 11:16:27 -0700240 int newBrightness = (int) Math.floor(brightness * REDUCED_FILTER_VALUE_SPACE);
241 if (mBrightness != newBrightness) {
242 mBrightness = newBrightness;
Sunny Goyal95abbb32014-08-04 10:53:22 -0700243 updateFilter();
244 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700245 }
246
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800247 private float getBrightness() {
Winsonc0880492015-08-21 11:16:27 -0700248 return (float) mBrightness / REDUCED_FILTER_VALUE_SPACE;
249 }
250
251 /**
252 * Updates the paint to reflect the current brightness and saturation.
253 */
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700254 private void updateFilter() {
Winsonc0880492015-08-21 11:16:27 -0700255 boolean usePorterDuffFilter = false;
256 int key = -1;
257 if (mDesaturation > 0) {
258 key = (mDesaturation << 16) | mBrightness;
259 } else if (mBrightness > 0) {
260 // Compose a key with a fully saturated icon if we are just animating brightness
261 key = (1 << 16) | mBrightness;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700262
Winsonc0880492015-08-21 11:16:27 -0700263 // We found that in L, ColorFilters cause drawing artifacts with shadows baked into
264 // icons, so just use a PorterDuff filter when we aren't animating saturation
265 usePorterDuffFilter = true;
266 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700267
Winsonc0880492015-08-21 11:16:27 -0700268 // Debounce multiple updates on the same frame
269 if (key == mPrevUpdateKey) {
270 return;
271 }
272 mPrevUpdateKey = key;
273
274 if (key != -1) {
275 ColorFilter filter = sCachedFilter.get(key);
Sunny Goyal508da152014-08-14 10:53:27 -0700276 if (filter == null) {
Winsonc0880492015-08-21 11:16:27 -0700277 float brightnessF = getBrightness();
278 int brightnessI = (int) (255 * brightnessF);
279 if (usePorterDuffFilter) {
280 filter = new PorterDuffColorFilter(Color.argb(brightnessI, 255, 255, 255),
281 PorterDuff.Mode.SRC_ATOP);
282 } else {
283 float saturationF = 1f - getDesaturation();
284 sTempFilterMatrix.setSaturation(saturationF);
285 if (mBrightness > 0) {
286 // Brightness: C-new = C-old*(1-amount) + amount
287 float scale = 1f - brightnessF;
288 float[] mat = sTempBrightnessMatrix.getArray();
289 mat[0] = scale;
290 mat[6] = scale;
291 mat[12] = scale;
292 mat[4] = brightnessI;
293 mat[9] = brightnessI;
294 mat[14] = brightnessI;
295 sTempFilterMatrix.preConcat(sTempBrightnessMatrix);
296 }
297 filter = new ColorMatrixColorFilter(sTempFilterMatrix);
298 }
299 sCachedFilter.append(key, filter);
Sunny Goyal508da152014-08-14 10:53:27 -0700300 }
301 mPaint.setColorFilter(filter);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700302 } else {
303 mPaint.setColorFilter(null);
304 }
Winsonc0880492015-08-21 11:16:27 -0700305 invalidateSelf();
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700306 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800307}