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