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.Rect; |
| 32 | import android.graphics.drawable.Drawable; |
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 | |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 35 | public class FastBitmapDrawable extends Drawable { |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 36 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 37 | static final TimeInterpolator CLICK_FEEDBACK_INTERPOLATOR = new TimeInterpolator() { |
| 38 | |
| 39 | @Override |
| 40 | public float getInterpolation(float input) { |
| 41 | if (input < 0.05f) { |
| 42 | return input / 0.05f; |
| 43 | } else if (input < 0.3f){ |
| 44 | return 1; |
| 45 | } else { |
| 46 | return (1 - input) / 0.7f; |
| 47 | } |
| 48 | } |
| 49 | }; |
| 50 | static final long CLICK_FEEDBACK_DURATION = 2000; |
| 51 | |
| 52 | private static final int PRESSED_BRIGHTNESS = 100; |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 53 | private static ColorMatrix sGhostModeMatrix; |
| 54 | private static final ColorMatrix sTempMatrix = new ColorMatrix(); |
| 55 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 56 | /** |
| 57 | * Store the brightness colors filters to optimize animations during icon press. This |
| 58 | * only works for non-ghost-mode icons. |
| 59 | */ |
| 60 | private static final SparseArray<ColorFilter> sCachedBrightnessFilter = |
| 61 | new SparseArray<ColorFilter>(); |
| 62 | |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 63 | private static final int GHOST_MODE_MIN_COLOR_RANGE = 130; |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 64 | |
| 65 | private final Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 66 | private final Bitmap mBitmap; |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 67 | private int mAlpha; |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 68 | |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 69 | private int mBrightness = 0; |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 70 | private boolean mGhostModeEnabled = false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 71 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 72 | private boolean mPressed = false; |
| 73 | private ObjectAnimator mPressedAnimator; |
| 74 | |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 75 | public FastBitmapDrawable(Bitmap b) { |
Winson Chung | eeb5bbc | 2013-11-13 15:47:05 -0800 | [diff] [blame] | 76 | mAlpha = 255; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 77 | mBitmap = b; |
Winson Chung | 268f1c5 | 2013-11-18 14:04:41 -0800 | [diff] [blame] | 78 | setBounds(0, 0, b.getWidth(), b.getHeight()); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public void draw(Canvas canvas) { |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 83 | final Rect r = getBounds(); |
Winson Chung | 8a19635 | 2013-05-28 16:12:55 -0700 | [diff] [blame] | 84 | // Draw the bitmap into the bounding rect |
| 85 | canvas.drawBitmap(mBitmap, null, r, mPaint); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Override |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 89 | public void setColorFilter(ColorFilter cf) { |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 90 | // No op |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 94 | public int getOpacity() { |
| 95 | return PixelFormat.TRANSLUCENT; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public void setAlpha(int alpha) { |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 100 | mAlpha = alpha; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 101 | mPaint.setAlpha(alpha); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 104 | @Override |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 105 | public void setFilterBitmap(boolean filterBitmap) { |
| 106 | mPaint.setFilterBitmap(filterBitmap); |
Winson Chung | 6e1c0d3 | 2013-10-25 15:24:24 -0700 | [diff] [blame] | 107 | mPaint.setAntiAlias(filterBitmap); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 110 | public int getAlpha() { |
| 111 | return mAlpha; |
| 112 | } |
| 113 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 114 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 115 | public int getIntrinsicWidth() { |
Sunny Goyal | c424f22 | 2014-09-05 07:04:59 -0700 | [diff] [blame] | 116 | return mBitmap.getWidth(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public int getIntrinsicHeight() { |
Sunny Goyal | c424f22 | 2014-09-05 07:04:59 -0700 | [diff] [blame] | 121 | return mBitmap.getHeight(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public int getMinimumWidth() { |
Winson Chung | eeb5bbc | 2013-11-13 15:47:05 -0800 | [diff] [blame] | 126 | return getBounds().width(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public int getMinimumHeight() { |
Winson Chung | eeb5bbc | 2013-11-13 15:47:05 -0800 | [diff] [blame] | 131 | return getBounds().height(); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 132 | } |
| 133 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 134 | public Bitmap getBitmap() { |
| 135 | return mBitmap; |
| 136 | } |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 137 | |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 138 | /** |
| 139 | * When enabled, the icon is grayed out and the contrast is increased to give it a 'ghost' |
| 140 | * appearance. |
| 141 | */ |
| 142 | public void setGhostModeEnabled(boolean enabled) { |
| 143 | if (mGhostModeEnabled != enabled) { |
| 144 | mGhostModeEnabled = enabled; |
| 145 | updateFilter(); |
| 146 | } |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 149 | public void setPressed(boolean pressed) { |
| 150 | if (mPressed != pressed) { |
| 151 | mPressed = pressed; |
| 152 | if (mPressed) { |
| 153 | mPressedAnimator = ObjectAnimator |
| 154 | .ofInt(this, "brightness", PRESSED_BRIGHTNESS) |
| 155 | .setDuration(CLICK_FEEDBACK_DURATION); |
| 156 | mPressedAnimator.setInterpolator(CLICK_FEEDBACK_INTERPOLATOR); |
| 157 | mPressedAnimator.start(); |
| 158 | } else if (mPressedAnimator != null) { |
| 159 | mPressedAnimator.cancel(); |
| 160 | setBrightness(0); |
| 161 | } |
| 162 | } |
| 163 | invalidateSelf(); |
| 164 | } |
| 165 | |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 166 | public boolean isGhostModeEnabled() { |
| 167 | return mGhostModeEnabled; |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | public int getBrightness() { |
| 171 | return mBrightness; |
| 172 | } |
| 173 | |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 174 | public void setBrightness(int brightness) { |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 175 | if (mBrightness != brightness) { |
| 176 | mBrightness = brightness; |
| 177 | updateFilter(); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 178 | invalidateSelf(); |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 179 | } |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | private void updateFilter() { |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 183 | if (mGhostModeEnabled) { |
| 184 | if (sGhostModeMatrix == null) { |
| 185 | sGhostModeMatrix = new ColorMatrix(); |
| 186 | sGhostModeMatrix.setSaturation(0); |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 187 | |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 188 | // For ghost mode, set the color range to [GHOST_MODE_MIN_COLOR_RANGE, 255] |
| 189 | float range = (255 - GHOST_MODE_MIN_COLOR_RANGE) / 255.0f; |
| 190 | sTempMatrix.set(new float[] { |
| 191 | range, 0, 0, 0, GHOST_MODE_MIN_COLOR_RANGE, |
| 192 | 0, range, 0, 0, GHOST_MODE_MIN_COLOR_RANGE, |
| 193 | 0, 0, range, 0, GHOST_MODE_MIN_COLOR_RANGE, |
| 194 | 0, 0, 0, 1, 0 }); |
| 195 | sGhostModeMatrix.preConcat(sTempMatrix); |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 196 | } |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 197 | |
| 198 | if (mBrightness == 0) { |
| 199 | mPaint.setColorFilter(new ColorMatrixColorFilter(sGhostModeMatrix)); |
| 200 | } else { |
| 201 | setBrightnessMatrix(sTempMatrix, mBrightness); |
| 202 | sTempMatrix.postConcat(sGhostModeMatrix); |
| 203 | mPaint.setColorFilter(new ColorMatrixColorFilter(sTempMatrix)); |
| 204 | } |
| 205 | } else if (mBrightness != 0) { |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 206 | ColorFilter filter = sCachedBrightnessFilter.get(mBrightness); |
| 207 | if (filter == null) { |
| 208 | filter = new PorterDuffColorFilter(Color.argb(mBrightness, 255, 255, 255), |
| 209 | PorterDuff.Mode.SRC_ATOP); |
| 210 | sCachedBrightnessFilter.put(mBrightness, filter); |
| 211 | } |
| 212 | mPaint.setColorFilter(filter); |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 213 | } else { |
| 214 | mPaint.setColorFilter(null); |
| 215 | } |
| 216 | } |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 217 | |
| 218 | private static void setBrightnessMatrix(ColorMatrix matrix, int brightness) { |
| 219 | // Brightness: C-new = C-old*(1-amount) + amount |
| 220 | float scale = 1 - brightness / 255.0f; |
| 221 | matrix.setScale(scale, scale, scale, 1); |
| 222 | float[] array = matrix.getArray(); |
| 223 | |
| 224 | // Add the amount to RGB components of the matrix, as per the above formula. |
| 225 | // Fifth elements in the array correspond to the constant being added to |
| 226 | // red, blue, green, and alpha channel respectively. |
| 227 | array[4] = brightness; |
| 228 | array[9] = brightness; |
| 229 | array[14] = brightness; |
| 230 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 231 | } |