The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1 | /* |
| 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 Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 18 | |
Sunny Goyal | 726bee7 | 2018-03-05 12:54:24 -0800 | [diff] [blame] | 19 | import static com.android.launcher3.anim.Interpolators.ACCEL; |
Hyunyoung Song | ef468d8 | 2019-01-03 01:02:43 -0800 | [diff] [blame] | 20 | import static com.android.launcher3.anim.Interpolators.DEACCEL; |
Sunny Goyal | 726bee7 | 2018-03-05 12:54:24 -0800 | [diff] [blame] | 21 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 22 | import android.animation.ObjectAnimator; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 23 | import android.graphics.Bitmap; |
| 24 | import android.graphics.Canvas; |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 25 | import android.graphics.Color; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 26 | import android.graphics.ColorFilter; |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 27 | import android.graphics.ColorMatrix; |
| 28 | import android.graphics.ColorMatrixColorFilter; |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 29 | import android.graphics.Paint; |
| 30 | import android.graphics.PixelFormat; |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 31 | import android.graphics.PorterDuff; |
| 32 | import android.graphics.PorterDuffColorFilter; |
Sunny Goyal | 726bee7 | 2018-03-05 12:54:24 -0800 | [diff] [blame] | 33 | import android.graphics.Rect; |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 34 | import android.graphics.drawable.Drawable; |
Tony Wickham | 1e61849 | 2017-02-02 12:57:18 -0800 | [diff] [blame] | 35 | import android.util.Property; |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 36 | import android.util.SparseArray; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 37 | |
Hyunyoung Song | 48cb7bc | 2018-09-25 17:03:34 -0700 | [diff] [blame] | 38 | import com.android.launcher3.icons.BitmapInfo; |
Tony Wickham | 9a8d11f | 2017-01-11 09:53:12 -0800 | [diff] [blame] | 39 | |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 40 | public class FastBitmapDrawable extends Drawable { |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 41 | |
Sunny Goyal | 726bee7 | 2018-03-05 12:54:24 -0800 | [diff] [blame] | 42 | private static final float PRESSED_SCALE = 1.1f; |
| 43 | |
Tony Wickham | 6b910a2 | 2016-11-08 10:40:34 -0800 | [diff] [blame] | 44 | private static final float DISABLED_DESATURATION = 1f; |
| 45 | private static final float DISABLED_BRIGHTNESS = 0.5f; |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 46 | |
Sunny Goyal | 726bee7 | 2018-03-05 12:54:24 -0800 | [diff] [blame] | 47 | public static final int CLICK_FEEDBACK_DURATION = 200; |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 48 | |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 49 | // Since we don't need 256^2 values for combinations of both the brightness and saturation, we |
| 50 | // reduce the value space to a smaller value V, which reduces the number of cached |
| 51 | // ColorMatrixColorFilters that we need to keep to V^2 |
| 52 | private static final int REDUCED_FILTER_VALUE_SPACE = 48; |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 53 | |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 54 | // A cache of ColorFilters for optimizing brightness and saturation animations |
| 55 | private static final SparseArray<ColorFilter> sCachedFilter = new SparseArray<>(); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 56 | |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 57 | // Temporary matrices used for calculation |
| 58 | private static final ColorMatrix sTempBrightnessMatrix = new ColorMatrix(); |
| 59 | private static final ColorMatrix sTempFilterMatrix = new ColorMatrix(); |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 60 | |
Sunny Goyal | 55cb70b | 2016-11-12 09:58:29 -0800 | [diff] [blame] | 61 | protected final Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG); |
Sunny Goyal | 61e0846 | 2018-03-02 17:25:59 -0800 | [diff] [blame] | 62 | protected Bitmap mBitmap; |
Sunny Goyal | 179249d | 2017-12-19 16:49:24 -0800 | [diff] [blame] | 63 | protected final int mIconColor; |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 64 | |
| 65 | private boolean mIsPressed; |
Tony Wickham | 6b910a2 | 2016-11-08 10:40:34 -0800 | [diff] [blame] | 66 | private boolean mIsDisabled; |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 67 | |
Sunny Goyal | 726bee7 | 2018-03-05 12:54:24 -0800 | [diff] [blame] | 68 | // 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 Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 71 | @Override |
| 72 | public Float get(FastBitmapDrawable fastBitmapDrawable) { |
Sunny Goyal | 726bee7 | 2018-03-05 12:54:24 -0800 | [diff] [blame] | 73 | return fastBitmapDrawable.mScale; |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public void set(FastBitmapDrawable fastBitmapDrawable, Float value) { |
Sunny Goyal | 726bee7 | 2018-03-05 12:54:24 -0800 | [diff] [blame] | 78 | fastBitmapDrawable.mScale = value; |
| 79 | fastBitmapDrawable.invalidateSelf(); |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 80 | } |
| 81 | }; |
Sunny Goyal | 726bee7 | 2018-03-05 12:54:24 -0800 | [diff] [blame] | 82 | private ObjectAnimator mScaleAnimation; |
| 83 | private float mScale = 1; |
| 84 | |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 85 | |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 86 | // The saturation and brightness are values that are mapped to REDUCED_FILTER_VALUE_SPACE and |
| 87 | // as a result, can be used to compose the key for the cached ColorMatrixColorFilters |
| 88 | private int mDesaturation = 0; |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 89 | private int mBrightness = 0; |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 90 | private int mAlpha = 255; |
| 91 | private int mPrevUpdateKey = Integer.MAX_VALUE; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 92 | |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 93 | public FastBitmapDrawable(Bitmap b) { |
Sunny Goyal | 179249d | 2017-12-19 16:49:24 -0800 | [diff] [blame] | 94 | this(b, Color.TRANSPARENT); |
| 95 | } |
| 96 | |
| 97 | public FastBitmapDrawable(BitmapInfo info) { |
| 98 | this(info.icon, info.color); |
| 99 | } |
| 100 | |
| 101 | public FastBitmapDrawable(ItemInfoWithIcon info) { |
| 102 | this(info.iconBitmap, info.iconColor); |
| 103 | } |
| 104 | |
| 105 | protected FastBitmapDrawable(Bitmap b, int iconColor) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 106 | mBitmap = b; |
Sunny Goyal | 179249d | 2017-12-19 16:49:24 -0800 | [diff] [blame] | 107 | mIconColor = iconColor; |
Sunny Goyal | 96ac68a | 2017-02-02 16:37:21 -0800 | [diff] [blame] | 108 | setFilterBitmap(true); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | @Override |
Sunny Goyal | 726bee7 | 2018-03-05 12:54:24 -0800 | [diff] [blame] | 112 | public final void draw(Canvas canvas) { |
Matthew Ng | eb9cc9d | 2018-06-25 15:32:24 -0700 | [diff] [blame] | 113 | if (mScale != 1f) { |
Sunny Goyal | 726bee7 | 2018-03-05 12:54:24 -0800 | [diff] [blame] | 114 | int count = canvas.save(); |
| 115 | Rect bounds = getBounds(); |
| 116 | canvas.scale(mScale, mScale, bounds.exactCenterX(), bounds.exactCenterY()); |
| 117 | drawInternal(canvas, bounds); |
| 118 | canvas.restoreToCount(count); |
| 119 | } else { |
| 120 | drawInternal(canvas, getBounds()); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | protected void drawInternal(Canvas canvas, Rect bounds) { |
| 125 | canvas.drawBitmap(mBitmap, null, bounds, mPaint); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Override |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 129 | public void setColorFilter(ColorFilter cf) { |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 130 | // No op |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 134 | public int getOpacity() { |
| 135 | return PixelFormat.TRANSLUCENT; |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public void setAlpha(int alpha) { |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 140 | mAlpha = alpha; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 141 | mPaint.setAlpha(alpha); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 144 | @Override |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 145 | public void setFilterBitmap(boolean filterBitmap) { |
| 146 | mPaint.setFilterBitmap(filterBitmap); |
Winson Chung | 6e1c0d3 | 2013-10-25 15:24:24 -0700 | [diff] [blame] | 147 | mPaint.setAntiAlias(filterBitmap); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 150 | public int getAlpha() { |
| 151 | return mAlpha; |
| 152 | } |
| 153 | |
Matthew Ng | eb9cc9d | 2018-06-25 15:32:24 -0700 | [diff] [blame] | 154 | public void setScale(float scale) { |
| 155 | if (mScaleAnimation != null) { |
| 156 | mScaleAnimation.cancel(); |
| 157 | mScaleAnimation = null; |
| 158 | } |
| 159 | mScale = scale; |
| 160 | invalidateSelf(); |
| 161 | } |
| 162 | |
Sunny Goyal | 726bee7 | 2018-03-05 12:54:24 -0800 | [diff] [blame] | 163 | public float getAnimatedScale() { |
| 164 | return mScaleAnimation == null ? 1 : mScale; |
| 165 | } |
| 166 | |
Matthew Ng | eb9cc9d | 2018-06-25 15:32:24 -0700 | [diff] [blame] | 167 | public float getScale() { |
| 168 | return mScale; |
| 169 | } |
| 170 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 171 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 172 | public int getIntrinsicWidth() { |
Sunny Goyal | c424f22 | 2014-09-05 07:04:59 -0700 | [diff] [blame] | 173 | return mBitmap.getWidth(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public int getIntrinsicHeight() { |
Sunny Goyal | c424f22 | 2014-09-05 07:04:59 -0700 | [diff] [blame] | 178 | return mBitmap.getHeight(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public int getMinimumWidth() { |
Winson Chung | eeb5bbc | 2013-11-13 15:47:05 -0800 | [diff] [blame] | 183 | return getBounds().width(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public int getMinimumHeight() { |
Winson Chung | eeb5bbc | 2013-11-13 15:47:05 -0800 | [diff] [blame] | 188 | return getBounds().height(); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 191 | @Override |
| 192 | public boolean isStateful() { |
| 193 | return true; |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 196 | @Override |
Sunny Goyal | 2814112 | 2017-06-21 17:28:23 -0700 | [diff] [blame] | 197 | public ColorFilter getColorFilter() { |
| 198 | return mPaint.getColorFilter(); |
| 199 | } |
| 200 | |
| 201 | @Override |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 202 | protected boolean onStateChange(int[] state) { |
| 203 | boolean isPressed = false; |
| 204 | for (int s : state) { |
| 205 | if (s == android.R.attr.state_pressed) { |
| 206 | isPressed = true; |
| 207 | break; |
| 208 | } |
| 209 | } |
| 210 | if (mIsPressed != isPressed) { |
| 211 | mIsPressed = isPressed; |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 212 | |
Sunny Goyal | 726bee7 | 2018-03-05 12:54:24 -0800 | [diff] [blame] | 213 | if (mScaleAnimation != null) { |
| 214 | mScaleAnimation.cancel(); |
| 215 | mScaleAnimation = null; |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 216 | } |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 217 | |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 218 | if (mIsPressed) { |
| 219 | // Animate when going to pressed state |
Sunny Goyal | 726bee7 | 2018-03-05 12:54:24 -0800 | [diff] [blame] | 220 | mScaleAnimation = ObjectAnimator.ofFloat(this, SCALE, PRESSED_SCALE); |
| 221 | mScaleAnimation.setDuration(CLICK_FEEDBACK_DURATION); |
| 222 | mScaleAnimation.setInterpolator(ACCEL); |
| 223 | mScaleAnimation.start(); |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 224 | } else { |
Hyunyoung Song | ef468d8 | 2019-01-03 01:02:43 -0800 | [diff] [blame] | 225 | if (isVisible()) { |
| 226 | mScaleAnimation = ObjectAnimator.ofFloat(this, SCALE, 1f); |
| 227 | mScaleAnimation.setDuration(CLICK_FEEDBACK_DURATION); |
| 228 | mScaleAnimation.setInterpolator(DEACCEL); |
| 229 | mScaleAnimation.start(); |
| 230 | } else { |
| 231 | mScale = 1f; |
| 232 | invalidateSelf(); |
| 233 | } |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 234 | } |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 235 | return true; |
| 236 | } |
| 237 | return false; |
| 238 | } |
| 239 | |
Tony Wickham | 6b910a2 | 2016-11-08 10:40:34 -0800 | [diff] [blame] | 240 | private void invalidateDesaturationAndBrightness() { |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 241 | setDesaturation(mIsDisabled ? DISABLED_DESATURATION : 0); |
Sunny Goyal | 726bee7 | 2018-03-05 12:54:24 -0800 | [diff] [blame] | 242 | setBrightness(mIsDisabled ? DISABLED_BRIGHTNESS : 0); |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Tony Wickham | 6b910a2 | 2016-11-08 10:40:34 -0800 | [diff] [blame] | 245 | public void setIsDisabled(boolean isDisabled) { |
| 246 | if (mIsDisabled != isDisabled) { |
| 247 | mIsDisabled = isDisabled; |
| 248 | invalidateDesaturationAndBrightness(); |
| 249 | } |
| 250 | } |
| 251 | |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 252 | /** |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 253 | * Sets the saturation of this icon, 0 [full color] -> 1 [desaturated] |
| 254 | */ |
Sunny Goyal | 55cb70b | 2016-11-12 09:58:29 -0800 | [diff] [blame] | 255 | private void setDesaturation(float desaturation) { |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 256 | int newDesaturation = (int) Math.floor(desaturation * REDUCED_FILTER_VALUE_SPACE); |
| 257 | if (mDesaturation != newDesaturation) { |
| 258 | mDesaturation = newDesaturation; |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 259 | updateFilter(); |
| 260 | } |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 263 | public float getDesaturation() { |
| 264 | return (float) mDesaturation / REDUCED_FILTER_VALUE_SPACE; |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 267 | /** |
| 268 | * Sets the brightness of this icon, 0 [no add. brightness] -> 1 [2bright2furious] |
| 269 | */ |
Sunny Goyal | 55cb70b | 2016-11-12 09:58:29 -0800 | [diff] [blame] | 270 | private void setBrightness(float brightness) { |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 271 | int newBrightness = (int) Math.floor(brightness * REDUCED_FILTER_VALUE_SPACE); |
| 272 | if (mBrightness != newBrightness) { |
| 273 | mBrightness = newBrightness; |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 274 | updateFilter(); |
| 275 | } |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Sunny Goyal | 55cb70b | 2016-11-12 09:58:29 -0800 | [diff] [blame] | 278 | private float getBrightness() { |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 279 | return (float) mBrightness / REDUCED_FILTER_VALUE_SPACE; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Updates the paint to reflect the current brightness and saturation. |
| 284 | */ |
Sunny Goyal | 0ffab44 | 2018-06-07 17:31:48 -0700 | [diff] [blame] | 285 | protected void updateFilter() { |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 286 | boolean usePorterDuffFilter = false; |
| 287 | int key = -1; |
| 288 | if (mDesaturation > 0) { |
| 289 | key = (mDesaturation << 16) | mBrightness; |
| 290 | } else if (mBrightness > 0) { |
| 291 | // Compose a key with a fully saturated icon if we are just animating brightness |
| 292 | key = (1 << 16) | mBrightness; |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 293 | |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 294 | // We found that in L, ColorFilters cause drawing artifacts with shadows baked into |
| 295 | // icons, so just use a PorterDuff filter when we aren't animating saturation |
| 296 | usePorterDuffFilter = true; |
| 297 | } |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 298 | |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 299 | // Debounce multiple updates on the same frame |
| 300 | if (key == mPrevUpdateKey) { |
| 301 | return; |
| 302 | } |
| 303 | mPrevUpdateKey = key; |
| 304 | |
| 305 | if (key != -1) { |
| 306 | ColorFilter filter = sCachedFilter.get(key); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 307 | if (filter == null) { |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 308 | float brightnessF = getBrightness(); |
| 309 | int brightnessI = (int) (255 * brightnessF); |
| 310 | if (usePorterDuffFilter) { |
| 311 | filter = new PorterDuffColorFilter(Color.argb(brightnessI, 255, 255, 255), |
| 312 | PorterDuff.Mode.SRC_ATOP); |
| 313 | } else { |
| 314 | float saturationF = 1f - getDesaturation(); |
| 315 | sTempFilterMatrix.setSaturation(saturationF); |
| 316 | if (mBrightness > 0) { |
| 317 | // Brightness: C-new = C-old*(1-amount) + amount |
| 318 | float scale = 1f - brightnessF; |
| 319 | float[] mat = sTempBrightnessMatrix.getArray(); |
| 320 | mat[0] = scale; |
| 321 | mat[6] = scale; |
| 322 | mat[12] = scale; |
| 323 | mat[4] = brightnessI; |
| 324 | mat[9] = brightnessI; |
| 325 | mat[14] = brightnessI; |
| 326 | sTempFilterMatrix.preConcat(sTempBrightnessMatrix); |
| 327 | } |
| 328 | filter = new ColorMatrixColorFilter(sTempFilterMatrix); |
| 329 | } |
| 330 | sCachedFilter.append(key, filter); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 331 | } |
| 332 | mPaint.setColorFilter(filter); |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 333 | } else { |
| 334 | mPaint.setColorFilter(null); |
| 335 | } |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 336 | invalidateSelf(); |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 337 | } |
Sunny Goyal | 338d15d | 2018-02-23 12:19:44 -0800 | [diff] [blame] | 338 | |
| 339 | @Override |
| 340 | public ConstantState getConstantState() { |
| 341 | return new MyConstantState(mBitmap, mIconColor); |
| 342 | } |
| 343 | |
Sunny Goyal | 61e0846 | 2018-03-02 17:25:59 -0800 | [diff] [blame] | 344 | protected static class MyConstantState extends ConstantState { |
| 345 | protected final Bitmap mBitmap; |
| 346 | protected final int mIconColor; |
Sunny Goyal | 338d15d | 2018-02-23 12:19:44 -0800 | [diff] [blame] | 347 | |
| 348 | public MyConstantState(Bitmap bitmap, int color) { |
| 349 | mBitmap = bitmap; |
| 350 | mIconColor = color; |
| 351 | } |
| 352 | |
| 353 | @Override |
| 354 | public Drawable newDrawable() { |
| 355 | return new FastBitmapDrawable(mBitmap, mIconColor); |
| 356 | } |
| 357 | |
| 358 | @Override |
| 359 | public int getChangingConfigurations() { |
| 360 | return 0; |
| 361 | } |
| 362 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 363 | } |