blob: 597a725fa9d633ed467731b3b470eb2726089eb5 [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 public static float DEFAULT_STROKE_WIDTH = 6.0f;
39
40 HolographicOutlineHelper() {
Michael Jurka5f1c5092010-09-03 14:15:02 -070041 mHolographicPaint.setFilterBitmap(true);
42 mHolographicPaint.setAntiAlias(true);
Winson Chung64a3cd42010-09-17 16:47:33 -070043 mExpensiveBlurPaint.setFilterBitmap(true);
Michael Jurka5f1c5092010-09-03 14:15:02 -070044 mErasePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
45 mErasePaint.setFilterBitmap(true);
46 mErasePaint.setAntiAlias(true);
47 }
48
Michael Jurka5f1c5092010-09-03 14:15:02 -070049 /**
50 * Returns the interpolated holographic highlight alpha for the effect we want when scrolling
51 * pages.
52 */
53 public float highlightAlphaInterpolator(float r) {
Winson Chung64a3cd42010-09-17 16:47:33 -070054 float maxAlpha = 0.8f;
Winson Chunge8878e32010-09-15 20:37:09 -070055 return (float) Math.pow(maxAlpha * (1.0f - r), 1.5f);
Michael Jurka5f1c5092010-09-03 14:15:02 -070056 }
57
58 /**
59 * Returns the interpolated view alpha for the effect we want when scrolling pages.
60 */
61 public float viewAlphaInterpolator(float r) {
Winson Chunge8878e32010-09-15 20:37:09 -070062 final float pivot = 0.95f;
Michael Jurka5f1c5092010-09-03 14:15:02 -070063 if (r < pivot) {
Winson Chunge8878e32010-09-15 20:37:09 -070064 return (float) Math.pow(r / pivot, 1.5f);
Michael Jurka5f1c5092010-09-03 14:15:02 -070065 } else {
66 return 1.0f;
67 }
68 }
69
70 /**
Winson Chung64a3cd42010-09-17 16:47:33 -070071 * Applies a more expensive and accurate outline to whatever is currently drawn in a specified
72 * bitmap.
Michael Jurka5f1c5092010-09-03 14:15:02 -070073 */
Winson Chung64a3cd42010-09-17 16:47:33 -070074 void applyExpensiveOutlineWithBlur(Bitmap srcDst, Canvas srcDstCanvas, int color,
75 int outlineColor) {
76 // calculate the outer blur first
77 mExpensiveBlurPaint.setMaskFilter(mThickOuterBlurMaskFilter);
78 int[] outerBlurOffset = new int[2];
79 Bitmap thickOuterBlur = srcDst.extractAlpha(mExpensiveBlurPaint, outerBlurOffset);
80 mExpensiveBlurPaint.setMaskFilter(mThinOuterBlurMaskFilter);
81 int[] thinOuterBlurOffset = new int[2];
82 Bitmap thinOuterBlur = srcDst.extractAlpha(mExpensiveBlurPaint, thinOuterBlurOffset);
83
84 // calculate the inner blur
85 srcDstCanvas.drawColor(0xFF000000, PorterDuff.Mode.SRC_OUT);
86 mExpensiveBlurPaint.setMaskFilter(mThickInnerBlurMaskFilter);
87 int[] thickInnerBlurOffset = new int[2];
88 Bitmap thickInnerBlur = srcDst.extractAlpha(mExpensiveBlurPaint, thickInnerBlurOffset);
89
90 // mask out the inner blur
91 srcDstCanvas.setBitmap(thickInnerBlur);
92 srcDstCanvas.drawBitmap(srcDst, -thickInnerBlurOffset[0],
93 -thickInnerBlurOffset[1], mErasePaint);
94 srcDstCanvas.drawRect(0, 0, -thickInnerBlurOffset[0], thickInnerBlur.getHeight(),
95 mErasePaint);
96 srcDstCanvas.drawRect(0, 0, thickInnerBlur.getWidth(), -thickInnerBlurOffset[1],
97 mErasePaint);
98
99 // draw the inner and outer blur
100 srcDstCanvas.setBitmap(srcDst);
101 srcDstCanvas.drawColor(0x00000000, PorterDuff.Mode.CLEAR);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700102 mHolographicPaint.setColor(color);
Winson Chung64a3cd42010-09-17 16:47:33 -0700103 srcDstCanvas.drawBitmap(thickInnerBlur, thickInnerBlurOffset[0], thickInnerBlurOffset[1],
104 mHolographicPaint);
105 srcDstCanvas.drawBitmap(thickOuterBlur, outerBlurOffset[0], outerBlurOffset[1],
106 mHolographicPaint);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700107
Winson Chung64a3cd42010-09-17 16:47:33 -0700108 // draw the bright outline
109 mHolographicPaint.setColor(outlineColor);
110 srcDstCanvas.drawBitmap(thinOuterBlur, thinOuterBlurOffset[0], thinOuterBlurOffset[1],
111 mHolographicPaint);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700112
Winson Chung64a3cd42010-09-17 16:47:33 -0700113 // cleanup
114 thinOuterBlur.recycle();
115 thickOuterBlur.recycle();
116 thickInnerBlur.recycle();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700117 }
118}