blob: 270d5393916918c7ccea7544dd47cc39e7bd43f2 [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
Adam Cohen119e8982016-02-05 14:47:50 -080097 private 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) {
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
122 @Override
Adam Cohenbadf71e2011-05-26 19:08:29 -0700123 public void setColorFilter(ColorFilter cf) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700124 // No op
Adam Cohenbadf71e2011-05-26 19:08:29 -0700125 }
126
127 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128 public int getOpacity() {
129 return PixelFormat.TRANSLUCENT;
130 }
131
132 @Override
133 public void setAlpha(int alpha) {
Winson Chung29d6fea2010-12-01 15:47:31 -0800134 mAlpha = alpha;
Winson Chungb3347bb2010-08-19 14:51:28 -0700135 mPaint.setAlpha(alpha);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136 }
137
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700138 @Override
Adam Cohen76fc0852011-06-17 13:26:23 -0700139 public void setFilterBitmap(boolean filterBitmap) {
140 mPaint.setFilterBitmap(filterBitmap);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700141 mPaint.setAntiAlias(filterBitmap);
Adam Cohen76fc0852011-06-17 13:26:23 -0700142 }
143
Winson Chung29d6fea2010-12-01 15:47:31 -0800144 public int getAlpha() {
145 return mAlpha;
146 }
147
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149 public int getIntrinsicWidth() {
Sunny Goyalc424f222014-09-05 07:04:59 -0700150 return mBitmap.getWidth();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 }
152
153 @Override
154 public int getIntrinsicHeight() {
Sunny Goyalc424f222014-09-05 07:04:59 -0700155 return mBitmap.getHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 }
157
158 @Override
159 public int getMinimumWidth() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800160 return getBounds().width();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 }
162
163 @Override
164 public int getMinimumHeight() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800165 return getBounds().height();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800166 }
167
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 public Bitmap getBitmap() {
169 return mBitmap;
170 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700171
Sunny Goyal95abbb32014-08-04 10:53:22 -0700172 /**
Winsonc0880492015-08-21 11:16:27 -0700173 * Animates this drawable to a new state.
174 *
175 * @return whether the state has changed.
Sunny Goyal95abbb32014-08-04 10:53:22 -0700176 */
Winsonc0880492015-08-21 11:16:27 -0700177 public boolean animateState(State newState) {
178 State prevState = mState;
179 if (mState != newState) {
180 mState = newState;
181
Tony Wickham6b910a22016-11-08 10:40:34 -0800182 float desaturation = mIsDisabled ? DISABLED_DESATURATION : newState.desaturation;
183 float brightness = mIsDisabled ? DISABLED_BRIGHTNESS: newState.brightness;
184
Winsonc0880492015-08-21 11:16:27 -0700185 mPropertyAnimator = cancelAnimator(mPropertyAnimator);
186 mPropertyAnimator = new AnimatorSet();
187 mPropertyAnimator.playTogether(
Tony Wickham6b910a22016-11-08 10:40:34 -0800188 ObjectAnimator.ofFloat(this, "desaturation", desaturation),
189 ObjectAnimator.ofFloat(this, "brightness", brightness));
Winsonc0880492015-08-21 11:16:27 -0700190 mPropertyAnimator.setInterpolator(newState.interpolator);
191 mPropertyAnimator.setDuration(getDurationForStateChange(prevState, newState));
192 mPropertyAnimator.setStartDelay(getStartDelayForStateChange(prevState, newState));
193 mPropertyAnimator.start();
194 return true;
195 }
196 return false;
197 }
198
199 /**
200 * Immediately sets this drawable to a new state.
201 *
202 * @return whether the state has changed.
203 */
204 public boolean setState(State newState) {
205 if (mState != newState) {
206 mState = newState;
207
208 mPropertyAnimator = cancelAnimator(mPropertyAnimator);
209
Tony Wickham6b910a22016-11-08 10:40:34 -0800210 invalidateDesaturationAndBrightness();
Winsonc0880492015-08-21 11:16:27 -0700211 return true;
212 }
213 return false;
214 }
215
Tony Wickham6b910a22016-11-08 10:40:34 -0800216 private void invalidateDesaturationAndBrightness() {
217 setDesaturation(mIsDisabled ? DISABLED_DESATURATION : mState.desaturation);
218 setBrightness(mIsDisabled ? DISABLED_BRIGHTNESS: mState.brightness);
219 }
220
Winsonc0880492015-08-21 11:16:27 -0700221 /**
222 * Returns the current state.
223 */
224 public State getCurrentState() {
225 return mState;
226 }
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 /**
236 * Returns the duration for the state change animation.
237 */
238 public static int getDurationForStateChange(State fromState, State toState) {
239 switch (toState) {
240 case NORMAL:
241 switch (fromState) {
242 case PRESSED:
243 return 0;
244 case FAST_SCROLL_HIGHLIGHTED:
245 case FAST_SCROLL_UNHIGHLIGHTED:
246 return FAST_SCROLL_INACTIVE_DURATION;
247 }
248 case PRESSED:
249 return CLICK_FEEDBACK_DURATION;
250 case FAST_SCROLL_HIGHLIGHTED:
251 return FAST_SCROLL_HIGHLIGHT_DURATION;
252 case FAST_SCROLL_UNHIGHLIGHTED:
253 switch (fromState) {
254 case NORMAL:
255 // When animating from normal state, take a little longer
256 return FAST_SCROLL_UNHIGHLIGHT_FROM_NORMAL_DURATION;
257 default:
258 return FAST_SCROLL_UNHIGHLIGHT_DURATION;
259 }
260 }
261 return 0;
262 }
263
264 /**
265 * Returns the start delay when animating between certain fast scroll states.
266 */
267 public static int getStartDelayForStateChange(State fromState, State toState) {
268 switch (toState) {
269 case FAST_SCROLL_UNHIGHLIGHTED:
270 switch (fromState) {
271 case NORMAL:
272 return FAST_SCROLL_UNHIGHLIGHT_DURATION / 4;
273 }
274 }
275 return 0;
276 }
277
278 /**
279 * Sets the saturation of this icon, 0 [full color] -> 1 [desaturated]
280 */
281 public void setDesaturation(float desaturation) {
282 int newDesaturation = (int) Math.floor(desaturation * REDUCED_FILTER_VALUE_SPACE);
283 if (mDesaturation != newDesaturation) {
284 mDesaturation = newDesaturation;
Sunny Goyal95abbb32014-08-04 10:53:22 -0700285 updateFilter();
286 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700287 }
288
Winsonc0880492015-08-21 11:16:27 -0700289 public float getDesaturation() {
290 return (float) mDesaturation / REDUCED_FILTER_VALUE_SPACE;
Sunny Goyal508da152014-08-14 10:53:27 -0700291 }
292
Winsonc0880492015-08-21 11:16:27 -0700293 /**
294 * Sets the brightness of this icon, 0 [no add. brightness] -> 1 [2bright2furious]
295 */
296 public void setBrightness(float brightness) {
297 int newBrightness = (int) Math.floor(brightness * REDUCED_FILTER_VALUE_SPACE);
298 if (mBrightness != newBrightness) {
299 mBrightness = newBrightness;
Sunny Goyal95abbb32014-08-04 10:53:22 -0700300 updateFilter();
301 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700302 }
303
Winsonc0880492015-08-21 11:16:27 -0700304 public float getBrightness() {
305 return (float) mBrightness / REDUCED_FILTER_VALUE_SPACE;
306 }
307
308 /**
309 * Updates the paint to reflect the current brightness and saturation.
310 */
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700311 private void updateFilter() {
Winsonc0880492015-08-21 11:16:27 -0700312 boolean usePorterDuffFilter = false;
313 int key = -1;
314 if (mDesaturation > 0) {
315 key = (mDesaturation << 16) | mBrightness;
316 } else if (mBrightness > 0) {
317 // Compose a key with a fully saturated icon if we are just animating brightness
318 key = (1 << 16) | mBrightness;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700319
Winsonc0880492015-08-21 11:16:27 -0700320 // We found that in L, ColorFilters cause drawing artifacts with shadows baked into
321 // icons, so just use a PorterDuff filter when we aren't animating saturation
322 usePorterDuffFilter = true;
323 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700324
Winsonc0880492015-08-21 11:16:27 -0700325 // Debounce multiple updates on the same frame
326 if (key == mPrevUpdateKey) {
327 return;
328 }
329 mPrevUpdateKey = key;
330
331 if (key != -1) {
332 ColorFilter filter = sCachedFilter.get(key);
Sunny Goyal508da152014-08-14 10:53:27 -0700333 if (filter == null) {
Winsonc0880492015-08-21 11:16:27 -0700334 float brightnessF = getBrightness();
335 int brightnessI = (int) (255 * brightnessF);
336 if (usePorterDuffFilter) {
337 filter = new PorterDuffColorFilter(Color.argb(brightnessI, 255, 255, 255),
338 PorterDuff.Mode.SRC_ATOP);
339 } else {
340 float saturationF = 1f - getDesaturation();
341 sTempFilterMatrix.setSaturation(saturationF);
342 if (mBrightness > 0) {
343 // Brightness: C-new = C-old*(1-amount) + amount
344 float scale = 1f - brightnessF;
345 float[] mat = sTempBrightnessMatrix.getArray();
346 mat[0] = scale;
347 mat[6] = scale;
348 mat[12] = scale;
349 mat[4] = brightnessI;
350 mat[9] = brightnessI;
351 mat[14] = brightnessI;
352 sTempFilterMatrix.preConcat(sTempBrightnessMatrix);
353 }
354 filter = new ColorMatrixColorFilter(sTempFilterMatrix);
355 }
356 sCachedFilter.append(key, filter);
Sunny Goyal508da152014-08-14 10:53:27 -0700357 }
358 mPaint.setColorFilter(filter);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700359 } else {
360 mPaint.setColorFilter(null);
361 }
Winsonc0880492015-08-21 11:16:27 -0700362 invalidateSelf();
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700363 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700364
Winsonc0880492015-08-21 11:16:27 -0700365 private AnimatorSet cancelAnimator(AnimatorSet animator) {
366 if (animator != null) {
Winsonc0880492015-08-21 11:16:27 -0700367 animator.cancel();
368 }
369 return null;
Sunny Goyal95abbb32014-08-04 10:53:22 -0700370 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800371}