1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.cyanogenmod.trebuchet;
import android.graphics.Bitmap;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
public class HolographicOutlineHelper {
private final Paint mHolographicPaint = new Paint();
private final Paint mBlurPaint = new Paint();
private final Paint mErasePaint = new Paint();
public static final int MAX_OUTER_BLUR_RADIUS;
public static final int MIN_OUTER_BLUR_RADIUS;
private static final BlurMaskFilter sExtraThickOuterBlurMaskFilter;
private static final BlurMaskFilter sThickOuterBlurMaskFilter;
private static final BlurMaskFilter sMediumOuterBlurMaskFilter;
private static final BlurMaskFilter sThinOuterBlurMaskFilter;
private static final BlurMaskFilter sThickInnerBlurMaskFilter;
private static final BlurMaskFilter sExtraThickInnerBlurMaskFilter;
private static final BlurMaskFilter sMediumInnerBlurMaskFilter;
private static final int THICK = 0;
private static final int MEDIUM = 1;
private static final int EXTRA_THICK = 2;
static {
final float scale = LauncherApplication.getScreenDensity();
MIN_OUTER_BLUR_RADIUS = (int) (scale * 1.0f);
MAX_OUTER_BLUR_RADIUS = (int) (scale * 12.0f);
sExtraThickOuterBlurMaskFilter = new BlurMaskFilter(scale * 12.0f, BlurMaskFilter.Blur.OUTER);
sThickOuterBlurMaskFilter = new BlurMaskFilter(scale * 6.0f, BlurMaskFilter.Blur.OUTER);
sMediumOuterBlurMaskFilter = new BlurMaskFilter(scale * 2.0f, BlurMaskFilter.Blur.OUTER);
sThinOuterBlurMaskFilter = new BlurMaskFilter(scale * 1.0f, BlurMaskFilter.Blur.OUTER);
sExtraThickInnerBlurMaskFilter = new BlurMaskFilter(scale * 6.0f, BlurMaskFilter.Blur.NORMAL);
sThickInnerBlurMaskFilter = new BlurMaskFilter(scale * 4.0f, BlurMaskFilter.Blur.NORMAL);
sMediumInnerBlurMaskFilter = new BlurMaskFilter(scale * 2.0f, BlurMaskFilter.Blur.NORMAL);
}
HolographicOutlineHelper() {
mHolographicPaint.setFilterBitmap(true);
mHolographicPaint.setAntiAlias(true);
mBlurPaint.setFilterBitmap(true);
mBlurPaint.setAntiAlias(true);
mErasePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
mErasePaint.setFilterBitmap(true);
mErasePaint.setAntiAlias(true);
}
/**
* Returns the interpolated holographic highlight alpha for the effect we want when scrolling
* pages.
*/
public static float highlightAlphaInterpolator(float r) {
float maxAlpha = 0.6f;
return (float) Math.pow(maxAlpha * (1.0f - r), 1.5f);
}
/**
* Returns the interpolated view alpha for the effect we want when scrolling pages.
*/
public static float viewAlphaInterpolator(float r) {
final float pivot = 0.95f;
if (r < pivot) {
return (float) Math.pow(r / pivot, 1.5f);
} else {
return 1.0f;
}
}
/**
* Applies a more expensive and accurate outline to whatever is currently drawn in a specified
* bitmap.
*/
void applyExpensiveOutlineWithBlur(Bitmap srcDst, Canvas srcDstCanvas, int color,
int outlineColor, int thickness) {
applyExpensiveOutlineWithBlur(srcDst, srcDstCanvas, color, outlineColor, true,
thickness);
}
void applyExpensiveOutlineWithBlur(Bitmap srcDst, Canvas srcDstCanvas, int color,
int outlineColor, boolean clipAlpha, int thickness) {
// We start by removing most of the alpha channel so as to ignore shadows, and
// other types of partial transparency when defining the shape of the object
if (clipAlpha) {
int[] srcBuffer = new int[srcDst.getWidth() * srcDst.getHeight()];
srcDst.getPixels(srcBuffer,
0, srcDst.getWidth(), 0, 0, srcDst.getWidth(), srcDst.getHeight());
for (int i = 0; i < srcBuffer.length; i++) {
final int alpha = srcBuffer[i] >>> 24;
if (alpha < 188) {
srcBuffer[i] = 0;
}
}
srcDst.setPixels(srcBuffer,
0, srcDst.getWidth(), 0, 0, srcDst.getWidth(), srcDst.getHeight());
}
Bitmap glowShape = srcDst.extractAlpha();
// calculate the outer blur first
BlurMaskFilter outerBlurMaskFilter;
switch (thickness) {
case EXTRA_THICK:
outerBlurMaskFilter = sExtraThickOuterBlurMaskFilter;
break;
case THICK:
outerBlurMaskFilter = sThickOuterBlurMaskFilter;
break;
case MEDIUM:
outerBlurMaskFilter = sMediumOuterBlurMaskFilter;
break;
default:
throw new RuntimeException("Invalid blur thickness");
}
mBlurPaint.setMaskFilter(outerBlurMaskFilter);
int[] outerBlurOffset = new int[2];
Bitmap thickOuterBlur = glowShape.extractAlpha(mBlurPaint, outerBlurOffset);
if (thickness == EXTRA_THICK) {
mBlurPaint.setMaskFilter(sMediumOuterBlurMaskFilter);
} else {
mBlurPaint.setMaskFilter(sThinOuterBlurMaskFilter);
}
int[] brightOutlineOffset = new int[2];
Bitmap brightOutline = glowShape.extractAlpha(mBlurPaint, brightOutlineOffset);
// calculate the inner blur
srcDstCanvas.setBitmap(glowShape);
srcDstCanvas.drawColor(0xFF000000, PorterDuff.Mode.SRC_OUT);
BlurMaskFilter innerBlurMaskFilter;
switch (thickness) {
case EXTRA_THICK:
innerBlurMaskFilter = sExtraThickInnerBlurMaskFilter;
break;
case THICK:
innerBlurMaskFilter = sThickInnerBlurMaskFilter;
break;
case MEDIUM:
innerBlurMaskFilter = sMediumInnerBlurMaskFilter;
break;
default:
throw new RuntimeException("Invalid blur thickness");
}
mBlurPaint.setMaskFilter(innerBlurMaskFilter);
int[] thickInnerBlurOffset = new int[2];
Bitmap thickInnerBlur = glowShape.extractAlpha(mBlurPaint, thickInnerBlurOffset);
// mask out the inner blur
srcDstCanvas.setBitmap(thickInnerBlur);
srcDstCanvas.drawBitmap(glowShape, -thickInnerBlurOffset[0],
-thickInnerBlurOffset[1], mErasePaint);
srcDstCanvas.drawRect(0, 0, -thickInnerBlurOffset[0], thickInnerBlur.getHeight(),
mErasePaint);
srcDstCanvas.drawRect(0, 0, thickInnerBlur.getWidth(), -thickInnerBlurOffset[1],
mErasePaint);
// draw the inner and outer blur
srcDstCanvas.setBitmap(srcDst);
srcDstCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
mHolographicPaint.setColor(color);
srcDstCanvas.drawBitmap(thickInnerBlur, thickInnerBlurOffset[0], thickInnerBlurOffset[1],
mHolographicPaint);
srcDstCanvas.drawBitmap(thickOuterBlur, outerBlurOffset[0], outerBlurOffset[1],
mHolographicPaint);
// draw the bright outline
mHolographicPaint.setColor(outlineColor);
srcDstCanvas.drawBitmap(brightOutline, brightOutlineOffset[0], brightOutlineOffset[1],
mHolographicPaint);
// cleanup
srcDstCanvas.setBitmap(null);
brightOutline.recycle();
thickOuterBlur.recycle();
thickInnerBlur.recycle();
glowShape.recycle();
}
void applyExtraThickExpensiveOutlineWithBlur(Bitmap srcDst, Canvas srcDstCanvas, int color,
int outlineColor) {
applyExpensiveOutlineWithBlur(srcDst, srcDstCanvas, color, outlineColor, EXTRA_THICK);
}
void applyThickExpensiveOutlineWithBlur(Bitmap srcDst, Canvas srcDstCanvas, int color,
int outlineColor) {
applyExpensiveOutlineWithBlur(srcDst, srcDstCanvas, color, outlineColor, THICK);
}
void applyMediumExpensiveOutlineWithBlur(Bitmap srcDst, Canvas srcDstCanvas, int color,
int outlineColor, boolean clipAlpha) {
applyExpensiveOutlineWithBlur(srcDst, srcDstCanvas, color, outlineColor, clipAlpha,
MEDIUM);
}
void applyMediumExpensiveOutlineWithBlur(Bitmap srcDst, Canvas srcDstCanvas, int color,
int outlineColor) {
applyExpensiveOutlineWithBlur(srcDst, srcDstCanvas, color, outlineColor, MEDIUM);
}
}
|