blob: bca3f715e5809e021a9c2517df9cf54ac6069a32 [file] [log] [blame]
Michael Jurka5f1c5092010-09-03 14:15:02 -07001/*
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
17package com.android.launcher2;
18
19import android.graphics.Bitmap;
20import android.graphics.BlurMaskFilter;
21import android.graphics.Canvas;
Joe Onorato4be866d2010-10-10 11:26:02 -070022import android.graphics.MaskFilter;
Michael Jurka5f1c5092010-09-03 14:15:02 -070023import android.graphics.Paint;
Michael Jurka5f1c5092010-09-03 14:15:02 -070024import android.graphics.PorterDuff;
25import android.graphics.PorterDuffXfermode;
Joe Onorato4be866d2010-10-10 11:26:02 -070026import android.graphics.TableMaskFilter;
Michael Jurka5f1c5092010-09-03 14:15:02 -070027
28public class HolographicOutlineHelper {
Michael Jurka5f1c5092010-09-03 14:15:02 -070029 private final Paint mHolographicPaint = new Paint();
Joe Onorato4be866d2010-10-10 11:26:02 -070030 private final Paint mBlurPaint = new Paint();
Michael Jurka5f1c5092010-09-03 14:15:02 -070031 private final Paint mErasePaint = new Paint();
Michael Jurka5f1c5092010-09-03 14:15:02 -070032
Patrick Dubroy8e58e912010-10-14 13:21:48 -070033 public static final int OUTER_BLUR_RADIUS;
Joe Onorato4be866d2010-10-10 11:26:02 -070034
Patrick Dubroy8e58e912010-10-14 13:21:48 -070035 private static final BlurMaskFilter sThickOuterBlurMaskFilter;
36 private static final BlurMaskFilter sMediumOuterBlurMaskFilter;
37 private static final BlurMaskFilter sThinOuterBlurMaskFilter;
38 private static final BlurMaskFilter sThickInnerBlurMaskFilter;
39
40 static {
41 final float scale = LauncherApplication.getScreenDensity();
42
43 OUTER_BLUR_RADIUS = (int) (scale * 6.0f);
44
45 sThickOuterBlurMaskFilter = new BlurMaskFilter(OUTER_BLUR_RADIUS,
46 BlurMaskFilter.Blur.OUTER);
47 sMediumOuterBlurMaskFilter = new BlurMaskFilter(scale * 2.0f, BlurMaskFilter.Blur.OUTER);
48 sThinOuterBlurMaskFilter = new BlurMaskFilter(scale * 1.0f, BlurMaskFilter.Blur.OUTER);
49 sThickInnerBlurMaskFilter = new BlurMaskFilter(scale * 4.0f, BlurMaskFilter.Blur.NORMAL);
50 }
Joe Onorato4be866d2010-10-10 11:26:02 -070051
52 private static final MaskFilter sFineClipTable = TableMaskFilter.CreateClipTable(0, 20);
53 private static final MaskFilter sCoarseClipTable = TableMaskFilter.CreateClipTable(0, 200);
54
55 private int[] mTempOffset = new int[2];
Michael Jurka5f1c5092010-09-03 14:15:02 -070056
Winson Chung64a3cd42010-09-17 16:47:33 -070057 HolographicOutlineHelper() {
Michael Jurka5f1c5092010-09-03 14:15:02 -070058 mHolographicPaint.setFilterBitmap(true);
59 mHolographicPaint.setAntiAlias(true);
Joe Onorato4be866d2010-10-10 11:26:02 -070060 mBlurPaint.setFilterBitmap(true);
61 mBlurPaint.setAntiAlias(true);
Michael Jurka5f1c5092010-09-03 14:15:02 -070062 mErasePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
63 mErasePaint.setFilterBitmap(true);
64 mErasePaint.setAntiAlias(true);
65 }
66
Michael Jurka5f1c5092010-09-03 14:15:02 -070067 /**
68 * Returns the interpolated holographic highlight alpha for the effect we want when scrolling
69 * pages.
70 */
71 public float highlightAlphaInterpolator(float r) {
Winson Chung64a3cd42010-09-17 16:47:33 -070072 float maxAlpha = 0.8f;
Winson Chunge8878e32010-09-15 20:37:09 -070073 return (float) Math.pow(maxAlpha * (1.0f - r), 1.5f);
Michael Jurka5f1c5092010-09-03 14:15:02 -070074 }
75
76 /**
77 * Returns the interpolated view alpha for the effect we want when scrolling pages.
78 */
79 public float viewAlphaInterpolator(float r) {
Winson Chunge8878e32010-09-15 20:37:09 -070080 final float pivot = 0.95f;
Michael Jurka5f1c5092010-09-03 14:15:02 -070081 if (r < pivot) {
Winson Chunge8878e32010-09-15 20:37:09 -070082 return (float) Math.pow(r / pivot, 1.5f);
Michael Jurka5f1c5092010-09-03 14:15:02 -070083 } else {
84 return 1.0f;
85 }
86 }
87
Patrick Dubroy8e58e912010-10-14 13:21:48 -070088 /**
89 * Apply an outer blur to the given bitmap.
90 * You should use OUTER_BLUR_RADIUS to ensure that the bitmap is big enough to draw
91 * the blur without clipping.
92 */
93 void applyOuterBlur(Bitmap bitmap, Canvas canvas, int color) {
Joe Onorato4be866d2010-10-10 11:26:02 -070094 mBlurPaint.setMaskFilter(sThickOuterBlurMaskFilter);
95 Bitmap glow = bitmap.extractAlpha(mBlurPaint, mTempOffset);
96
97 // Use the clip table to make the glow heavier closer to the outline
98 mHolographicPaint.setMaskFilter(sCoarseClipTable);
99 mHolographicPaint.setAlpha(150);
100 mHolographicPaint.setColor(color);
101 canvas.drawBitmap(glow, mTempOffset[0], mTempOffset[1], mHolographicPaint);
102 glow.recycle();
103 }
104
105 /**
106 * Draws a solid outline around a bitmap, erasing the original pixels.
107 *
108 * @param bitmap The bitmap to modify
109 * @param canvas A canvas on the bitmap
110 * @param color The color to draw the outline and glow in
111 * @param removeOrig If true, punch out the original pixels to just leave the outline
112 */
113 void applyExpensiveOuterOutline(Bitmap bitmap, Canvas canvas, int color, boolean removeOrig) {
114 Bitmap originalImage = null;
115 if (removeOrig) {
116 originalImage = bitmap.extractAlpha();
117 }
118
119 // Compute an outer blur on the original bitmap
120 mBlurPaint.setMaskFilter(sMediumOuterBlurMaskFilter);
121 Bitmap outline = bitmap.extractAlpha(mBlurPaint, mTempOffset);
122
123 // Paint the blurred bitmap back into the canvas. Using the clip table causes any alpha
124 // pixels above a certain threshold to be rounded up to be fully opaque. This gives the
125 // effect of a thick outline, with a slight blur on the edge
126 mHolographicPaint.setColor(color);
127 mHolographicPaint.setMaskFilter(sFineClipTable);
128 canvas.drawBitmap(outline, mTempOffset[0], mTempOffset[1], mHolographicPaint);
129 outline.recycle();
130
131 if (removeOrig) {
132 // Finally, punch out the original pixels, leaving just the outline
133 canvas.drawBitmap(originalImage, 0, 0, mErasePaint);
134 originalImage.recycle();
135 }
136 }
137
Michael Jurka5f1c5092010-09-03 14:15:02 -0700138 /**
Winson Chung64a3cd42010-09-17 16:47:33 -0700139 * Applies a more expensive and accurate outline to whatever is currently drawn in a specified
140 * bitmap.
Michael Jurka5f1c5092010-09-03 14:15:02 -0700141 */
Winson Chung64a3cd42010-09-17 16:47:33 -0700142 void applyExpensiveOutlineWithBlur(Bitmap srcDst, Canvas srcDstCanvas, int color,
143 int outlineColor) {
144 // calculate the outer blur first
Joe Onorato4be866d2010-10-10 11:26:02 -0700145 mBlurPaint.setMaskFilter(sThickOuterBlurMaskFilter);
Winson Chung64a3cd42010-09-17 16:47:33 -0700146 int[] outerBlurOffset = new int[2];
Joe Onorato4be866d2010-10-10 11:26:02 -0700147 Bitmap thickOuterBlur = srcDst.extractAlpha(mBlurPaint, outerBlurOffset);
148 mBlurPaint.setMaskFilter(sThinOuterBlurMaskFilter);
Winson Chung64a3cd42010-09-17 16:47:33 -0700149 int[] thinOuterBlurOffset = new int[2];
Joe Onorato4be866d2010-10-10 11:26:02 -0700150 Bitmap thinOuterBlur = srcDst.extractAlpha(mBlurPaint, thinOuterBlurOffset);
Winson Chung64a3cd42010-09-17 16:47:33 -0700151
152 // calculate the inner blur
153 srcDstCanvas.drawColor(0xFF000000, PorterDuff.Mode.SRC_OUT);
Joe Onorato4be866d2010-10-10 11:26:02 -0700154 mBlurPaint.setMaskFilter(sThickInnerBlurMaskFilter);
Winson Chung64a3cd42010-09-17 16:47:33 -0700155 int[] thickInnerBlurOffset = new int[2];
Joe Onorato4be866d2010-10-10 11:26:02 -0700156 Bitmap thickInnerBlur = srcDst.extractAlpha(mBlurPaint, thickInnerBlurOffset);
Winson Chung64a3cd42010-09-17 16:47:33 -0700157
158 // mask out the inner blur
159 srcDstCanvas.setBitmap(thickInnerBlur);
160 srcDstCanvas.drawBitmap(srcDst, -thickInnerBlurOffset[0],
161 -thickInnerBlurOffset[1], mErasePaint);
162 srcDstCanvas.drawRect(0, 0, -thickInnerBlurOffset[0], thickInnerBlur.getHeight(),
163 mErasePaint);
164 srcDstCanvas.drawRect(0, 0, thickInnerBlur.getWidth(), -thickInnerBlurOffset[1],
165 mErasePaint);
166
167 // draw the inner and outer blur
168 srcDstCanvas.setBitmap(srcDst);
169 srcDstCanvas.drawColor(0x00000000, PorterDuff.Mode.CLEAR);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700170 mHolographicPaint.setColor(color);
Winson Chung64a3cd42010-09-17 16:47:33 -0700171 srcDstCanvas.drawBitmap(thickInnerBlur, thickInnerBlurOffset[0], thickInnerBlurOffset[1],
172 mHolographicPaint);
173 srcDstCanvas.drawBitmap(thickOuterBlur, outerBlurOffset[0], outerBlurOffset[1],
174 mHolographicPaint);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700175
Winson Chung64a3cd42010-09-17 16:47:33 -0700176 // draw the bright outline
177 mHolographicPaint.setColor(outlineColor);
178 srcDstCanvas.drawBitmap(thinOuterBlur, thinOuterBlurOffset[0], thinOuterBlurOffset[1],
179 mHolographicPaint);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700180
Winson Chung64a3cd42010-09-17 16:47:33 -0700181 // cleanup
182 thinOuterBlur.recycle();
183 thickOuterBlur.recycle();
184 thickInnerBlur.recycle();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700185 }
186}