blob: 38700805f6885286e49ea9daa6ea387276d4d722 [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 {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070037
Winsonc0880492015-08-21 11:16:27 -070038 /**
39 * The possible states that a FastBitmapDrawable can be in.
40 */
41 public enum State {
42
43 NORMAL (0f, 0f, 1f, new DecelerateInterpolator()),
44 PRESSED (0f, 100f / 255f, 1f, CLICK_FEEDBACK_INTERPOLATOR),
Winsonc08c59d2015-10-28 15:30:38 -070045 FAST_SCROLL_HIGHLIGHTED (0f, 0f, 1.15f, new DecelerateInterpolator()),
46 FAST_SCROLL_UNHIGHLIGHTED (0f, 0f, 1f, new DecelerateInterpolator()),
Winsonc0880492015-08-21 11:16:27 -070047 DISABLED (1f, 0.5f, 1f, new DecelerateInterpolator());
48
49 public final float desaturation;
50 public final float brightness;
51 /**
52 * Used specifically by the view drawing this FastBitmapDrawable.
53 */
54 public final float viewScale;
55 public final TimeInterpolator interpolator;
56
57 State(float desaturation, float brightness, float viewScale, TimeInterpolator interpolator) {
58 this.desaturation = desaturation;
59 this.brightness = brightness;
60 this.viewScale = viewScale;
61 this.interpolator = interpolator;
62 }
63 }
64
65 public static final TimeInterpolator CLICK_FEEDBACK_INTERPOLATOR = new TimeInterpolator() {
Sunny Goyal508da152014-08-14 10:53:27 -070066
67 @Override
68 public float getInterpolation(float input) {
69 if (input < 0.05f) {
70 return input / 0.05f;
71 } else if (input < 0.3f){
72 return 1;
73 } else {
74 return (1 - input) / 0.7f;
75 }
76 }
77 };
Winsonc0880492015-08-21 11:16:27 -070078 public static final int CLICK_FEEDBACK_DURATION = 2000;
79 public static final int FAST_SCROLL_HIGHLIGHT_DURATION = 225;
80 public static final int FAST_SCROLL_UNHIGHLIGHT_DURATION = 150;
81 public static final int FAST_SCROLL_UNHIGHLIGHT_FROM_NORMAL_DURATION = 225;
82 public static final int FAST_SCROLL_INACTIVE_DURATION = 275;
Sunny Goyal508da152014-08-14 10:53:27 -070083
Winsonc0880492015-08-21 11:16:27 -070084 // Since we don't need 256^2 values for combinations of both the brightness and saturation, we
85 // reduce the value space to a smaller value V, which reduces the number of cached
86 // ColorMatrixColorFilters that we need to keep to V^2
87 private static final int REDUCED_FILTER_VALUE_SPACE = 48;
Sunny Goyal95abbb32014-08-04 10:53:22 -070088
Winsonc0880492015-08-21 11:16:27 -070089 // A cache of ColorFilters for optimizing brightness and saturation animations
90 private static final SparseArray<ColorFilter> sCachedFilter = new SparseArray<>();
Sunny Goyal508da152014-08-14 10:53:27 -070091
Winsonc0880492015-08-21 11:16:27 -070092 // Temporary matrices used for calculation
93 private static final ColorMatrix sTempBrightnessMatrix = new ColorMatrix();
94 private static final ColorMatrix sTempFilterMatrix = new ColorMatrix();
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070095
Adam Cohen119e8982016-02-05 14:47:50 -080096 private final Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG);
Sunny Goyal508da152014-08-14 10:53:27 -070097 private final Bitmap mBitmap;
Winsonc0880492015-08-21 11:16:27 -070098 private State mState = State.NORMAL;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070099
Winsonc0880492015-08-21 11:16:27 -0700100 // The saturation and brightness are values that are mapped to REDUCED_FILTER_VALUE_SPACE and
101 // as a result, can be used to compose the key for the cached ColorMatrixColorFilters
102 private int mDesaturation = 0;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700103 private int mBrightness = 0;
Winsonc0880492015-08-21 11:16:27 -0700104 private int mAlpha = 255;
105 private int mPrevUpdateKey = Integer.MAX_VALUE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106
Winsonc0880492015-08-21 11:16:27 -0700107 // Animators for the fast bitmap drawable's properties
108 private AnimatorSet mPropertyAnimator;
Sunny Goyal508da152014-08-14 10:53:27 -0700109
Hyunyoung Song3f471442015-04-08 19:01:34 -0700110 public FastBitmapDrawable(Bitmap b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111 mBitmap = b;
Winson Chung268f1c52013-11-18 14:04:41 -0800112 setBounds(0, 0, b.getWidth(), b.getHeight());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113 }
114
115 @Override
116 public void draw(Canvas canvas) {
Winsonc0880492015-08-21 11:16:27 -0700117 canvas.drawBitmap(mBitmap, null, getBounds(), mPaint);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118 }
119
120 @Override
Adam Cohenbadf71e2011-05-26 19:08:29 -0700121 public void setColorFilter(ColorFilter cf) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700122 // No op
Adam Cohenbadf71e2011-05-26 19:08:29 -0700123 }
124
125 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126 public int getOpacity() {
127 return PixelFormat.TRANSLUCENT;
128 }
129
130 @Override
131 public void setAlpha(int alpha) {
Winson Chung29d6fea2010-12-01 15:47:31 -0800132 mAlpha = alpha;
Winson Chungb3347bb2010-08-19 14:51:28 -0700133 mPaint.setAlpha(alpha);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134 }
135
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700136 @Override
Adam Cohen76fc0852011-06-17 13:26:23 -0700137 public void setFilterBitmap(boolean filterBitmap) {
138 mPaint.setFilterBitmap(filterBitmap);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700139 mPaint.setAntiAlias(filterBitmap);
Adam Cohen76fc0852011-06-17 13:26:23 -0700140 }
141
Winson Chung29d6fea2010-12-01 15:47:31 -0800142 public int getAlpha() {
143 return mAlpha;
144 }
145
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 public int getIntrinsicWidth() {
Sunny Goyalc424f222014-09-05 07:04:59 -0700148 return mBitmap.getWidth();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149 }
150
151 @Override
152 public int getIntrinsicHeight() {
Sunny Goyalc424f222014-09-05 07:04:59 -0700153 return mBitmap.getHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 }
155
156 @Override
157 public int getMinimumWidth() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800158 return getBounds().width();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 }
160
161 @Override
162 public int getMinimumHeight() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800163 return getBounds().height();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800164 }
165
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 public Bitmap getBitmap() {
167 return mBitmap;
168 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700169
Sunny Goyal95abbb32014-08-04 10:53:22 -0700170 /**
Winsonc0880492015-08-21 11:16:27 -0700171 * Animates this drawable to a new state.
172 *
173 * @return whether the state has changed.
Sunny Goyal95abbb32014-08-04 10:53:22 -0700174 */
Winsonc0880492015-08-21 11:16:27 -0700175 public boolean animateState(State newState) {
176 State prevState = mState;
177 if (mState != newState) {
178 mState = newState;
179
180 mPropertyAnimator = cancelAnimator(mPropertyAnimator);
181 mPropertyAnimator = new AnimatorSet();
182 mPropertyAnimator.playTogether(
183 ObjectAnimator
184 .ofFloat(this, "desaturation", newState.desaturation),
185 ObjectAnimator
186 .ofFloat(this, "brightness", newState.brightness));
187 mPropertyAnimator.setInterpolator(newState.interpolator);
188 mPropertyAnimator.setDuration(getDurationForStateChange(prevState, newState));
189 mPropertyAnimator.setStartDelay(getStartDelayForStateChange(prevState, newState));
190 mPropertyAnimator.start();
191 return true;
192 }
193 return false;
194 }
195
196 /**
197 * Immediately sets this drawable to a new state.
198 *
199 * @return whether the state has changed.
200 */
201 public boolean setState(State newState) {
202 if (mState != newState) {
203 mState = newState;
204
205 mPropertyAnimator = cancelAnimator(mPropertyAnimator);
206
207 setDesaturation(newState.desaturation);
208 setBrightness(newState.brightness);
209 return true;
210 }
211 return false;
212 }
213
214 /**
215 * Returns the current state.
216 */
217 public State getCurrentState() {
218 return mState;
219 }
220
221 /**
222 * Returns the duration for the state change animation.
223 */
224 public static int getDurationForStateChange(State fromState, State toState) {
225 switch (toState) {
226 case NORMAL:
227 switch (fromState) {
228 case PRESSED:
229 return 0;
230 case FAST_SCROLL_HIGHLIGHTED:
231 case FAST_SCROLL_UNHIGHLIGHTED:
232 return FAST_SCROLL_INACTIVE_DURATION;
233 }
234 case PRESSED:
235 return CLICK_FEEDBACK_DURATION;
236 case FAST_SCROLL_HIGHLIGHTED:
237 return FAST_SCROLL_HIGHLIGHT_DURATION;
238 case FAST_SCROLL_UNHIGHLIGHTED:
239 switch (fromState) {
240 case NORMAL:
241 // When animating from normal state, take a little longer
242 return FAST_SCROLL_UNHIGHLIGHT_FROM_NORMAL_DURATION;
243 default:
244 return FAST_SCROLL_UNHIGHLIGHT_DURATION;
245 }
246 }
247 return 0;
248 }
249
250 /**
251 * Returns the start delay when animating between certain fast scroll states.
252 */
253 public static int getStartDelayForStateChange(State fromState, State toState) {
254 switch (toState) {
255 case FAST_SCROLL_UNHIGHLIGHTED:
256 switch (fromState) {
257 case NORMAL:
258 return FAST_SCROLL_UNHIGHLIGHT_DURATION / 4;
259 }
260 }
261 return 0;
262 }
263
264 /**
265 * Sets the saturation of this icon, 0 [full color] -> 1 [desaturated]
266 */
267 public void setDesaturation(float desaturation) {
268 int newDesaturation = (int) Math.floor(desaturation * REDUCED_FILTER_VALUE_SPACE);
269 if (mDesaturation != newDesaturation) {
270 mDesaturation = newDesaturation;
Sunny Goyal95abbb32014-08-04 10:53:22 -0700271 updateFilter();
272 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700273 }
274
Winsonc0880492015-08-21 11:16:27 -0700275 public float getDesaturation() {
276 return (float) mDesaturation / REDUCED_FILTER_VALUE_SPACE;
Sunny Goyal508da152014-08-14 10:53:27 -0700277 }
278
Winsonc0880492015-08-21 11:16:27 -0700279 /**
280 * Sets the brightness of this icon, 0 [no add. brightness] -> 1 [2bright2furious]
281 */
282 public void setBrightness(float brightness) {
283 int newBrightness = (int) Math.floor(brightness * REDUCED_FILTER_VALUE_SPACE);
284 if (mBrightness != newBrightness) {
285 mBrightness = newBrightness;
Sunny Goyal95abbb32014-08-04 10:53:22 -0700286 updateFilter();
287 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700288 }
289
Winsonc0880492015-08-21 11:16:27 -0700290 public float getBrightness() {
291 return (float) mBrightness / REDUCED_FILTER_VALUE_SPACE;
292 }
293
294 /**
295 * Updates the paint to reflect the current brightness and saturation.
296 */
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700297 private void updateFilter() {
Winsonc0880492015-08-21 11:16:27 -0700298 boolean usePorterDuffFilter = false;
299 int key = -1;
300 if (mDesaturation > 0) {
301 key = (mDesaturation << 16) | mBrightness;
302 } else if (mBrightness > 0) {
303 // Compose a key with a fully saturated icon if we are just animating brightness
304 key = (1 << 16) | mBrightness;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700305
Winsonc0880492015-08-21 11:16:27 -0700306 // We found that in L, ColorFilters cause drawing artifacts with shadows baked into
307 // icons, so just use a PorterDuff filter when we aren't animating saturation
308 usePorterDuffFilter = true;
309 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700310
Winsonc0880492015-08-21 11:16:27 -0700311 // Debounce multiple updates on the same frame
312 if (key == mPrevUpdateKey) {
313 return;
314 }
315 mPrevUpdateKey = key;
316
317 if (key != -1) {
318 ColorFilter filter = sCachedFilter.get(key);
Sunny Goyal508da152014-08-14 10:53:27 -0700319 if (filter == null) {
Winsonc0880492015-08-21 11:16:27 -0700320 float brightnessF = getBrightness();
321 int brightnessI = (int) (255 * brightnessF);
322 if (usePorterDuffFilter) {
323 filter = new PorterDuffColorFilter(Color.argb(brightnessI, 255, 255, 255),
324 PorterDuff.Mode.SRC_ATOP);
325 } else {
326 float saturationF = 1f - getDesaturation();
327 sTempFilterMatrix.setSaturation(saturationF);
328 if (mBrightness > 0) {
329 // Brightness: C-new = C-old*(1-amount) + amount
330 float scale = 1f - brightnessF;
331 float[] mat = sTempBrightnessMatrix.getArray();
332 mat[0] = scale;
333 mat[6] = scale;
334 mat[12] = scale;
335 mat[4] = brightnessI;
336 mat[9] = brightnessI;
337 mat[14] = brightnessI;
338 sTempFilterMatrix.preConcat(sTempBrightnessMatrix);
339 }
340 filter = new ColorMatrixColorFilter(sTempFilterMatrix);
341 }
342 sCachedFilter.append(key, filter);
Sunny Goyal508da152014-08-14 10:53:27 -0700343 }
344 mPaint.setColorFilter(filter);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700345 } else {
346 mPaint.setColorFilter(null);
347 }
Winsonc0880492015-08-21 11:16:27 -0700348 invalidateSelf();
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700349 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700350
Winsonc0880492015-08-21 11:16:27 -0700351 private AnimatorSet cancelAnimator(AnimatorSet animator) {
352 if (animator != null) {
353 animator.removeAllListeners();
354 animator.cancel();
355 }
356 return null;
Sunny Goyal95abbb32014-08-04 10:53:22 -0700357 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358}