blob: cb3b8efd6f319bd997a2bbfdfaf868401b1366fe [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;
Michael Jurka5f1c5092010-09-03 14:15:02 -070024import android.graphics.Paint;
25import android.graphics.PorterDuff;
26import android.util.AttributeSet;
27
Adam Cohenf34bab52010-09-30 14:11:56 -070028public class DimmableBubbleTextView extends BubbleTextView implements Dimmable {
Michael Jurka5f1c5092010-09-03 14:15:02 -070029 private Paint mDimmedPaint = new Paint();
30 private int mAlpha;
Michael Jurka5f1c5092010-09-03 14:15:02 -070031 private Bitmap mDimmedView;
32 private Canvas mDimmedViewCanvas;
33 private boolean isDimmedViewUpdatePass;
Adam Cohenf34bab52010-09-30 14:11:56 -070034 private float mDimmableProgress;
Michael Jurka5f1c5092010-09-03 14:15:02 -070035
36 public DimmableBubbleTextView(Context context) {
37 super(context);
38 mDimmedPaint.setFilterBitmap(true);
39 }
40
41 public DimmableBubbleTextView(Context context, AttributeSet attrs) {
42 super(context, attrs);
43 mDimmedPaint.setFilterBitmap(true);
44 }
45
46 public DimmableBubbleTextView(Context context, AttributeSet attrs, int defStyle) {
47 super(context, attrs, defStyle);
48 mDimmedPaint.setFilterBitmap(true);
49 }
50
Adam Cohenf34bab52010-09-30 14:11:56 -070051 public void setDimmableProgress(float progress) {
52 mDimmableProgress = progress;
Michael Jurka5f1c5092010-09-03 14:15:02 -070053 }
54
Adam Cohenf34bab52010-09-30 14:11:56 -070055 public float getDimmableProgress() {
56 return mDimmableProgress;
Michael Jurka5f1c5092010-09-03 14:15:02 -070057 }
58
59 @Override
60 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
61 super.onLayout(changed, left, top, right, bottom);
62
63 if (mDimmedView == null) {
64 isDimmedViewUpdatePass = true;
65 mDimmedView = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(),
66 Bitmap.Config.ARGB_8888);
67 mDimmedViewCanvas = new Canvas(mDimmedView);
68 mDimmedViewCanvas.concat(getMatrix());
69
70 draw(mDimmedViewCanvas);
71
72 // MAKE THE DIMMED VERSION
73 int dimmedColor = getContext().getResources().getColor(R.color.dimmed_view_color);
74 mDimmedViewCanvas.drawColor(dimmedColor, PorterDuff.Mode.SRC_IN);
75
76 isDimmedViewUpdatePass = false;
77 }
78 }
79
80 @Override
81 protected void onDraw(Canvas canvas) {
82 if (isDimmedViewUpdatePass) {
83 canvas.save();
84 final float alpha = getAlpha();
85 super.setAlpha(1.0f);
86 super.onDraw(canvas);
87 super.setAlpha(alpha);
88 canvas.restore();
89 } else {
Adam Cohenf34bab52010-09-30 14:11:56 -070090 super.onDraw(canvas);
Michael Jurka5f1c5092010-09-03 14:15:02 -070091 }
92
Adam Cohenf34bab52010-09-30 14:11:56 -070093 if (mDimmedView != null && mDimmableProgress > 0) {
94 mDimmedPaint.setAlpha((int) (mDimmableProgress * 255));
Michael Jurka5f1c5092010-09-03 14:15:02 -070095 canvas.drawBitmap(mDimmedView, mScrollX, mScrollY, mDimmedPaint);
96 }
97 }
98}