Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.launcher2; |
| 18 | |
| 19 | import com.android.launcher.R; |
| 20 | |
| 21 | import android.content.Context; |
| 22 | import android.graphics.Bitmap; |
| 23 | import android.graphics.Canvas; |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 24 | import android.graphics.Paint; |
| 25 | import android.graphics.PorterDuff; |
| 26 | import android.util.AttributeSet; |
| 27 | |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 28 | public class DimmableBubbleTextView extends BubbleTextView implements Dimmable { |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 29 | private Paint mDimmedPaint = new Paint(); |
| 30 | private int mAlpha; |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 31 | private Bitmap mDimmedView; |
| 32 | private Canvas mDimmedViewCanvas; |
| 33 | private boolean isDimmedViewUpdatePass; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 34 | private float mDimmableProgress; |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 35 | |
| 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 Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 51 | public void setDimmableProgress(float progress) { |
| 52 | mDimmableProgress = progress; |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 55 | public float getDimmableProgress() { |
| 56 | return mDimmableProgress; |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 57 | } |
| 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 Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 90 | super.onDraw(canvas); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 93 | if (mDimmedView != null && mDimmableProgress > 0) { |
| 94 | mDimmedPaint.setAlpha((int) (mDimmableProgress * 255)); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 95 | canvas.drawBitmap(mDimmedView, mScrollX, mScrollY, mDimmedPaint); |
| 96 | } |
| 97 | } |
| 98 | } |