blob: 621033748df55afedd19f9d31be634e8e7e7bb16 [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) {
62 final float pivot = 0.3f;
63 if (r < pivot) {
64 return Math.max(0.5f, 0.65f * cubic(r / pivot));
65 } else {
66 return Math.min(1.0f, 0.65f * cubic(1 - (r - pivot) / (1 - pivot)));
67 }
68 }
69
70 /**
71 * Returns the interpolated view alpha for the effect we want when scrolling pages.
72 */
73 public float viewAlphaInterpolator(float r) {
74 final float pivot = 0.6f;
75 if (r < pivot) {
76 return r / pivot;
77 } else {
78 return 1.0f;
79 }
80 }
81
82 /**
83 * Sets the color of the holographic paint to be used when applying the outline/blur.
84 */
85 void setColor(int color) {
86 mHolographicPaint.setColor(color);
87 }
88
89 /**
90 * Applies an outline to whatever is currently drawn in the specified bitmap.
91 */
92 void applyOutline(Bitmap srcDst, Canvas srcDstCanvas, float strokeWidth, PointF offset) {
93 strokeWidth *= mDensity;
94 Bitmap mask = srcDst.extractAlpha();
95 Matrix m = new Matrix();
96 final int width = srcDst.getWidth();
97 final int height = srcDst.getHeight();
98 float xScale = strokeWidth * 2.0f / width;
99 float yScale = strokeWidth * 2.0f / height;
100 m.preScale(1 + xScale, 1 + yScale, (width / 2.0f) + offset.x,
101 (height / 2.0f) + offset.y);
102
103 srcDstCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
104 srcDstCanvas.drawBitmap(mask, m, mHolographicPaint);
105 srcDstCanvas.drawBitmap(mask, mIdentity, mErasePaint);
106 mask.recycle();
107 }
108
109 /**
110 * Applies an blur to whatever is currently drawn in the specified bitmap.
111 */
112 void applyBlur(Bitmap srcDst, Canvas srcDstCanvas) {
113 int[] xy = new int[2];
114 Bitmap mask = srcDst.extractAlpha(mBlurPaint, xy);
115 srcDstCanvas.drawBitmap(mask, xy[0], xy[1], mHolographicPaint);
116 mask.recycle();
117 }
118}