blob: ea4b01a4ccc9f3e77c70180e7786374f6d11994e [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.Rect;
27import android.graphics.TableMaskFilter;
Michael Jurka5f1c5092010-09-03 14:15:02 -070028
29public class HolographicOutlineHelper {
Michael Jurka5f1c5092010-09-03 14:15:02 -070030 private final Paint mHolographicPaint = new Paint();
Joe Onorato4be866d2010-10-10 11:26:02 -070031 private final Paint mBlurPaint = new Paint();
Michael Jurka5f1c5092010-09-03 14:15:02 -070032 private final Paint mErasePaint = new Paint();
Michael Jurka5f1c5092010-09-03 14:15:02 -070033
Joe Onorato4be866d2010-10-10 11:26:02 -070034 private static final BlurMaskFilter sLargeGlowBlurMaskFilter = new BlurMaskFilter(
35 10.0f, BlurMaskFilter.Blur.OUTER);
36 private static final BlurMaskFilter sThickOuterBlurMaskFilter = new BlurMaskFilter(
37 6.0f, BlurMaskFilter.Blur.OUTER);
38 private static final BlurMaskFilter sMediumOuterBlurMaskFilter = new BlurMaskFilter(
39 2.0f, BlurMaskFilter.Blur.OUTER);
40 private static final BlurMaskFilter sThinOuterBlurMaskFilter = new BlurMaskFilter(
41 1.0f, BlurMaskFilter.Blur.OUTER);
42
43 private static final BlurMaskFilter sThickInnerBlurMaskFilter = new BlurMaskFilter(
44 4.0f, BlurMaskFilter.Blur.NORMAL);
45 private static final BlurMaskFilter sThinInnerBlurMaskFilter = new BlurMaskFilter(
46 1.0f, BlurMaskFilter.Blur.INNER);
47
48 private static final MaskFilter sFineClipTable = TableMaskFilter.CreateClipTable(0, 20);
49 private static final MaskFilter sCoarseClipTable = TableMaskFilter.CreateClipTable(0, 200);
50
51 private int[] mTempOffset = new int[2];
Michael Jurka5f1c5092010-09-03 14:15:02 -070052
Winson Chung64a3cd42010-09-17 16:47:33 -070053 HolographicOutlineHelper() {
Michael Jurka5f1c5092010-09-03 14:15:02 -070054 mHolographicPaint.setFilterBitmap(true);
55 mHolographicPaint.setAntiAlias(true);
Joe Onorato4be866d2010-10-10 11:26:02 -070056 mBlurPaint.setFilterBitmap(true);
57 mBlurPaint.setAntiAlias(true);
Michael Jurka5f1c5092010-09-03 14:15:02 -070058 mErasePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
59 mErasePaint.setFilterBitmap(true);
60 mErasePaint.setAntiAlias(true);
61 }
62
Michael Jurka5f1c5092010-09-03 14:15:02 -070063 /**
64 * Returns the interpolated holographic highlight alpha for the effect we want when scrolling
65 * pages.
66 */
67 public float highlightAlphaInterpolator(float r) {
Winson Chung64a3cd42010-09-17 16:47:33 -070068 float maxAlpha = 0.8f;
Winson Chunge8878e32010-09-15 20:37:09 -070069 return (float) Math.pow(maxAlpha * (1.0f - r), 1.5f);
Michael Jurka5f1c5092010-09-03 14:15:02 -070070 }
71
72 /**
73 * Returns the interpolated view alpha for the effect we want when scrolling pages.
74 */
75 public float viewAlphaInterpolator(float r) {
Winson Chunge8878e32010-09-15 20:37:09 -070076 final float pivot = 0.95f;
Michael Jurka5f1c5092010-09-03 14:15:02 -070077 if (r < pivot) {
Winson Chunge8878e32010-09-15 20:37:09 -070078 return (float) Math.pow(r / pivot, 1.5f);
Michael Jurka5f1c5092010-09-03 14:15:02 -070079 } else {
80 return 1.0f;
81 }
82 }
83
Joe Onorato4be866d2010-10-10 11:26:02 -070084 void applyGlow(Bitmap bitmap, Canvas canvas, int color) {
85 mBlurPaint.setMaskFilter(sThickOuterBlurMaskFilter);
86 Bitmap glow = bitmap.extractAlpha(mBlurPaint, mTempOffset);
87
88 // Use the clip table to make the glow heavier closer to the outline
89 mHolographicPaint.setMaskFilter(sCoarseClipTable);
90 mHolographicPaint.setAlpha(150);
91 mHolographicPaint.setColor(color);
92 canvas.drawBitmap(glow, mTempOffset[0], mTempOffset[1], mHolographicPaint);
93 glow.recycle();
94 }
95
96 /**
97 * Draws a solid outline around a bitmap, erasing the original pixels.
98 *
99 * @param bitmap The bitmap to modify
100 * @param canvas A canvas on the bitmap
101 * @param color The color to draw the outline and glow in
102 * @param removeOrig If true, punch out the original pixels to just leave the outline
103 */
104 void applyExpensiveOuterOutline(Bitmap bitmap, Canvas canvas, int color, boolean removeOrig) {
105 Bitmap originalImage = null;
106 if (removeOrig) {
107 originalImage = bitmap.extractAlpha();
108 }
109
110 // Compute an outer blur on the original bitmap
111 mBlurPaint.setMaskFilter(sMediumOuterBlurMaskFilter);
112 Bitmap outline = bitmap.extractAlpha(mBlurPaint, mTempOffset);
113
114 // Paint the blurred bitmap back into the canvas. Using the clip table causes any alpha
115 // pixels above a certain threshold to be rounded up to be fully opaque. This gives the
116 // effect of a thick outline, with a slight blur on the edge
117 mHolographicPaint.setColor(color);
118 mHolographicPaint.setMaskFilter(sFineClipTable);
119 canvas.drawBitmap(outline, mTempOffset[0], mTempOffset[1], mHolographicPaint);
120 outline.recycle();
121
122 if (removeOrig) {
123 // Finally, punch out the original pixels, leaving just the outline
124 canvas.drawBitmap(originalImage, 0, 0, mErasePaint);
125 originalImage.recycle();
126 }
127 }
128
Michael Jurka5f1c5092010-09-03 14:15:02 -0700129 /**
Winson Chung64a3cd42010-09-17 16:47:33 -0700130 * Applies a more expensive and accurate outline to whatever is currently drawn in a specified
131 * bitmap.
Michael Jurka5f1c5092010-09-03 14:15:02 -0700132 */
Winson Chung64a3cd42010-09-17 16:47:33 -0700133 void applyExpensiveOutlineWithBlur(Bitmap srcDst, Canvas srcDstCanvas, int color,
134 int outlineColor) {
135 // calculate the outer blur first
Joe Onorato4be866d2010-10-10 11:26:02 -0700136 mBlurPaint.setMaskFilter(sThickOuterBlurMaskFilter);
Winson Chung64a3cd42010-09-17 16:47:33 -0700137 int[] outerBlurOffset = new int[2];
Joe Onorato4be866d2010-10-10 11:26:02 -0700138 Bitmap thickOuterBlur = srcDst.extractAlpha(mBlurPaint, outerBlurOffset);
139 mBlurPaint.setMaskFilter(sThinOuterBlurMaskFilter);
Winson Chung64a3cd42010-09-17 16:47:33 -0700140 int[] thinOuterBlurOffset = new int[2];
Joe Onorato4be866d2010-10-10 11:26:02 -0700141 Bitmap thinOuterBlur = srcDst.extractAlpha(mBlurPaint, thinOuterBlurOffset);
Winson Chung64a3cd42010-09-17 16:47:33 -0700142
143 // calculate the inner blur
144 srcDstCanvas.drawColor(0xFF000000, PorterDuff.Mode.SRC_OUT);
Joe Onorato4be866d2010-10-10 11:26:02 -0700145 mBlurPaint.setMaskFilter(sThickInnerBlurMaskFilter);
Winson Chung64a3cd42010-09-17 16:47:33 -0700146 int[] thickInnerBlurOffset = new int[2];
Joe Onorato4be866d2010-10-10 11:26:02 -0700147 Bitmap thickInnerBlur = srcDst.extractAlpha(mBlurPaint, thickInnerBlurOffset);
Winson Chung64a3cd42010-09-17 16:47:33 -0700148
149 // mask out the inner blur
150 srcDstCanvas.setBitmap(thickInnerBlur);
151 srcDstCanvas.drawBitmap(srcDst, -thickInnerBlurOffset[0],
152 -thickInnerBlurOffset[1], mErasePaint);
153 srcDstCanvas.drawRect(0, 0, -thickInnerBlurOffset[0], thickInnerBlur.getHeight(),
154 mErasePaint);
155 srcDstCanvas.drawRect(0, 0, thickInnerBlur.getWidth(), -thickInnerBlurOffset[1],
156 mErasePaint);
157
158 // draw the inner and outer blur
159 srcDstCanvas.setBitmap(srcDst);
160 srcDstCanvas.drawColor(0x00000000, PorterDuff.Mode.CLEAR);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700161 mHolographicPaint.setColor(color);
Winson Chung64a3cd42010-09-17 16:47:33 -0700162 srcDstCanvas.drawBitmap(thickInnerBlur, thickInnerBlurOffset[0], thickInnerBlurOffset[1],
163 mHolographicPaint);
164 srcDstCanvas.drawBitmap(thickOuterBlur, outerBlurOffset[0], outerBlurOffset[1],
165 mHolographicPaint);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700166
Winson Chung64a3cd42010-09-17 16:47:33 -0700167 // draw the bright outline
168 mHolographicPaint.setColor(outlineColor);
169 srcDstCanvas.drawBitmap(thinOuterBlur, thinOuterBlurOffset[0], thinOuterBlurOffset[1],
170 mHolographicPaint);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700171
Winson Chung64a3cd42010-09-17 16:47:33 -0700172 // cleanup
173 thinOuterBlur.recycle();
174 thickOuterBlur.recycle();
175 thickInnerBlur.recycle();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700176 }
177}