blob: 598761eef6c08b686c0e30affe4a13d4e674217d [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;
Winson Chungb3347bb2010-08-19 14:51:28 -070024import android.graphics.Matrix;
25import android.graphics.Paint;
26import android.graphics.PointF;
27import android.graphics.PorterDuff;
28import android.graphics.PorterDuffXfermode;
29import android.graphics.Rect;
30import android.graphics.Region.Op;
Winson Chung03c568e2010-08-19 17:16:59 -070031import android.graphics.drawable.Drawable;
Winson Chungb3347bb2010-08-19 14:51:28 -070032import android.util.AttributeSet;
33import android.util.DisplayMetrics;
Winson Chung03c568e2010-08-19 17:16:59 -070034import android.widget.Checkable;
Winson Chungb3347bb2010-08-19 14:51:28 -070035import android.widget.TextView;
36
37class 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 Chung03c568e2010-08-19 17:16:59 -070045 public static final int HOLOGRAPHIC_BLUE = 0xFF6699FF;
46 public static final int HOLOGRAPHIC_GREEN = 0xFF66FF66;
47
Winson Chungb3347bb2010-08-19 14:51:28 -070048 HolographicOutlineHelper(float density) {
Winson Chung03c568e2010-08-19 17:16:59 -070049 mHolographicPaint.setColor(HOLOGRAPHIC_BLUE);
Winson Chungb3347bb2010-08-19 14:51:28 -070050 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 Chung03c568e2010-08-19 17:16:59 -070090 * 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 Chungb3347bb2010-08-19 14:51:28 -070097 * 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 Chung03c568e2010-08-19 17:16:59 -0700130public class PagedViewIcon extends TextView implements Checkable {
Winson Chungb3347bb2010-08-19 14:51:28 -0700131 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 Chung03c568e2010-08-19 17:16:59 -0700144 private boolean mIsChecked;
145
Winson Chungb3347bb2010-08-19 14:51:28 -0700146 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 Chungb3347bb2010-08-19 14:51:28 -0700172 final float holographicAlpha = sHolographicOutlineHelper.highlightAlphaInterpolator(alpha);
Winson Chungaffd7b42010-08-20 15:11:56 -0700173 mAlpha = (int) (viewAlpha * 255);
Winson Chungb3347bb2010-08-19 14:51:28 -0700174 mHolographicAlpha = (int) (holographicAlpha * 255);
Winson Chungb3347bb2010-08-19 14:51:28 -0700175 super.setAlpha(viewAlpha);
176 }
177
178 @Override
Winson Chung03c568e2010-08-19 17:16:59 -0700179 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 Chungb3347bb2010-08-19 14:51:28 -0700194 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 Chung03c568e2010-08-19 17:16:59 -0700213 sHolographicOutlineHelper.setColor(HolographicOutlineHelper.HOLOGRAPHIC_BLUE);
214 sHolographicOutlineHelper.applyOutline(mHolographicOutline, mHolographicOutlineCanvas,
Winson Chungb3347bb2010-08-19 14:51:28 -0700215 offset);
216 sHolographicOutlineHelper.applyBlur(mHolographicOutline, mHolographicOutlineCanvas);
217 mIsHolographicUpdatePass = false;
218 }
219 }
220
Winson Chungb3347bb2010-08-19 14:51:28 -0700221 @Override
222 protected void onDraw(Canvas canvas) {
Winson Chungaffd7b42010-08-20 15:11:56 -0700223 // draw the view itself
Winson Chungb3347bb2010-08-19 14:51:28 -0700224 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 Chungaffd7b42010-08-20 15:11:56 -0700228 final float alpha = getAlpha();
229 super.setAlpha(1.0f);
Winson Chungb3347bb2010-08-19 14:51:28 -0700230 super.onDraw(canvas);
Winson Chungaffd7b42010-08-20 15:11:56 -0700231 super.setAlpha(alpha);
Winson Chungb3347bb2010-08-19 14:51:28 -0700232 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 Chung03c568e2010-08-19 17:16:59 -0700244
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 Chungb3347bb2010-08-19 14:51:28 -0700260}