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