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