blob: 7a1d4f9daf438a0fc1b70c6e818f3e701726dc94 [file] [log] [blame]
Michael Jurka5f1c5092010-09-03 14:15:02 -07001/*
2 * Copyright (C) 2010 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 com.android.launcher.R;
20
21import android.content.Context;
22import android.graphics.Bitmap;
23import android.graphics.Canvas;
24import android.graphics.Color;
25import android.graphics.Paint;
26import android.graphics.PorterDuff;
27import android.view.View;
28
29public class DimmableAppWidgetHostView extends LauncherAppWidgetHostView {
30 public DimmableAppWidgetHostView(Context context) {
31 super(context);
32 mPaint.setFilterBitmap(true);
33 }
34
35 private final Paint mPaint = new Paint();
36 private int mAlpha;
37 private int mDimmedAlpha;
38 private Bitmap mDimmedView;
39 private Canvas mDimmedViewCanvas;
40 private boolean isDimmedViewUpdatePass;
41
42 private static float cubic(float r) {
43 return (float) (Math.pow(r-1, 3) + 1);
44 }
45
46 /**
47 * Returns the interpolated holographic highlight alpha for the effect we want when scrolling
48 * pages.
49 */
50 public static float highlightAlphaInterpolator(float r) {
51 final float pivot = 0.3f;
52 if (r < pivot) {
53 return Math.max(0.5f, 0.65f*cubic(r/pivot));
54 } else {
55 return Math.min(1.0f, 0.65f*cubic(1 - (r-pivot)/(1-pivot)));
56 }
57 }
58
59 /**
60 * Returns the interpolated view alpha for the effect we want when scrolling pages.
61 */
62 public static float viewAlphaInterpolator(float r) {
63 final float pivot = 0.6f;
64 if (r < pivot) {
65 return r/pivot;
66 } else {
67 return 1.0f;
68 }
69 }
70
71 private void setChildAlpha(float alpha) {
72 if (getChildCount() > 0) {
73 final View child = getChildAt(0);
74 if (child.getAlpha() != alpha) {
75 getChildAt(0).setAlpha(alpha);
76 }
77 }
78 }
79
80 private void updateChildAlpha() {
81 // hacky, but sometimes widgets get their alpha set back to 1.0f, so we call
82 // this to force them back
83 setChildAlpha(getAlpha());
84 }
85
86 @Override
87 public boolean onSetAlpha(int alpha) {
88 super.onSetAlpha(alpha);
89 return true;
90 }
91
92 @Override
93 public void setAlpha(float alpha) {
94 final float viewAlpha = viewAlphaInterpolator(alpha);
95 final float dimmedAlpha = highlightAlphaInterpolator(alpha);
96 mAlpha = (int) (viewAlpha * 255);
97 mDimmedAlpha = (int) (dimmedAlpha * 255);
98 super.setAlpha(viewAlpha);
99 setChildAlpha(viewAlpha);
100 }
101
102 private void updateDimmedView() {
103 if (mDimmedView == null) {
104 mDimmedView = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(),
105 Bitmap.Config.ARGB_8888);
106 mDimmedViewCanvas = new Canvas(mDimmedView);
107 }
108 mDimmedViewCanvas.drawColor(0x00000000);
109 mDimmedViewCanvas.concat(getMatrix());
110 isDimmedViewUpdatePass = true;
111 draw(mDimmedViewCanvas);
112 // make the bitmap look "dimmed"
113 int dimmedColor = getContext().getResources().getColor(R.color.dimmed_view_color);
114 mDimmedViewCanvas.drawColor(dimmedColor, PorterDuff.Mode.SRC_IN);
115 isDimmedViewUpdatePass = false;
116 }
117
118 @Override
119 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
120 super.onLayout(changed, left, top, right, bottom);
121
122 if (mDimmedView == null && mDimmedAlpha > 0.0f) {
123 updateDimmedView();
124 }
125 }
126
127 @Override
128 public void dispatchDraw(Canvas canvas) {
129 if (isDimmedViewUpdatePass) {
130 final float alpha = getAlpha();
131 canvas.save();
132 setAlpha(1.0f);
133 super.dispatchDraw(canvas);
134 canvas.restore();
135 setAlpha(alpha);
136 } else {
137 if (mDimmedView != null && mDimmedAlpha > 0) {
138 // draw the dimmed version of this widget
139 mPaint.setAlpha(mDimmedAlpha);
140 canvas.drawBitmap(mDimmedView, 0, 0, mPaint);
141 }
142
143 updateChildAlpha();
144 super.dispatchDraw(canvas);
145 }
146 }
147}