blob: 9e61b79d0cb21f87ab04c14318438cc14255005d [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;
Michael Jurka5f1c5092010-09-03 14:15:02 -070022import android.graphics.Paint;
Michael Jurka5f1c5092010-09-03 14:15:02 -070023import android.graphics.PorterDuff;
24import android.graphics.PorterDuffXfermode;
25
26public class HolographicOutlineHelper {
Michael Jurka5f1c5092010-09-03 14:15:02 -070027 private final Paint mHolographicPaint = new Paint();
Winson Chung64a3cd42010-09-17 16:47:33 -070028 private final Paint mExpensiveBlurPaint = new Paint();
Michael Jurka5f1c5092010-09-03 14:15:02 -070029 private final Paint mErasePaint = new Paint();
Michael Jurka5f1c5092010-09-03 14:15:02 -070030
Winson Chung64a3cd42010-09-17 16:47:33 -070031 private static final BlurMaskFilter mThickOuterBlurMaskFilter = new BlurMaskFilter(6.0f,
32 BlurMaskFilter.Blur.OUTER);
33 private static final BlurMaskFilter mThinOuterBlurMaskFilter = new BlurMaskFilter(1.0f,
34 BlurMaskFilter.Blur.OUTER);
35 private static final BlurMaskFilter mThickInnerBlurMaskFilter = new BlurMaskFilter(4.0f,
36 BlurMaskFilter.Blur.NORMAL);
Michael Jurka5f1c5092010-09-03 14:15:02 -070037
Winson Chung64a3cd42010-09-17 16:47:33 -070038 HolographicOutlineHelper() {
Michael Jurka5f1c5092010-09-03 14:15:02 -070039 mHolographicPaint.setFilterBitmap(true);
40 mHolographicPaint.setAntiAlias(true);
Winson Chung64a3cd42010-09-17 16:47:33 -070041 mExpensiveBlurPaint.setFilterBitmap(true);
Michael Jurka5f1c5092010-09-03 14:15:02 -070042 mErasePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
43 mErasePaint.setFilterBitmap(true);
44 mErasePaint.setAntiAlias(true);
45 }
46
Michael Jurka5f1c5092010-09-03 14:15:02 -070047 /**
48 * Returns the interpolated holographic highlight alpha for the effect we want when scrolling
49 * pages.
50 */
51 public float highlightAlphaInterpolator(float r) {
Winson Chung64a3cd42010-09-17 16:47:33 -070052 float maxAlpha = 0.8f;
Winson Chunge8878e32010-09-15 20:37:09 -070053 return (float) Math.pow(maxAlpha * (1.0f - r), 1.5f);
Michael Jurka5f1c5092010-09-03 14:15:02 -070054 }
55
56 /**
57 * Returns the interpolated view alpha for the effect we want when scrolling pages.
58 */
59 public float viewAlphaInterpolator(float r) {
Winson Chunge8878e32010-09-15 20:37:09 -070060 final float pivot = 0.95f;
Michael Jurka5f1c5092010-09-03 14:15:02 -070061 if (r < pivot) {
Winson Chunge8878e32010-09-15 20:37:09 -070062 return (float) Math.pow(r / pivot, 1.5f);
Michael Jurka5f1c5092010-09-03 14:15:02 -070063 } else {
64 return 1.0f;
65 }
66 }
67
68 /**
Winson Chung64a3cd42010-09-17 16:47:33 -070069 * Applies a more expensive and accurate outline to whatever is currently drawn in a specified
70 * bitmap.
Michael Jurka5f1c5092010-09-03 14:15:02 -070071 */
Winson Chung64a3cd42010-09-17 16:47:33 -070072 void applyExpensiveOutlineWithBlur(Bitmap srcDst, Canvas srcDstCanvas, int color,
73 int outlineColor) {
74 // calculate the outer blur first
75 mExpensiveBlurPaint.setMaskFilter(mThickOuterBlurMaskFilter);
76 int[] outerBlurOffset = new int[2];
77 Bitmap thickOuterBlur = srcDst.extractAlpha(mExpensiveBlurPaint, outerBlurOffset);
78 mExpensiveBlurPaint.setMaskFilter(mThinOuterBlurMaskFilter);
79 int[] thinOuterBlurOffset = new int[2];
80 Bitmap thinOuterBlur = srcDst.extractAlpha(mExpensiveBlurPaint, thinOuterBlurOffset);
81
82 // calculate the inner blur
83 srcDstCanvas.drawColor(0xFF000000, PorterDuff.Mode.SRC_OUT);
84 mExpensiveBlurPaint.setMaskFilter(mThickInnerBlurMaskFilter);
85 int[] thickInnerBlurOffset = new int[2];
86 Bitmap thickInnerBlur = srcDst.extractAlpha(mExpensiveBlurPaint, thickInnerBlurOffset);
87
88 // mask out the inner blur
89 srcDstCanvas.setBitmap(thickInnerBlur);
90 srcDstCanvas.drawBitmap(srcDst, -thickInnerBlurOffset[0],
91 -thickInnerBlurOffset[1], mErasePaint);
92 srcDstCanvas.drawRect(0, 0, -thickInnerBlurOffset[0], thickInnerBlur.getHeight(),
93 mErasePaint);
94 srcDstCanvas.drawRect(0, 0, thickInnerBlur.getWidth(), -thickInnerBlurOffset[1],
95 mErasePaint);
96
97 // draw the inner and outer blur
98 srcDstCanvas.setBitmap(srcDst);
99 srcDstCanvas.drawColor(0x00000000, PorterDuff.Mode.CLEAR);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700100 mHolographicPaint.setColor(color);
Winson Chung64a3cd42010-09-17 16:47:33 -0700101 srcDstCanvas.drawBitmap(thickInnerBlur, thickInnerBlurOffset[0], thickInnerBlurOffset[1],
102 mHolographicPaint);
103 srcDstCanvas.drawBitmap(thickOuterBlur, outerBlurOffset[0], outerBlurOffset[1],
104 mHolographicPaint);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700105
Winson Chung64a3cd42010-09-17 16:47:33 -0700106 // draw the bright outline
107 mHolographicPaint.setColor(outlineColor);
108 srcDstCanvas.drawBitmap(thinOuterBlur, thinOuterBlurOffset[0], thinOuterBlurOffset[1],
109 mHolographicPaint);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700110
Winson Chung64a3cd42010-09-17 16:47:33 -0700111 // cleanup
112 thinOuterBlur.recycle();
113 thickOuterBlur.recycle();
114 thickInnerBlur.recycle();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700115 }
116}