blob: a78159f570e76cea9ea0419749bed049686b725b [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 Goyal726bee72018-03-05 12:54:24 -080019import static com.android.launcher3.anim.Interpolators.ACCEL;
Hyunyoung Songef468d82019-01-03 01:02:43 -080020import static com.android.launcher3.anim.Interpolators.DEACCEL;
Sunny Goyal726bee72018-03-05 12:54:24 -080021
Sunny Goyal508da152014-08-14 10:53:27 -070022import android.animation.ObjectAnimator;
Sunny Goyal14168432019-10-24 15:59:49 -070023import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.graphics.Bitmap;
25import android.graphics.Canvas;
Sunny Goyal508da152014-08-14 10:53:27 -070026import android.graphics.Color;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.graphics.ColorFilter;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070028import android.graphics.ColorMatrix;
29import android.graphics.ColorMatrixColorFilter;
Winson Chung45e1d6e2010-11-09 17:19:49 -080030import android.graphics.Paint;
31import android.graphics.PixelFormat;
Sunny Goyal726bee72018-03-05 12:54:24 -080032import android.graphics.Rect;
Winson Chung45e1d6e2010-11-09 17:19:49 -080033import android.graphics.drawable.Drawable;
Tony Wickham1e618492017-02-02 12:57:18 -080034import android.util.Property;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035
Sunny Goyal14168432019-10-24 15:59:49 -070036import com.android.launcher3.graphics.PlaceHolderIconDrawable;
Hyunyoung Song48cb7bc2018-09-25 17:03:34 -070037import com.android.launcher3.icons.BitmapInfo;
Tony Wickham9a8d11f2017-01-11 09:53:12 -080038
Samuel Fufa61d39642020-01-23 12:42:06 -080039
Hyunyoung Song3f471442015-04-08 19:01:34 -070040public class FastBitmapDrawable extends Drawable {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080041
Sunny Goyal726bee72018-03-05 12:54:24 -080042 private static final float PRESSED_SCALE = 1.1f;
43
Tony Wickham6b910a22016-11-08 10:40:34 -080044 private static final float DISABLED_DESATURATION = 1f;
45 private static final float DISABLED_BRIGHTNESS = 0.5f;
Samuel Fufa61d39642020-01-23 12:42:06 -080046 private static final float DISABLED_ALPHA = 0.54f;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070047
Sunny Goyal726bee72018-03-05 12:54:24 -080048 public static final int CLICK_FEEDBACK_DURATION = 200;
Sunny Goyal508da152014-08-14 10:53:27 -070049
Samuel Fufa61d39642020-01-23 12:42:06 -080050 private static ColorFilter sDisabledFColorFilter;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070051
Sunny Goyal55cb70b2016-11-12 09:58:29 -080052 protected final Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG);
Sunny Goyal61e08462018-03-02 17:25:59 -080053 protected Bitmap mBitmap;
Sunny Goyal179249d2017-12-19 16:49:24 -080054 protected final int mIconColor;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080055
56 private boolean mIsPressed;
Tony Wickham6b910a22016-11-08 10:40:34 -080057 private boolean mIsDisabled;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070058
Sunny Goyal726bee72018-03-05 12:54:24 -080059 // Animator and properties for the fast bitmap drawable's scale
60 private static final Property<FastBitmapDrawable, Float> SCALE
61 = new Property<FastBitmapDrawable, Float>(Float.TYPE, "scale") {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080062 @Override
63 public Float get(FastBitmapDrawable fastBitmapDrawable) {
Sunny Goyal726bee72018-03-05 12:54:24 -080064 return fastBitmapDrawable.mScale;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080065 }
66
67 @Override
68 public void set(FastBitmapDrawable fastBitmapDrawable, Float value) {
Sunny Goyal726bee72018-03-05 12:54:24 -080069 fastBitmapDrawable.mScale = value;
70 fastBitmapDrawable.invalidateSelf();
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080071 }
72 };
Sunny Goyal726bee72018-03-05 12:54:24 -080073 private ObjectAnimator mScaleAnimation;
74 private float mScale = 1;
75
Winsonc0880492015-08-21 11:16:27 -070076 private int mAlpha = 255;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077
Hyunyoung Song3f471442015-04-08 19:01:34 -070078 public FastBitmapDrawable(Bitmap b) {
Sunny Goyal179249d2017-12-19 16:49:24 -080079 this(b, Color.TRANSPARENT);
80 }
81
82 public FastBitmapDrawable(BitmapInfo info) {
83 this(info.icon, info.color);
84 }
85
Sunny Goyal179249d2017-12-19 16:49:24 -080086 protected FastBitmapDrawable(Bitmap b, int iconColor) {
Jon Mirandab6d686d2019-03-29 10:49:43 -070087 this(b, iconColor, false);
88 }
89
90 protected FastBitmapDrawable(Bitmap b, int iconColor, boolean isDisabled) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091 mBitmap = b;
Sunny Goyal179249d2017-12-19 16:49:24 -080092 mIconColor = iconColor;
Sunny Goyal96ac68a2017-02-02 16:37:21 -080093 setFilterBitmap(true);
Jon Mirandab6d686d2019-03-29 10:49:43 -070094 setIsDisabled(isDisabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095 }
96
97 @Override
Sunny Goyal726bee72018-03-05 12:54:24 -080098 public final void draw(Canvas canvas) {
Matthew Ngeb9cc9d2018-06-25 15:32:24 -070099 if (mScale != 1f) {
Sunny Goyal726bee72018-03-05 12:54:24 -0800100 int count = canvas.save();
101 Rect bounds = getBounds();
102 canvas.scale(mScale, mScale, bounds.exactCenterX(), bounds.exactCenterY());
103 drawInternal(canvas, bounds);
104 canvas.restoreToCount(count);
105 } else {
106 drawInternal(canvas, getBounds());
107 }
108 }
109
110 protected void drawInternal(Canvas canvas, Rect bounds) {
111 canvas.drawBitmap(mBitmap, null, bounds, mPaint);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112 }
113
114 @Override
Adam Cohenbadf71e2011-05-26 19:08:29 -0700115 public void setColorFilter(ColorFilter cf) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700116 // No op
Adam Cohenbadf71e2011-05-26 19:08:29 -0700117 }
118
119 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120 public int getOpacity() {
121 return PixelFormat.TRANSLUCENT;
122 }
123
124 @Override
125 public void setAlpha(int alpha) {
Jon Mirandadff0de42019-08-30 18:42:01 -0700126 if (mAlpha != alpha) {
127 mAlpha = alpha;
128 mPaint.setAlpha(alpha);
129 invalidateSelf();
130 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131 }
132
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700133 @Override
Adam Cohen76fc0852011-06-17 13:26:23 -0700134 public void setFilterBitmap(boolean filterBitmap) {
135 mPaint.setFilterBitmap(filterBitmap);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700136 mPaint.setAntiAlias(filterBitmap);
Adam Cohen76fc0852011-06-17 13:26:23 -0700137 }
138
Winson Chung29d6fea2010-12-01 15:47:31 -0800139 public int getAlpha() {
140 return mAlpha;
141 }
142
Matthew Ngeb9cc9d2018-06-25 15:32:24 -0700143 public void setScale(float scale) {
144 if (mScaleAnimation != null) {
145 mScaleAnimation.cancel();
146 mScaleAnimation = null;
147 }
148 mScale = scale;
149 invalidateSelf();
150 }
151
Sunny Goyal726bee72018-03-05 12:54:24 -0800152 public float getAnimatedScale() {
153 return mScaleAnimation == null ? 1 : mScale;
154 }
155
Matthew Ngeb9cc9d2018-06-25 15:32:24 -0700156 public float getScale() {
157 return mScale;
158 }
159
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 public int getIntrinsicWidth() {
Sunny Goyalc424f222014-09-05 07:04:59 -0700162 return mBitmap.getWidth();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 }
164
165 @Override
166 public int getIntrinsicHeight() {
Sunny Goyalc424f222014-09-05 07:04:59 -0700167 return mBitmap.getHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 }
169
170 @Override
171 public int getMinimumWidth() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800172 return getBounds().width();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 }
174
175 @Override
176 public int getMinimumHeight() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800177 return getBounds().height();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800178 }
179
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 Goyal726bee72018-03-05 12:54:24 -0800202 if (mScaleAnimation != null) {
203 mScaleAnimation.cancel();
204 mScaleAnimation = null;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800205 }
Winsonc0880492015-08-21 11:16:27 -0700206
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800207 if (mIsPressed) {
208 // Animate when going to pressed state
Sunny Goyal726bee72018-03-05 12:54:24 -0800209 mScaleAnimation = ObjectAnimator.ofFloat(this, SCALE, PRESSED_SCALE);
210 mScaleAnimation.setDuration(CLICK_FEEDBACK_DURATION);
211 mScaleAnimation.setInterpolator(ACCEL);
212 mScaleAnimation.start();
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800213 } else {
Hyunyoung Songef468d82019-01-03 01:02:43 -0800214 if (isVisible()) {
215 mScaleAnimation = ObjectAnimator.ofFloat(this, SCALE, 1f);
216 mScaleAnimation.setDuration(CLICK_FEEDBACK_DURATION);
217 mScaleAnimation.setInterpolator(DEACCEL);
218 mScaleAnimation.start();
219 } else {
220 mScale = 1f;
221 invalidateSelf();
222 }
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800223 }
Winsonc0880492015-08-21 11:16:27 -0700224 return true;
225 }
226 return false;
227 }
228
Tony Wickham6b910a22016-11-08 10:40:34 -0800229 public void setIsDisabled(boolean isDisabled) {
230 if (mIsDisabled != isDisabled) {
231 mIsDisabled = isDisabled;
Samuel Fufa61d39642020-01-23 12:42:06 -0800232 updateFilter();
Tony Wickham6b910a22016-11-08 10:40:34 -0800233 }
234 }
235
Jon Mirandab6d686d2019-03-29 10:49:43 -0700236 protected boolean isDisabled() {
237 return mIsDisabled;
238 }
239
Samuel Fufa61d39642020-01-23 12:42:06 -0800240 private ColorFilter getDisabledColorFilter() {
241 if (sDisabledFColorFilter == null) {
242 ColorMatrix tempBrightnessMatrix = new ColorMatrix();
243 ColorMatrix tempFilterMatrix = new ColorMatrix();
244
245 tempFilterMatrix.setSaturation(1f - DISABLED_DESATURATION);
246 float scale = 1 - DISABLED_BRIGHTNESS;
247 int brightnessI = (int) (255 * DISABLED_BRIGHTNESS);
248 float[] mat = tempBrightnessMatrix.getArray();
249 mat[0] = scale;
250 mat[6] = scale;
251 mat[12] = scale;
252 mat[4] = brightnessI;
253 mat[9] = brightnessI;
254 mat[14] = brightnessI;
255 mat[18] = DISABLED_ALPHA;
256 tempFilterMatrix.preConcat(tempBrightnessMatrix);
257 sDisabledFColorFilter = new ColorMatrixColorFilter(tempFilterMatrix);
Sunny Goyal95abbb32014-08-04 10:53:22 -0700258 }
Samuel Fufa61d39642020-01-23 12:42:06 -0800259 return sDisabledFColorFilter;
Winsonc0880492015-08-21 11:16:27 -0700260 }
261
262 /**
263 * Updates the paint to reflect the current brightness and saturation.
264 */
Sunny Goyal0ffab442018-06-07 17:31:48 -0700265 protected void updateFilter() {
Samuel Fufa61d39642020-01-23 12:42:06 -0800266 mPaint.setColorFilter(mIsDisabled ? getDisabledColorFilter() : null);
Winsonc0880492015-08-21 11:16:27 -0700267 invalidateSelf();
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700268 }
Sunny Goyal338d15d2018-02-23 12:19:44 -0800269
270 @Override
271 public ConstantState getConstantState() {
Jon Mirandab6d686d2019-03-29 10:49:43 -0700272 return new MyConstantState(mBitmap, mIconColor, mIsDisabled);
Sunny Goyal338d15d2018-02-23 12:19:44 -0800273 }
274
Sunny Goyal61e08462018-03-02 17:25:59 -0800275 protected static class MyConstantState extends ConstantState {
276 protected final Bitmap mBitmap;
277 protected final int mIconColor;
Jon Mirandab6d686d2019-03-29 10:49:43 -0700278 protected final boolean mIsDisabled;
Sunny Goyal338d15d2018-02-23 12:19:44 -0800279
Jon Mirandab6d686d2019-03-29 10:49:43 -0700280 public MyConstantState(Bitmap bitmap, int color, boolean isDisabled) {
Sunny Goyal338d15d2018-02-23 12:19:44 -0800281 mBitmap = bitmap;
282 mIconColor = color;
Jon Mirandab6d686d2019-03-29 10:49:43 -0700283 mIsDisabled = isDisabled;
Sunny Goyal338d15d2018-02-23 12:19:44 -0800284 }
285
286 @Override
Sunny Goyal14168432019-10-24 15:59:49 -0700287 public FastBitmapDrawable newDrawable() {
Jon Mirandab6d686d2019-03-29 10:49:43 -0700288 return new FastBitmapDrawable(mBitmap, mIconColor, mIsDisabled);
Sunny Goyal338d15d2018-02-23 12:19:44 -0800289 }
290
291 @Override
292 public int getChangingConfigurations() {
293 return 0;
294 }
295 }
Sunny Goyal14168432019-10-24 15:59:49 -0700296
297 /**
298 * Interface to be implemented by custom {@link BitmapInfo} to handle drawable construction
299 */
300 public interface Factory {
301
302 /**
303 * Called to create a new drawable
304 */
305 FastBitmapDrawable newDrawable();
306 }
307
308 /**
309 * Returns a FastBitmapDrawable with the icon.
310 */
311 public static FastBitmapDrawable newIcon(Context context, ItemInfoWithIcon info) {
312 FastBitmapDrawable drawable = newIcon(context, info.bitmap);
313 drawable.setIsDisabled(info.isDisabled());
314 return drawable;
315 }
316
317 /**
318 * Creates a drawable for the provided BitmapInfo
319 */
320 public static FastBitmapDrawable newIcon(Context context, BitmapInfo info) {
321 if (info instanceof Factory) {
322 return ((Factory) info).newDrawable();
323 } else if (info.isLowRes()) {
324 return new PlaceHolderIconDrawable(info, context);
325 } else {
326 return new FastBitmapDrawable(info);
327 }
328 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800329}