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