blob: f96aafa6b89070571f30d4ebd0737af61ccd55f9 [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;
Sunny Goyale396abf2020-04-06 15:11:17 -070038import com.android.launcher3.model.data.ItemInfoWithIcon;
Tony Wickham9a8d11f2017-01-11 09:53:12 -080039
Samuel Fufa61d39642020-01-23 12:42:06 -080040
Hyunyoung Song3f471442015-04-08 19:01:34 -070041public class FastBitmapDrawable extends Drawable {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080042
Sunny Goyal726bee72018-03-05 12:54:24 -080043 private static final float PRESSED_SCALE = 1.1f;
44
Tony Wickham6b910a22016-11-08 10:40:34 -080045 private static final float DISABLED_DESATURATION = 1f;
46 private static final float DISABLED_BRIGHTNESS = 0.5f;
Samuel Fufa61d39642020-01-23 12:42:06 -080047 private static final float DISABLED_ALPHA = 0.54f;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070048
Sunny Goyal726bee72018-03-05 12:54:24 -080049 public static final int CLICK_FEEDBACK_DURATION = 200;
Sunny Goyal508da152014-08-14 10:53:27 -070050
Samuel Fufa61d39642020-01-23 12:42:06 -080051 private static ColorFilter sDisabledFColorFilter;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070052
Sunny Goyal55cb70b2016-11-12 09:58:29 -080053 protected final Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG);
Sunny Goyal61e08462018-03-02 17:25:59 -080054 protected Bitmap mBitmap;
Sunny Goyal179249d2017-12-19 16:49:24 -080055 protected final int mIconColor;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080056
57 private boolean mIsPressed;
Tony Wickham6b910a22016-11-08 10:40:34 -080058 private boolean mIsDisabled;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070059
Sunny Goyal726bee72018-03-05 12:54:24 -080060 // Animator and properties for the fast bitmap drawable's scale
61 private static final Property<FastBitmapDrawable, Float> SCALE
62 = new Property<FastBitmapDrawable, Float>(Float.TYPE, "scale") {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080063 @Override
64 public Float get(FastBitmapDrawable fastBitmapDrawable) {
Sunny Goyal726bee72018-03-05 12:54:24 -080065 return fastBitmapDrawable.mScale;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080066 }
67
68 @Override
69 public void set(FastBitmapDrawable fastBitmapDrawable, Float value) {
Sunny Goyal726bee72018-03-05 12:54:24 -080070 fastBitmapDrawable.mScale = value;
71 fastBitmapDrawable.invalidateSelf();
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080072 }
73 };
Sunny Goyal726bee72018-03-05 12:54:24 -080074 private ObjectAnimator mScaleAnimation;
75 private float mScale = 1;
76
Winsonc0880492015-08-21 11:16:27 -070077 private int mAlpha = 255;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078
Hyunyoung Song3f471442015-04-08 19:01:34 -070079 public FastBitmapDrawable(Bitmap b) {
Sunny Goyal179249d2017-12-19 16:49:24 -080080 this(b, Color.TRANSPARENT);
81 }
82
83 public FastBitmapDrawable(BitmapInfo info) {
84 this(info.icon, info.color);
85 }
86
Sunny Goyal179249d2017-12-19 16:49:24 -080087 protected FastBitmapDrawable(Bitmap b, int iconColor) {
Jon Mirandab6d686d2019-03-29 10:49:43 -070088 this(b, iconColor, false);
89 }
90
91 protected FastBitmapDrawable(Bitmap b, int iconColor, boolean isDisabled) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092 mBitmap = b;
Sunny Goyal179249d2017-12-19 16:49:24 -080093 mIconColor = iconColor;
Sunny Goyal96ac68a2017-02-02 16:37:21 -080094 setFilterBitmap(true);
Jon Mirandab6d686d2019-03-29 10:49:43 -070095 setIsDisabled(isDisabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096 }
97
98 @Override
Sunny Goyal726bee72018-03-05 12:54:24 -080099 public final void draw(Canvas canvas) {
Matthew Ngeb9cc9d2018-06-25 15:32:24 -0700100 if (mScale != 1f) {
Sunny Goyal726bee72018-03-05 12:54:24 -0800101 int count = canvas.save();
102 Rect bounds = getBounds();
103 canvas.scale(mScale, mScale, bounds.exactCenterX(), bounds.exactCenterY());
104 drawInternal(canvas, bounds);
105 canvas.restoreToCount(count);
106 } else {
107 drawInternal(canvas, getBounds());
108 }
109 }
110
111 protected void drawInternal(Canvas canvas, Rect bounds) {
112 canvas.drawBitmap(mBitmap, null, bounds, mPaint);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113 }
114
115 @Override
Adam Cohenbadf71e2011-05-26 19:08:29 -0700116 public void setColorFilter(ColorFilter cf) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700117 // No op
Adam Cohenbadf71e2011-05-26 19:08:29 -0700118 }
119
120 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 public int getOpacity() {
122 return PixelFormat.TRANSLUCENT;
123 }
124
125 @Override
126 public void setAlpha(int alpha) {
Jon Mirandadff0de42019-08-30 18:42:01 -0700127 if (mAlpha != alpha) {
128 mAlpha = alpha;
129 mPaint.setAlpha(alpha);
130 invalidateSelf();
131 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 }
133
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700134 @Override
Adam Cohen76fc0852011-06-17 13:26:23 -0700135 public void setFilterBitmap(boolean filterBitmap) {
136 mPaint.setFilterBitmap(filterBitmap);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700137 mPaint.setAntiAlias(filterBitmap);
Adam Cohen76fc0852011-06-17 13:26:23 -0700138 }
139
Winson Chung29d6fea2010-12-01 15:47:31 -0800140 public int getAlpha() {
141 return mAlpha;
142 }
143
Matthew Ngeb9cc9d2018-06-25 15:32:24 -0700144 public void setScale(float scale) {
145 if (mScaleAnimation != null) {
146 mScaleAnimation.cancel();
147 mScaleAnimation = null;
148 }
149 mScale = scale;
150 invalidateSelf();
151 }
152
Sunny Goyal726bee72018-03-05 12:54:24 -0800153 public float getAnimatedScale() {
154 return mScaleAnimation == null ? 1 : mScale;
155 }
156
Matthew Ngeb9cc9d2018-06-25 15:32:24 -0700157 public float getScale() {
158 return mScale;
159 }
160
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 public int getIntrinsicWidth() {
Sunny Goyalc424f222014-09-05 07:04:59 -0700163 return mBitmap.getWidth();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 }
165
166 @Override
167 public int getIntrinsicHeight() {
Sunny Goyalc424f222014-09-05 07:04:59 -0700168 return mBitmap.getHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 }
170
171 @Override
172 public int getMinimumWidth() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800173 return getBounds().width();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 }
175
176 @Override
177 public int getMinimumHeight() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800178 return getBounds().height();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800179 }
180
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800181 @Override
182 public boolean isStateful() {
183 return true;
Winsonc0880492015-08-21 11:16:27 -0700184 }
185
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800186 @Override
Sunny Goyal28141122017-06-21 17:28:23 -0700187 public ColorFilter getColorFilter() {
188 return mPaint.getColorFilter();
189 }
190
191 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800192 protected boolean onStateChange(int[] state) {
193 boolean isPressed = false;
194 for (int s : state) {
195 if (s == android.R.attr.state_pressed) {
196 isPressed = true;
197 break;
198 }
199 }
200 if (mIsPressed != isPressed) {
201 mIsPressed = isPressed;
Winsonc0880492015-08-21 11:16:27 -0700202
Sunny Goyal726bee72018-03-05 12:54:24 -0800203 if (mScaleAnimation != null) {
204 mScaleAnimation.cancel();
205 mScaleAnimation = null;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800206 }
Winsonc0880492015-08-21 11:16:27 -0700207
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800208 if (mIsPressed) {
209 // Animate when going to pressed state
Sunny Goyal726bee72018-03-05 12:54:24 -0800210 mScaleAnimation = ObjectAnimator.ofFloat(this, SCALE, PRESSED_SCALE);
211 mScaleAnimation.setDuration(CLICK_FEEDBACK_DURATION);
212 mScaleAnimation.setInterpolator(ACCEL);
213 mScaleAnimation.start();
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800214 } else {
Hyunyoung Songef468d82019-01-03 01:02:43 -0800215 if (isVisible()) {
216 mScaleAnimation = ObjectAnimator.ofFloat(this, SCALE, 1f);
217 mScaleAnimation.setDuration(CLICK_FEEDBACK_DURATION);
218 mScaleAnimation.setInterpolator(DEACCEL);
219 mScaleAnimation.start();
220 } else {
221 mScale = 1f;
222 invalidateSelf();
223 }
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800224 }
Winsonc0880492015-08-21 11:16:27 -0700225 return true;
226 }
227 return false;
228 }
229
Tony Wickham6b910a22016-11-08 10:40:34 -0800230 public void setIsDisabled(boolean isDisabled) {
231 if (mIsDisabled != isDisabled) {
232 mIsDisabled = isDisabled;
Samuel Fufa61d39642020-01-23 12:42:06 -0800233 updateFilter();
Tony Wickham6b910a22016-11-08 10:40:34 -0800234 }
235 }
236
Jon Mirandab6d686d2019-03-29 10:49:43 -0700237 protected boolean isDisabled() {
238 return mIsDisabled;
239 }
240
Samuel Fufa61d39642020-01-23 12:42:06 -0800241 private ColorFilter getDisabledColorFilter() {
242 if (sDisabledFColorFilter == null) {
243 ColorMatrix tempBrightnessMatrix = new ColorMatrix();
244 ColorMatrix tempFilterMatrix = new ColorMatrix();
245
246 tempFilterMatrix.setSaturation(1f - DISABLED_DESATURATION);
247 float scale = 1 - DISABLED_BRIGHTNESS;
248 int brightnessI = (int) (255 * DISABLED_BRIGHTNESS);
249 float[] mat = tempBrightnessMatrix.getArray();
250 mat[0] = scale;
251 mat[6] = scale;
252 mat[12] = scale;
253 mat[4] = brightnessI;
254 mat[9] = brightnessI;
255 mat[14] = brightnessI;
256 mat[18] = DISABLED_ALPHA;
257 tempFilterMatrix.preConcat(tempBrightnessMatrix);
258 sDisabledFColorFilter = new ColorMatrixColorFilter(tempFilterMatrix);
Sunny Goyal95abbb32014-08-04 10:53:22 -0700259 }
Samuel Fufa61d39642020-01-23 12:42:06 -0800260 return sDisabledFColorFilter;
Winsonc0880492015-08-21 11:16:27 -0700261 }
262
263 /**
264 * Updates the paint to reflect the current brightness and saturation.
265 */
Sunny Goyal0ffab442018-06-07 17:31:48 -0700266 protected void updateFilter() {
Samuel Fufa61d39642020-01-23 12:42:06 -0800267 mPaint.setColorFilter(mIsDisabled ? getDisabledColorFilter() : null);
Winsonc0880492015-08-21 11:16:27 -0700268 invalidateSelf();
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700269 }
Sunny Goyal338d15d2018-02-23 12:19:44 -0800270
271 @Override
272 public ConstantState getConstantState() {
Jon Mirandab6d686d2019-03-29 10:49:43 -0700273 return new MyConstantState(mBitmap, mIconColor, mIsDisabled);
Sunny Goyal338d15d2018-02-23 12:19:44 -0800274 }
275
Sunny Goyal61e08462018-03-02 17:25:59 -0800276 protected static class MyConstantState extends ConstantState {
277 protected final Bitmap mBitmap;
278 protected final int mIconColor;
Jon Mirandab6d686d2019-03-29 10:49:43 -0700279 protected final boolean mIsDisabled;
Sunny Goyal338d15d2018-02-23 12:19:44 -0800280
Jon Mirandab6d686d2019-03-29 10:49:43 -0700281 public MyConstantState(Bitmap bitmap, int color, boolean isDisabled) {
Sunny Goyal338d15d2018-02-23 12:19:44 -0800282 mBitmap = bitmap;
283 mIconColor = color;
Jon Mirandab6d686d2019-03-29 10:49:43 -0700284 mIsDisabled = isDisabled;
Sunny Goyal338d15d2018-02-23 12:19:44 -0800285 }
286
287 @Override
Sunny Goyal14168432019-10-24 15:59:49 -0700288 public FastBitmapDrawable newDrawable() {
Jon Mirandab6d686d2019-03-29 10:49:43 -0700289 return new FastBitmapDrawable(mBitmap, mIconColor, mIsDisabled);
Sunny Goyal338d15d2018-02-23 12:19:44 -0800290 }
291
292 @Override
293 public int getChangingConfigurations() {
294 return 0;
295 }
296 }
Sunny Goyal14168432019-10-24 15:59:49 -0700297
298 /**
299 * Interface to be implemented by custom {@link BitmapInfo} to handle drawable construction
300 */
301 public interface Factory {
302
303 /**
304 * Called to create a new drawable
305 */
306 FastBitmapDrawable newDrawable();
307 }
308
309 /**
310 * Returns a FastBitmapDrawable with the icon.
311 */
312 public static FastBitmapDrawable newIcon(Context context, ItemInfoWithIcon info) {
313 FastBitmapDrawable drawable = newIcon(context, info.bitmap);
314 drawable.setIsDisabled(info.isDisabled());
315 return drawable;
316 }
317
318 /**
319 * Creates a drawable for the provided BitmapInfo
320 */
321 public static FastBitmapDrawable newIcon(Context context, BitmapInfo info) {
322 if (info instanceof Factory) {
323 return ((Factory) info).newDrawable();
324 } else if (info.isLowRes()) {
325 return new PlaceHolderIconDrawable(info, context);
326 } else {
327 return new FastBitmapDrawable(info);
328 }
329 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800330}