blob: 0cefc57392365556e64afefd6a5168bd7947e172 [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
Winsonc0880492015-08-21 11:16:27 -070019import android.animation.AnimatorSet;
Sunny Goyal508da152014-08-14 10:53:27 -070020import android.animation.ObjectAnimator;
21import android.animation.TimeInterpolator;
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;
Winson Chung45e1d6e2010-11-09 17:19:49 -080032import android.graphics.drawable.Drawable;
Sunny Goyal508da152014-08-14 10:53:27 -070033import android.util.SparseArray;
Winsonc0880492015-08-21 11:16:27 -070034import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035
Hyunyoung Song3f471442015-04-08 19:01:34 -070036public class FastBitmapDrawable extends Drawable {
Tony Wickham6b910a22016-11-08 10:40:34 -080037 private static final float DISABLED_DESATURATION = 1f;
38 private static final float DISABLED_BRIGHTNESS = 0.5f;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070039
Winsonc0880492015-08-21 11:16:27 -070040 /**
41 * The possible states that a FastBitmapDrawable can be in.
42 */
43 public enum State {
44
45 NORMAL (0f, 0f, 1f, new DecelerateInterpolator()),
46 PRESSED (0f, 100f / 255f, 1f, CLICK_FEEDBACK_INTERPOLATOR),
Winsonc08c59d2015-10-28 15:30:38 -070047 FAST_SCROLL_HIGHLIGHTED (0f, 0f, 1.15f, new DecelerateInterpolator()),
Tony Wickham6b910a22016-11-08 10:40:34 -080048 FAST_SCROLL_UNHIGHLIGHTED (0f, 0f, 1f, new DecelerateInterpolator());
Winsonc0880492015-08-21 11:16:27 -070049
50 public final float desaturation;
51 public final float brightness;
52 /**
53 * Used specifically by the view drawing this FastBitmapDrawable.
54 */
55 public final float viewScale;
56 public final TimeInterpolator interpolator;
57
58 State(float desaturation, float brightness, float viewScale, TimeInterpolator interpolator) {
59 this.desaturation = desaturation;
60 this.brightness = brightness;
61 this.viewScale = viewScale;
62 this.interpolator = interpolator;
63 }
64 }
65
66 public static final TimeInterpolator CLICK_FEEDBACK_INTERPOLATOR = new TimeInterpolator() {
Sunny Goyal508da152014-08-14 10:53:27 -070067
68 @Override
69 public float getInterpolation(float input) {
70 if (input < 0.05f) {
71 return input / 0.05f;
72 } else if (input < 0.3f){
73 return 1;
74 } else {
75 return (1 - input) / 0.7f;
76 }
77 }
78 };
Winsonc0880492015-08-21 11:16:27 -070079 public static final int CLICK_FEEDBACK_DURATION = 2000;
80 public static final int FAST_SCROLL_HIGHLIGHT_DURATION = 225;
81 public static final int FAST_SCROLL_UNHIGHLIGHT_DURATION = 150;
82 public static final int FAST_SCROLL_UNHIGHLIGHT_FROM_NORMAL_DURATION = 225;
83 public static final int FAST_SCROLL_INACTIVE_DURATION = 275;
Sunny Goyal508da152014-08-14 10:53:27 -070084
Winsonc0880492015-08-21 11:16:27 -070085 // Since we don't need 256^2 values for combinations of both the brightness and saturation, we
86 // reduce the value space to a smaller value V, which reduces the number of cached
87 // ColorMatrixColorFilters that we need to keep to V^2
88 private static final int REDUCED_FILTER_VALUE_SPACE = 48;
Sunny Goyal95abbb32014-08-04 10:53:22 -070089
Winsonc0880492015-08-21 11:16:27 -070090 // A cache of ColorFilters for optimizing brightness and saturation animations
91 private static final SparseArray<ColorFilter> sCachedFilter = new SparseArray<>();
Sunny Goyal508da152014-08-14 10:53:27 -070092
Winsonc0880492015-08-21 11:16:27 -070093 // Temporary matrices used for calculation
94 private static final ColorMatrix sTempBrightnessMatrix = new ColorMatrix();
95 private static final ColorMatrix sTempFilterMatrix = new ColorMatrix();
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070096
Sunny Goyal55cb70b2016-11-12 09:58:29 -080097 protected final Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG);
Sunny Goyal508da152014-08-14 10:53:27 -070098 private final Bitmap mBitmap;
Winsonc0880492015-08-21 11:16:27 -070099 private State mState = State.NORMAL;
Tony Wickham6b910a22016-11-08 10:40:34 -0800100 private boolean mIsDisabled;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700101
Winsonc0880492015-08-21 11:16:27 -0700102 // The saturation and brightness are values that are mapped to REDUCED_FILTER_VALUE_SPACE and
103 // as a result, can be used to compose the key for the cached ColorMatrixColorFilters
104 private int mDesaturation = 0;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700105 private int mBrightness = 0;
Winsonc0880492015-08-21 11:16:27 -0700106 private int mAlpha = 255;
107 private int mPrevUpdateKey = Integer.MAX_VALUE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108
Winsonc0880492015-08-21 11:16:27 -0700109 // Animators for the fast bitmap drawable's properties
110 private AnimatorSet mPropertyAnimator;
Sunny Goyal508da152014-08-14 10:53:27 -0700111
Hyunyoung Song3f471442015-04-08 19:01:34 -0700112 public FastBitmapDrawable(Bitmap b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113 mBitmap = b;
Winson Chung268f1c52013-11-18 14:04:41 -0800114 setBounds(0, 0, b.getWidth(), b.getHeight());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115 }
116
117 @Override
118 public void draw(Canvas canvas) {
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800119 drawInternal(canvas);
120 }
121
122 public void drawWithBrightness(Canvas canvas, float brightness) {
123 float oldBrightness = getBrightness();
124 setBrightness(brightness);
125 drawInternal(canvas);
126 setBrightness(oldBrightness);
127 }
128
129 protected void drawInternal(Canvas canvas) {
Winsonc0880492015-08-21 11:16:27 -0700130 canvas.drawBitmap(mBitmap, null, getBounds(), mPaint);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131 }
132
133 @Override
Adam Cohenbadf71e2011-05-26 19:08:29 -0700134 public void setColorFilter(ColorFilter cf) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700135 // No op
Adam Cohenbadf71e2011-05-26 19:08:29 -0700136 }
137
138 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139 public int getOpacity() {
140 return PixelFormat.TRANSLUCENT;
141 }
142
143 @Override
144 public void setAlpha(int alpha) {
Winson Chung29d6fea2010-12-01 15:47:31 -0800145 mAlpha = alpha;
Winson Chungb3347bb2010-08-19 14:51:28 -0700146 mPaint.setAlpha(alpha);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 }
148
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700149 @Override
Adam Cohen76fc0852011-06-17 13:26:23 -0700150 public void setFilterBitmap(boolean filterBitmap) {
151 mPaint.setFilterBitmap(filterBitmap);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700152 mPaint.setAntiAlias(filterBitmap);
Adam Cohen76fc0852011-06-17 13:26:23 -0700153 }
154
Winson Chung29d6fea2010-12-01 15:47:31 -0800155 public int getAlpha() {
156 return mAlpha;
157 }
158
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 public int getIntrinsicWidth() {
Sunny Goyalc424f222014-09-05 07:04:59 -0700161 return mBitmap.getWidth();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 }
163
164 @Override
165 public int getIntrinsicHeight() {
Sunny Goyalc424f222014-09-05 07:04:59 -0700166 return mBitmap.getHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 }
168
169 @Override
170 public int getMinimumWidth() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800171 return getBounds().width();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 }
173
174 @Override
175 public int getMinimumHeight() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800176 return getBounds().height();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800177 }
178
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 public Bitmap getBitmap() {
180 return mBitmap;
181 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700182
Sunny Goyal95abbb32014-08-04 10:53:22 -0700183 /**
Winsonc0880492015-08-21 11:16:27 -0700184 * Animates this drawable to a new state.
185 *
186 * @return whether the state has changed.
Sunny Goyal95abbb32014-08-04 10:53:22 -0700187 */
Winsonc0880492015-08-21 11:16:27 -0700188 public boolean animateState(State newState) {
189 State prevState = mState;
190 if (mState != newState) {
191 mState = newState;
192
Tony Wickham6b910a22016-11-08 10:40:34 -0800193 float desaturation = mIsDisabled ? DISABLED_DESATURATION : newState.desaturation;
194 float brightness = mIsDisabled ? DISABLED_BRIGHTNESS: newState.brightness;
195
Winsonc0880492015-08-21 11:16:27 -0700196 mPropertyAnimator = cancelAnimator(mPropertyAnimator);
197 mPropertyAnimator = new AnimatorSet();
198 mPropertyAnimator.playTogether(
Tony Wickham6b910a22016-11-08 10:40:34 -0800199 ObjectAnimator.ofFloat(this, "desaturation", desaturation),
200 ObjectAnimator.ofFloat(this, "brightness", brightness));
Winsonc0880492015-08-21 11:16:27 -0700201 mPropertyAnimator.setInterpolator(newState.interpolator);
202 mPropertyAnimator.setDuration(getDurationForStateChange(prevState, newState));
203 mPropertyAnimator.setStartDelay(getStartDelayForStateChange(prevState, newState));
204 mPropertyAnimator.start();
205 return true;
206 }
207 return false;
208 }
209
210 /**
211 * Immediately sets this drawable to a new state.
212 *
213 * @return whether the state has changed.
214 */
215 public boolean setState(State newState) {
216 if (mState != newState) {
217 mState = newState;
218
219 mPropertyAnimator = cancelAnimator(mPropertyAnimator);
220
Tony Wickham6b910a22016-11-08 10:40:34 -0800221 invalidateDesaturationAndBrightness();
Winsonc0880492015-08-21 11:16:27 -0700222 return true;
223 }
224 return false;
225 }
226
Tony Wickham6b910a22016-11-08 10:40:34 -0800227 private void invalidateDesaturationAndBrightness() {
228 setDesaturation(mIsDisabled ? DISABLED_DESATURATION : mState.desaturation);
229 setBrightness(mIsDisabled ? DISABLED_BRIGHTNESS: mState.brightness);
230 }
231
Winsonc0880492015-08-21 11:16:27 -0700232 /**
233 * Returns the current state.
234 */
235 public State getCurrentState() {
236 return mState;
237 }
238
Tony Wickham6b910a22016-11-08 10:40:34 -0800239 public void setIsDisabled(boolean isDisabled) {
240 if (mIsDisabled != isDisabled) {
241 mIsDisabled = isDisabled;
242 invalidateDesaturationAndBrightness();
243 }
244 }
245
Winsonc0880492015-08-21 11:16:27 -0700246 /**
247 * Returns the duration for the state change animation.
248 */
249 public static int getDurationForStateChange(State fromState, State toState) {
250 switch (toState) {
251 case NORMAL:
252 switch (fromState) {
253 case PRESSED:
254 return 0;
255 case FAST_SCROLL_HIGHLIGHTED:
256 case FAST_SCROLL_UNHIGHLIGHTED:
257 return FAST_SCROLL_INACTIVE_DURATION;
258 }
259 case PRESSED:
260 return CLICK_FEEDBACK_DURATION;
261 case FAST_SCROLL_HIGHLIGHTED:
262 return FAST_SCROLL_HIGHLIGHT_DURATION;
263 case FAST_SCROLL_UNHIGHLIGHTED:
264 switch (fromState) {
265 case NORMAL:
266 // When animating from normal state, take a little longer
267 return FAST_SCROLL_UNHIGHLIGHT_FROM_NORMAL_DURATION;
268 default:
269 return FAST_SCROLL_UNHIGHLIGHT_DURATION;
270 }
271 }
272 return 0;
273 }
274
275 /**
276 * Returns the start delay when animating between certain fast scroll states.
277 */
278 public static int getStartDelayForStateChange(State fromState, State toState) {
279 switch (toState) {
280 case FAST_SCROLL_UNHIGHLIGHTED:
281 switch (fromState) {
282 case NORMAL:
283 return FAST_SCROLL_UNHIGHLIGHT_DURATION / 4;
284 }
285 }
286 return 0;
287 }
288
289 /**
290 * Sets the saturation of this icon, 0 [full color] -> 1 [desaturated]
291 */
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800292 private void setDesaturation(float desaturation) {
Winsonc0880492015-08-21 11:16:27 -0700293 int newDesaturation = (int) Math.floor(desaturation * REDUCED_FILTER_VALUE_SPACE);
294 if (mDesaturation != newDesaturation) {
295 mDesaturation = newDesaturation;
Sunny Goyal95abbb32014-08-04 10:53:22 -0700296 updateFilter();
297 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700298 }
299
Winsonc0880492015-08-21 11:16:27 -0700300 public float getDesaturation() {
301 return (float) mDesaturation / REDUCED_FILTER_VALUE_SPACE;
Sunny Goyal508da152014-08-14 10:53:27 -0700302 }
303
Winsonc0880492015-08-21 11:16:27 -0700304 /**
305 * Sets the brightness of this icon, 0 [no add. brightness] -> 1 [2bright2furious]
306 */
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800307 private void setBrightness(float brightness) {
Winsonc0880492015-08-21 11:16:27 -0700308 int newBrightness = (int) Math.floor(brightness * REDUCED_FILTER_VALUE_SPACE);
309 if (mBrightness != newBrightness) {
310 mBrightness = newBrightness;
Sunny Goyal95abbb32014-08-04 10:53:22 -0700311 updateFilter();
312 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700313 }
314
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800315 private float getBrightness() {
Winsonc0880492015-08-21 11:16:27 -0700316 return (float) mBrightness / REDUCED_FILTER_VALUE_SPACE;
317 }
318
319 /**
320 * Updates the paint to reflect the current brightness and saturation.
321 */
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700322 private void updateFilter() {
Winsonc0880492015-08-21 11:16:27 -0700323 boolean usePorterDuffFilter = false;
324 int key = -1;
325 if (mDesaturation > 0) {
326 key = (mDesaturation << 16) | mBrightness;
327 } else if (mBrightness > 0) {
328 // Compose a key with a fully saturated icon if we are just animating brightness
329 key = (1 << 16) | mBrightness;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700330
Winsonc0880492015-08-21 11:16:27 -0700331 // We found that in L, ColorFilters cause drawing artifacts with shadows baked into
332 // icons, so just use a PorterDuff filter when we aren't animating saturation
333 usePorterDuffFilter = true;
334 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700335
Winsonc0880492015-08-21 11:16:27 -0700336 // Debounce multiple updates on the same frame
337 if (key == mPrevUpdateKey) {
338 return;
339 }
340 mPrevUpdateKey = key;
341
342 if (key != -1) {
343 ColorFilter filter = sCachedFilter.get(key);
Sunny Goyal508da152014-08-14 10:53:27 -0700344 if (filter == null) {
Winsonc0880492015-08-21 11:16:27 -0700345 float brightnessF = getBrightness();
346 int brightnessI = (int) (255 * brightnessF);
347 if (usePorterDuffFilter) {
348 filter = new PorterDuffColorFilter(Color.argb(brightnessI, 255, 255, 255),
349 PorterDuff.Mode.SRC_ATOP);
350 } else {
351 float saturationF = 1f - getDesaturation();
352 sTempFilterMatrix.setSaturation(saturationF);
353 if (mBrightness > 0) {
354 // Brightness: C-new = C-old*(1-amount) + amount
355 float scale = 1f - brightnessF;
356 float[] mat = sTempBrightnessMatrix.getArray();
357 mat[0] = scale;
358 mat[6] = scale;
359 mat[12] = scale;
360 mat[4] = brightnessI;
361 mat[9] = brightnessI;
362 mat[14] = brightnessI;
363 sTempFilterMatrix.preConcat(sTempBrightnessMatrix);
364 }
365 filter = new ColorMatrixColorFilter(sTempFilterMatrix);
366 }
367 sCachedFilter.append(key, filter);
Sunny Goyal508da152014-08-14 10:53:27 -0700368 }
369 mPaint.setColorFilter(filter);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700370 } else {
371 mPaint.setColorFilter(null);
372 }
Winsonc0880492015-08-21 11:16:27 -0700373 invalidateSelf();
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700374 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700375
Winsonc0880492015-08-21 11:16:27 -0700376 private AnimatorSet cancelAnimator(AnimatorSet animator) {
377 if (animator != null) {
Winsonc0880492015-08-21 11:16:27 -0700378 animator.cancel();
379 }
380 return null;
Sunny Goyal95abbb32014-08-04 10:53:22 -0700381 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800382}