blob: 0027a50d958b28a0b4e998fa341e315788c5eabf [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;
Pierre Barbier de Reuilleadc23262021-03-25 11:01:34 +000031import android.graphics.Path;
Winson Chung45e1d6e2010-11-09 17:19:49 -080032import android.graphics.PixelFormat;
Sunny Goyal726bee72018-03-05 12:54:24 -080033import android.graphics.Rect;
Winson Chung45e1d6e2010-11-09 17:19:49 -080034import android.graphics.drawable.Drawable;
Tony Wickham1e618492017-02-02 12:57:18 -080035import android.util.Property;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036
Winson Chungf9935182020-10-23 09:26:44 -070037import androidx.annotation.Nullable;
38
Sunny Goyal14168432019-10-24 15:59:49 -070039import com.android.launcher3.graphics.PlaceHolderIconDrawable;
Hyunyoung Song48cb7bc2018-09-25 17:03:34 -070040import com.android.launcher3.icons.BitmapInfo;
Sunny Goyale396abf2020-04-06 15:11:17 -070041import com.android.launcher3.model.data.ItemInfoWithIcon;
Samuel Fufa61bc63a2020-06-05 12:17:16 -070042import com.android.launcher3.util.Themes;
Tony Wickham9a8d11f2017-01-11 09:53:12 -080043
Samuel Fufa61d39642020-01-23 12:42:06 -080044
Hyunyoung Song3f471442015-04-08 19:01:34 -070045public class FastBitmapDrawable extends Drawable {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080046
Sunny Goyal726bee72018-03-05 12:54:24 -080047 private static final float PRESSED_SCALE = 1.1f;
48
Tony Wickham6b910a22016-11-08 10:40:34 -080049 private static final float DISABLED_DESATURATION = 1f;
50 private static final float DISABLED_BRIGHTNESS = 0.5f;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070051
Sunny Goyal726bee72018-03-05 12:54:24 -080052 public static final int CLICK_FEEDBACK_DURATION = 200;
Sunny Goyal508da152014-08-14 10:53:27 -070053
Samuel Fufa61d39642020-01-23 12:42:06 -080054 private static ColorFilter sDisabledFColorFilter;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070055
Sunny Goyal55cb70b2016-11-12 09:58:29 -080056 protected final Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG);
Sunny Goyal61e08462018-03-02 17:25:59 -080057 protected Bitmap mBitmap;
Sunny Goyal179249d2017-12-19 16:49:24 -080058 protected final int mIconColor;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080059
Winson Chungf9935182020-10-23 09:26:44 -070060 @Nullable private ColorFilter mColorFilter;
61
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080062 private boolean mIsPressed;
Tony Wickham6b910a22016-11-08 10:40:34 -080063 private boolean mIsDisabled;
Samuel Fufa61bc63a2020-06-05 12:17:16 -070064 private float mDisabledAlpha = 1f;
Pierre Barbier de Reuilleadc23262021-03-25 11:01:34 +000065 private float mRoundedCornersRadius = 0f;
66 private final Path mClipPath = new Path();
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070067
Sunny Goyal726bee72018-03-05 12:54:24 -080068 // Animator and properties for the fast bitmap drawable's scale
69 private static final Property<FastBitmapDrawable, Float> SCALE
70 = new Property<FastBitmapDrawable, Float>(Float.TYPE, "scale") {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080071 @Override
72 public Float get(FastBitmapDrawable fastBitmapDrawable) {
Sunny Goyal726bee72018-03-05 12:54:24 -080073 return fastBitmapDrawable.mScale;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080074 }
75
76 @Override
77 public void set(FastBitmapDrawable fastBitmapDrawable, Float value) {
Sunny Goyal726bee72018-03-05 12:54:24 -080078 fastBitmapDrawable.mScale = value;
79 fastBitmapDrawable.invalidateSelf();
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080080 }
81 };
Sunny Goyal726bee72018-03-05 12:54:24 -080082 private ObjectAnimator mScaleAnimation;
83 private float mScale = 1;
84
Winsonc0880492015-08-21 11:16:27 -070085 private int mAlpha = 255;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086
Hyunyoung Song3f471442015-04-08 19:01:34 -070087 public FastBitmapDrawable(Bitmap b) {
Sunny Goyal179249d2017-12-19 16:49:24 -080088 this(b, Color.TRANSPARENT);
89 }
90
91 public FastBitmapDrawable(BitmapInfo info) {
92 this(info.icon, info.color);
93 }
94
Sunny Goyal179249d2017-12-19 16:49:24 -080095 protected FastBitmapDrawable(Bitmap b, int iconColor) {
Jon Mirandab6d686d2019-03-29 10:49:43 -070096 this(b, iconColor, false);
97 }
98
99 protected FastBitmapDrawable(Bitmap b, int iconColor, boolean isDisabled) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800100 mBitmap = b;
Sunny Goyal179249d2017-12-19 16:49:24 -0800101 mIconColor = iconColor;
Sunny Goyal96ac68a2017-02-02 16:37:21 -0800102 setFilterBitmap(true);
Jon Mirandab6d686d2019-03-29 10:49:43 -0700103 setIsDisabled(isDisabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800104 }
105
106 @Override
Sunny Goyal726bee72018-03-05 12:54:24 -0800107 public final void draw(Canvas canvas) {
Pierre Barbier de Reuilleadc23262021-03-25 11:01:34 +0000108 if (mRoundedCornersRadius > 0) {
109 float radius = mRoundedCornersRadius * mScale;
110 mClipPath.reset();
111 mClipPath.addRoundRect(0, 0, getIntrinsicWidth(), getIntrinsicHeight(),
112 radius, radius, Path.Direction.CCW);
113 canvas.clipPath(mClipPath);
114 }
Matthew Ngeb9cc9d2018-06-25 15:32:24 -0700115 if (mScale != 1f) {
Sunny Goyal726bee72018-03-05 12:54:24 -0800116 int count = canvas.save();
117 Rect bounds = getBounds();
118 canvas.scale(mScale, mScale, bounds.exactCenterX(), bounds.exactCenterY());
119 drawInternal(canvas, bounds);
120 canvas.restoreToCount(count);
121 } else {
122 drawInternal(canvas, getBounds());
123 }
124 }
125
126 protected void drawInternal(Canvas canvas, Rect bounds) {
127 canvas.drawBitmap(mBitmap, null, bounds, mPaint);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128 }
129
130 @Override
Adam Cohenbadf71e2011-05-26 19:08:29 -0700131 public void setColorFilter(ColorFilter cf) {
Winson Chungf9935182020-10-23 09:26:44 -0700132 mColorFilter = cf;
133 updateFilter();
Adam Cohenbadf71e2011-05-26 19:08:29 -0700134 }
135
136 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 public int getOpacity() {
138 return PixelFormat.TRANSLUCENT;
139 }
140
141 @Override
142 public void setAlpha(int alpha) {
Jon Mirandadff0de42019-08-30 18:42:01 -0700143 if (mAlpha != alpha) {
144 mAlpha = alpha;
145 mPaint.setAlpha(alpha);
146 invalidateSelf();
147 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148 }
149
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700150 @Override
Adam Cohen76fc0852011-06-17 13:26:23 -0700151 public void setFilterBitmap(boolean filterBitmap) {
152 mPaint.setFilterBitmap(filterBitmap);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700153 mPaint.setAntiAlias(filterBitmap);
Adam Cohen76fc0852011-06-17 13:26:23 -0700154 }
155
Winson Chung29d6fea2010-12-01 15:47:31 -0800156 public int getAlpha() {
157 return mAlpha;
158 }
159
Matthew Ngeb9cc9d2018-06-25 15:32:24 -0700160 public void setScale(float scale) {
161 if (mScaleAnimation != null) {
162 mScaleAnimation.cancel();
163 mScaleAnimation = null;
164 }
165 mScale = scale;
166 invalidateSelf();
167 }
168
Sunny Goyal726bee72018-03-05 12:54:24 -0800169 public float getAnimatedScale() {
170 return mScaleAnimation == null ? 1 : mScale;
171 }
172
Matthew Ngeb9cc9d2018-06-25 15:32:24 -0700173 public float getScale() {
174 return mScale;
175 }
176
Pierre Barbier de Reuilleadc23262021-03-25 11:01:34 +0000177 public void setRoundedCornersRadius(float radius) {
178 mRoundedCornersRadius = radius;
179 }
180
181 public float getRoundedCornersRadius() {
182 return mRoundedCornersRadius;
183 }
184
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 public int getIntrinsicWidth() {
Sunny Goyalc424f222014-09-05 07:04:59 -0700187 return mBitmap.getWidth();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 }
189
190 @Override
191 public int getIntrinsicHeight() {
Sunny Goyalc424f222014-09-05 07:04:59 -0700192 return mBitmap.getHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193 }
194
195 @Override
196 public int getMinimumWidth() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800197 return getBounds().width();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198 }
199
200 @Override
201 public int getMinimumHeight() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800202 return getBounds().height();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800203 }
204
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800205 @Override
206 public boolean isStateful() {
207 return true;
Winsonc0880492015-08-21 11:16:27 -0700208 }
209
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800210 @Override
Sunny Goyal28141122017-06-21 17:28:23 -0700211 public ColorFilter getColorFilter() {
212 return mPaint.getColorFilter();
213 }
214
215 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800216 protected boolean onStateChange(int[] state) {
217 boolean isPressed = false;
218 for (int s : state) {
219 if (s == android.R.attr.state_pressed) {
220 isPressed = true;
221 break;
222 }
223 }
224 if (mIsPressed != isPressed) {
225 mIsPressed = isPressed;
Winsonc0880492015-08-21 11:16:27 -0700226
Sunny Goyal726bee72018-03-05 12:54:24 -0800227 if (mScaleAnimation != null) {
228 mScaleAnimation.cancel();
229 mScaleAnimation = null;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800230 }
Winsonc0880492015-08-21 11:16:27 -0700231
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800232 if (mIsPressed) {
233 // Animate when going to pressed state
Sunny Goyal726bee72018-03-05 12:54:24 -0800234 mScaleAnimation = ObjectAnimator.ofFloat(this, SCALE, PRESSED_SCALE);
235 mScaleAnimation.setDuration(CLICK_FEEDBACK_DURATION);
236 mScaleAnimation.setInterpolator(ACCEL);
237 mScaleAnimation.start();
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800238 } else {
Hyunyoung Songef468d82019-01-03 01:02:43 -0800239 if (isVisible()) {
240 mScaleAnimation = ObjectAnimator.ofFloat(this, SCALE, 1f);
241 mScaleAnimation.setDuration(CLICK_FEEDBACK_DURATION);
242 mScaleAnimation.setInterpolator(DEACCEL);
243 mScaleAnimation.start();
244 } else {
245 mScale = 1f;
246 invalidateSelf();
247 }
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800248 }
Winsonc0880492015-08-21 11:16:27 -0700249 return true;
250 }
251 return false;
252 }
253
Tony Wickham6b910a22016-11-08 10:40:34 -0800254 public void setIsDisabled(boolean isDisabled) {
255 if (mIsDisabled != isDisabled) {
256 mIsDisabled = isDisabled;
Samuel Fufa61d39642020-01-23 12:42:06 -0800257 updateFilter();
Tony Wickham6b910a22016-11-08 10:40:34 -0800258 }
259 }
260
Jon Mirandab6d686d2019-03-29 10:49:43 -0700261 protected boolean isDisabled() {
262 return mIsDisabled;
263 }
264
Samuel Fufa61d39642020-01-23 12:42:06 -0800265 private ColorFilter getDisabledColorFilter() {
266 if (sDisabledFColorFilter == null) {
267 ColorMatrix tempBrightnessMatrix = new ColorMatrix();
268 ColorMatrix tempFilterMatrix = new ColorMatrix();
269
270 tempFilterMatrix.setSaturation(1f - DISABLED_DESATURATION);
271 float scale = 1 - DISABLED_BRIGHTNESS;
272 int brightnessI = (int) (255 * DISABLED_BRIGHTNESS);
273 float[] mat = tempBrightnessMatrix.getArray();
274 mat[0] = scale;
275 mat[6] = scale;
276 mat[12] = scale;
277 mat[4] = brightnessI;
278 mat[9] = brightnessI;
279 mat[14] = brightnessI;
Samuel Fufa61bc63a2020-06-05 12:17:16 -0700280 mat[18] = mDisabledAlpha;
Samuel Fufa61d39642020-01-23 12:42:06 -0800281 tempFilterMatrix.preConcat(tempBrightnessMatrix);
282 sDisabledFColorFilter = new ColorMatrixColorFilter(tempFilterMatrix);
Sunny Goyal95abbb32014-08-04 10:53:22 -0700283 }
Samuel Fufa61d39642020-01-23 12:42:06 -0800284 return sDisabledFColorFilter;
Winsonc0880492015-08-21 11:16:27 -0700285 }
286
287 /**
288 * Updates the paint to reflect the current brightness and saturation.
289 */
Sunny Goyal0ffab442018-06-07 17:31:48 -0700290 protected void updateFilter() {
Winson Chungf9935182020-10-23 09:26:44 -0700291 mPaint.setColorFilter(mIsDisabled ? getDisabledColorFilter() : mColorFilter);
Winsonc0880492015-08-21 11:16:27 -0700292 invalidateSelf();
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700293 }
Sunny Goyal338d15d2018-02-23 12:19:44 -0800294
295 @Override
296 public ConstantState getConstantState() {
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800297 return new FastBitmapConstantState(mBitmap, mIconColor, mIsDisabled);
Sunny Goyal338d15d2018-02-23 12:19:44 -0800298 }
299
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800300 protected static class FastBitmapConstantState extends ConstantState {
Sunny Goyal61e08462018-03-02 17:25:59 -0800301 protected final Bitmap mBitmap;
302 protected final int mIconColor;
Jon Mirandab6d686d2019-03-29 10:49:43 -0700303 protected final boolean mIsDisabled;
Sunny Goyal338d15d2018-02-23 12:19:44 -0800304
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800305 public FastBitmapConstantState(Bitmap bitmap, int color, boolean isDisabled) {
Sunny Goyal338d15d2018-02-23 12:19:44 -0800306 mBitmap = bitmap;
307 mIconColor = color;
Jon Mirandab6d686d2019-03-29 10:49:43 -0700308 mIsDisabled = isDisabled;
Sunny Goyal338d15d2018-02-23 12:19:44 -0800309 }
310
311 @Override
Sunny Goyal14168432019-10-24 15:59:49 -0700312 public FastBitmapDrawable newDrawable() {
Jon Mirandab6d686d2019-03-29 10:49:43 -0700313 return new FastBitmapDrawable(mBitmap, mIconColor, mIsDisabled);
Sunny Goyal338d15d2018-02-23 12:19:44 -0800314 }
315
316 @Override
317 public int getChangingConfigurations() {
318 return 0;
319 }
320 }
Sunny Goyal14168432019-10-24 15:59:49 -0700321
322 /**
323 * Interface to be implemented by custom {@link BitmapInfo} to handle drawable construction
324 */
325 public interface Factory {
326
327 /**
328 * Called to create a new drawable
329 */
330 FastBitmapDrawable newDrawable();
331 }
332
333 /**
334 * Returns a FastBitmapDrawable with the icon.
335 */
336 public static FastBitmapDrawable newIcon(Context context, ItemInfoWithIcon info) {
337 FastBitmapDrawable drawable = newIcon(context, info.bitmap);
338 drawable.setIsDisabled(info.isDisabled());
339 return drawable;
340 }
341
342 /**
343 * Creates a drawable for the provided BitmapInfo
344 */
345 public static FastBitmapDrawable newIcon(Context context, BitmapInfo info) {
Samuel Fufa61bc63a2020-06-05 12:17:16 -0700346 final FastBitmapDrawable drawable;
Sunny Goyal14168432019-10-24 15:59:49 -0700347 if (info instanceof Factory) {
Samuel Fufa61bc63a2020-06-05 12:17:16 -0700348 drawable = ((Factory) info).newDrawable();
Sunny Goyal14168432019-10-24 15:59:49 -0700349 } else if (info.isLowRes()) {
Samuel Fufa61bc63a2020-06-05 12:17:16 -0700350 drawable = new PlaceHolderIconDrawable(info, context);
Sunny Goyal14168432019-10-24 15:59:49 -0700351 } else {
Samuel Fufa61bc63a2020-06-05 12:17:16 -0700352 drawable = new FastBitmapDrawable(info);
Sunny Goyal14168432019-10-24 15:59:49 -0700353 }
Samuel Fufa61bc63a2020-06-05 12:17:16 -0700354 drawable.mDisabledAlpha = Themes.getFloat(context, R.attr.disabledIconAlpha, 1f);
355 return drawable;
Sunny Goyal14168432019-10-24 15:59:49 -0700356 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357}