Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | |
| 17 | package com.android.launcher2; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.res.Resources; |
| 21 | import android.graphics.Bitmap; |
| 22 | import android.graphics.BlurMaskFilter; |
| 23 | import android.graphics.Canvas; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 24 | import android.graphics.Matrix; |
| 25 | import android.graphics.Paint; |
| 26 | import android.graphics.PointF; |
| 27 | import android.graphics.PorterDuff; |
| 28 | import android.graphics.PorterDuffXfermode; |
| 29 | import android.graphics.Rect; |
| 30 | import android.graphics.Region.Op; |
Winson Chung | 03c568e | 2010-08-19 17:16:59 -0700 | [diff] [blame] | 31 | import android.graphics.drawable.Drawable; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 32 | import android.util.AttributeSet; |
| 33 | import android.util.DisplayMetrics; |
Winson Chung | 03c568e | 2010-08-19 17:16:59 -0700 | [diff] [blame] | 34 | import android.widget.Checkable; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 35 | import android.widget.TextView; |
| 36 | |
| 37 | class HolographicOutlineHelper { |
| 38 | private final Paint mHolographicPaint = new Paint(); |
| 39 | private final Paint mBlurPaint = new Paint(); |
| 40 | private final Paint mErasePaint = new Paint(); |
| 41 | private static final Matrix mIdentity = new Matrix(); |
| 42 | private static final float STROKE_WIDTH = 6.0f; |
| 43 | private static final float BLUR_FACTOR = 3.5f; |
| 44 | |
Winson Chung | 03c568e | 2010-08-19 17:16:59 -0700 | [diff] [blame] | 45 | public static final int HOLOGRAPHIC_BLUE = 0xFF6699FF; |
| 46 | public static final int HOLOGRAPHIC_GREEN = 0xFF66FF66; |
| 47 | |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 48 | HolographicOutlineHelper(float density) { |
Winson Chung | 03c568e | 2010-08-19 17:16:59 -0700 | [diff] [blame] | 49 | mHolographicPaint.setColor(HOLOGRAPHIC_BLUE); |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 50 | mHolographicPaint.setFilterBitmap(true); |
| 51 | mHolographicPaint.setAntiAlias(true); |
| 52 | mBlurPaint.setMaskFilter(new BlurMaskFilter(BLUR_FACTOR * density, |
| 53 | BlurMaskFilter.Blur.OUTER)); |
| 54 | mBlurPaint.setFilterBitmap(true); |
| 55 | mErasePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT)); |
| 56 | mErasePaint.setFilterBitmap(true); |
| 57 | mErasePaint.setAntiAlias(true); |
| 58 | } |
| 59 | |
| 60 | private float cubic(float r) { |
| 61 | return (float) (Math.pow(r-1, 3) + 1); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Returns the interpolated holographic highlight alpha for the effect we want when scrolling |
| 66 | * pages. |
| 67 | */ |
| 68 | public float highlightAlphaInterpolator(float r) { |
| 69 | final float pivot = 0.3f; |
| 70 | if (r < pivot) { |
| 71 | return Math.max(0.5f, 0.65f*cubic(r/pivot)); |
| 72 | } else { |
| 73 | return Math.min(1.0f, 0.65f*cubic(1 - (r-pivot)/(1-pivot))); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Returns the interpolated view alpha for the effect we want when scrolling pages. |
| 79 | */ |
| 80 | public float viewAlphaInterpolator(float r) { |
| 81 | final float pivot = 0.6f; |
| 82 | if (r < pivot) { |
| 83 | return r/pivot; |
| 84 | } else { |
| 85 | return 1.0f; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
Winson Chung | 03c568e | 2010-08-19 17:16:59 -0700 | [diff] [blame] | 90 | * Sets the color of the holographic paint to be used when applying the outline/blur. |
| 91 | */ |
| 92 | void setColor(int color) { |
| 93 | mHolographicPaint.setColor(color); |
| 94 | } |
| 95 | |
| 96 | /** |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 97 | * Applies an outline to whatever is currently drawn in the specified bitmap. |
| 98 | */ |
| 99 | void applyOutline(Bitmap srcDst, Canvas srcDstCanvas, PointF offset) { |
| 100 | Bitmap mask = srcDst.extractAlpha(); |
| 101 | Matrix m = new Matrix(); |
| 102 | final int width = srcDst.getWidth(); |
| 103 | final int height = srcDst.getHeight(); |
| 104 | float xScale = STROKE_WIDTH*2.0f/width; |
| 105 | float yScale = STROKE_WIDTH*2.0f/height; |
| 106 | m.preScale(1+xScale, 1+yScale, (width / 2.0f) + offset.x, |
| 107 | (height / 2.0f) + offset.y); |
| 108 | |
| 109 | srcDstCanvas.drawColor(0, PorterDuff.Mode.CLEAR); |
| 110 | srcDstCanvas.drawBitmap(mask, m, mHolographicPaint); |
| 111 | srcDstCanvas.drawBitmap(mask, mIdentity, mErasePaint); |
| 112 | mask.recycle(); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Applies an blur to whatever is currently drawn in the specified bitmap. |
| 117 | */ |
| 118 | void applyBlur(Bitmap srcDst, Canvas srcDstCanvas) { |
| 119 | int[] xy = new int[2]; |
| 120 | Bitmap mask = srcDst.extractAlpha(mBlurPaint, xy); |
| 121 | srcDstCanvas.drawBitmap(mask, xy[0], xy[1], mHolographicPaint); |
| 122 | mask.recycle(); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * An icon on a PagedView, specifically for items in the launcher's paged view (with compound |
| 128 | * drawables on the top). |
| 129 | */ |
Winson Chung | 03c568e | 2010-08-19 17:16:59 -0700 | [diff] [blame] | 130 | public class PagedViewIcon extends TextView implements Checkable { |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 131 | private static final String TAG = "PagedViewIcon"; |
| 132 | |
| 133 | // holographic outline |
| 134 | private final Paint mPaint = new Paint(); |
| 135 | private static HolographicOutlineHelper sHolographicOutlineHelper; |
| 136 | private Bitmap mHolographicOutline; |
| 137 | private Canvas mHolographicOutlineCanvas; |
| 138 | private boolean mIsHolographicUpdatePass; |
| 139 | private Rect mDrawableClipRect; |
| 140 | |
| 141 | private int mAlpha; |
| 142 | private int mHolographicAlpha; |
| 143 | |
Winson Chung | 03c568e | 2010-08-19 17:16:59 -0700 | [diff] [blame] | 144 | private boolean mIsChecked; |
| 145 | |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 146 | public PagedViewIcon(Context context) { |
| 147 | this(context, null); |
| 148 | } |
| 149 | |
| 150 | public PagedViewIcon(Context context, AttributeSet attrs) { |
| 151 | this(context, attrs, 0); |
| 152 | } |
| 153 | |
| 154 | public PagedViewIcon(Context context, AttributeSet attrs, int defStyle) { |
| 155 | super(context, attrs, defStyle); |
| 156 | |
| 157 | if (sHolographicOutlineHelper == null) { |
| 158 | final Resources resources = context.getResources(); |
| 159 | final DisplayMetrics metrics = resources.getDisplayMetrics(); |
| 160 | final float density = metrics.density; |
| 161 | sHolographicOutlineHelper = new HolographicOutlineHelper(density); |
| 162 | } |
| 163 | mDrawableClipRect = new Rect(); |
| 164 | |
| 165 | setFocusable(true); |
| 166 | setBackgroundDrawable(null); |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public void setAlpha(float alpha) { |
| 171 | final float viewAlpha = sHolographicOutlineHelper.viewAlphaInterpolator(alpha); |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 172 | final float holographicAlpha = sHolographicOutlineHelper.highlightAlphaInterpolator(alpha); |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 173 | mAlpha = (int) (viewAlpha * 255); |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 174 | mHolographicAlpha = (int) (holographicAlpha * 255); |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 175 | super.setAlpha(viewAlpha); |
| 176 | } |
| 177 | |
| 178 | @Override |
Winson Chung | 03c568e | 2010-08-19 17:16:59 -0700 | [diff] [blame] | 179 | public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, |
| 180 | Drawable right, Drawable bottom) { |
| 181 | invalidateHolographicImage(); |
| 182 | super.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom); |
| 183 | } |
| 184 | |
| 185 | public void invalidateHolographicImage() { |
| 186 | if (mHolographicOutline != null) { |
| 187 | mHolographicOutline.recycle(); |
| 188 | mHolographicOutline = null; |
| 189 | mHolographicOutlineCanvas = null; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | @Override |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 194 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 195 | super.onLayout(changed, left, top, right, bottom); |
| 196 | |
| 197 | if (mHolographicOutline == null) { |
| 198 | final PointF offset = new PointF(0, |
| 199 | -(getCompoundPaddingBottom() + getCompoundPaddingTop())/2.0f); |
| 200 | |
| 201 | // update the clipping rect to be used in the holographic pass below |
| 202 | getDrawingRect(mDrawableClipRect); |
| 203 | mDrawableClipRect.bottom = getPaddingTop() + getCompoundPaddingTop(); |
| 204 | |
| 205 | // set a flag to indicate that we are going to draw the view at full alpha with the text |
| 206 | // clipped for the generation of the holographic icon |
| 207 | mIsHolographicUpdatePass = true; |
| 208 | mHolographicOutline = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), |
| 209 | Bitmap.Config.ARGB_8888); |
| 210 | mHolographicOutlineCanvas = new Canvas(mHolographicOutline); |
| 211 | mHolographicOutlineCanvas.concat(getMatrix()); |
| 212 | draw(mHolographicOutlineCanvas); |
Winson Chung | 03c568e | 2010-08-19 17:16:59 -0700 | [diff] [blame] | 213 | sHolographicOutlineHelper.setColor(HolographicOutlineHelper.HOLOGRAPHIC_BLUE); |
| 214 | sHolographicOutlineHelper.applyOutline(mHolographicOutline, mHolographicOutlineCanvas, |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 215 | offset); |
| 216 | sHolographicOutlineHelper.applyBlur(mHolographicOutline, mHolographicOutlineCanvas); |
| 217 | mIsHolographicUpdatePass = false; |
| 218 | } |
| 219 | } |
| 220 | |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 221 | @Override |
| 222 | protected void onDraw(Canvas canvas) { |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 223 | // draw the view itself |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 224 | if (mIsHolographicUpdatePass) { |
| 225 | // only clip to the text view (restore its alpha so that we get a proper outline) |
| 226 | canvas.save(); |
| 227 | canvas.clipRect(mDrawableClipRect, Op.REPLACE); |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 228 | final float alpha = getAlpha(); |
| 229 | super.setAlpha(1.0f); |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 230 | super.onDraw(canvas); |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 231 | super.setAlpha(alpha); |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 232 | canvas.restore(); |
| 233 | } else { |
| 234 | if (mAlpha > 0) { |
| 235 | super.onDraw(canvas); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | if (!mIsHolographicUpdatePass && mHolographicOutline != null && mHolographicAlpha > 0) { |
| 240 | mPaint.setAlpha(mHolographicAlpha); |
| 241 | canvas.drawBitmap(mHolographicOutline, 0, 0, mPaint); |
| 242 | } |
| 243 | } |
Winson Chung | 03c568e | 2010-08-19 17:16:59 -0700 | [diff] [blame] | 244 | |
| 245 | @Override |
| 246 | public boolean isChecked() { |
| 247 | return mIsChecked; |
| 248 | } |
| 249 | |
| 250 | @Override |
| 251 | public void setChecked(boolean checked) { |
| 252 | mIsChecked = checked; |
| 253 | invalidate(); |
| 254 | } |
| 255 | |
| 256 | @Override |
| 257 | public void toggle() { |
| 258 | setChecked(!mIsChecked); |
| 259 | } |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 260 | } |