blob: d6c5484b816eb98eb3bdb3fbe569cc2954df6557 [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;
22import android.graphics.Matrix;
23import android.graphics.Paint;
24import android.graphics.PointF;
25import android.graphics.PorterDuff;
26import android.graphics.PorterDuffXfermode;
27
28public class HolographicOutlineHelper {
29 private float mDensity;
30 private final Paint mHolographicPaint = new Paint();
31 private final Paint mBlurPaint = new Paint();
32 private final Paint mErasePaint = new Paint();
33 private static final Matrix mIdentity = new Matrix();;
34 private static final float BLUR_FACTOR = 3.5f;
35
36 public static final float DEFAULT_STROKE_WIDTH = 6.0f;
37 public static final int HOLOGRAPHIC_BLUE = 0xFF6699FF;
38 public static final int HOLOGRAPHIC_GREEN = 0xFF51E633;
39
40 HolographicOutlineHelper(float density) {
41 mDensity = density;
42 mHolographicPaint.setColor(HOLOGRAPHIC_BLUE);
43 mHolographicPaint.setFilterBitmap(true);
44 mHolographicPaint.setAntiAlias(true);
45 mBlurPaint.setMaskFilter(new BlurMaskFilter(BLUR_FACTOR * density,
46 BlurMaskFilter.Blur.OUTER));
47 mBlurPaint.setFilterBitmap(true);
48 mErasePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
49 mErasePaint.setFilterBitmap(true);
50 mErasePaint.setAntiAlias(true);
51 }
52
53 private float cubic(float r) {
54 return (float) (Math.pow(r - 1, 3) + 1);
55 }
56
57 /**
58 * Returns the interpolated holographic highlight alpha for the effect we want when scrolling
59 * pages.
60 */
61 public float highlightAlphaInterpolator(float r) {
Winson Chunge8878e32010-09-15 20:37:09 -070062 float maxAlpha = 0.6f;
63 return (float) Math.pow(maxAlpha * (1.0f - r), 1.5f);
Michael Jurka5f1c5092010-09-03 14:15:02 -070064 }
65
66 /**
67 * Returns the interpolated view alpha for the effect we want when scrolling pages.
68 */
69 public float viewAlphaInterpolator(float r) {
Winson Chunge8878e32010-09-15 20:37:09 -070070 final float pivot = 0.95f;
Michael Jurka5f1c5092010-09-03 14:15:02 -070071 if (r < pivot) {
Winson Chunge8878e32010-09-15 20:37:09 -070072 return (float) Math.pow(r / pivot, 1.5f);
Michael Jurka5f1c5092010-09-03 14:15:02 -070073 } else {
74 return 1.0f;
75 }
76 }
77
78 /**
79 * Sets the color of the holographic paint to be used when applying the outline/blur.
80 */
81 void setColor(int color) {
82 mHolographicPaint.setColor(color);
83 }
84
85 /**
86 * Applies an outline to whatever is currently drawn in the specified bitmap.
87 */
88 void applyOutline(Bitmap srcDst, Canvas srcDstCanvas, float strokeWidth, PointF offset) {
89 strokeWidth *= mDensity;
90 Bitmap mask = srcDst.extractAlpha();
91 Matrix m = new Matrix();
92 final int width = srcDst.getWidth();
93 final int height = srcDst.getHeight();
94 float xScale = strokeWidth * 2.0f / width;
95 float yScale = strokeWidth * 2.0f / height;
96 m.preScale(1 + xScale, 1 + yScale, (width / 2.0f) + offset.x,
97 (height / 2.0f) + offset.y);
98
99 srcDstCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
100 srcDstCanvas.drawBitmap(mask, m, mHolographicPaint);
101 srcDstCanvas.drawBitmap(mask, mIdentity, mErasePaint);
102 mask.recycle();
103 }
104
105 /**
106 * Applies an blur to whatever is currently drawn in the specified bitmap.
107 */
108 void applyBlur(Bitmap srcDst, Canvas srcDstCanvas) {
109 int[] xy = new int[2];
110 Bitmap mask = srcDst.extractAlpha(mBlurPaint, xy);
111 srcDstCanvas.drawBitmap(mask, xy[0], xy[1], mHolographicPaint);
112 mask.recycle();
113 }
114}