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