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; |
| 24 | import android.graphics.Color; |
| 25 | import android.graphics.Paint; |
| 26 | import android.graphics.PorterDuff; |
| 27 | import android.util.AttributeSet; |
| 28 | |
| 29 | public class DimmableBubbleTextView extends BubbleTextView { |
| 30 | private Paint mDimmedPaint = new Paint(); |
| 31 | private int mAlpha; |
| 32 | private int mDimmedAlpha; |
| 33 | private Bitmap mDimmedView; |
| 34 | private Canvas mDimmedViewCanvas; |
| 35 | private boolean isDimmedViewUpdatePass; |
| 36 | |
| 37 | public DimmableBubbleTextView(Context context) { |
| 38 | super(context); |
| 39 | mDimmedPaint.setFilterBitmap(true); |
| 40 | } |
| 41 | |
| 42 | public DimmableBubbleTextView(Context context, AttributeSet attrs) { |
| 43 | super(context, attrs); |
| 44 | mDimmedPaint.setFilterBitmap(true); |
| 45 | } |
| 46 | |
| 47 | public DimmableBubbleTextView(Context context, AttributeSet attrs, int defStyle) { |
| 48 | super(context, attrs, defStyle); |
| 49 | mDimmedPaint.setFilterBitmap(true); |
| 50 | } |
| 51 | |
| 52 | private static float cubic(float r) { |
| 53 | return (float) (Math.pow(r-1, 3) + 1); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Returns the interpolated holographic highlight alpha for the effect we want when scrolling |
| 58 | * pages. |
| 59 | */ |
| 60 | public static float highlightAlphaInterpolator(float r) { |
| 61 | final float pivot = 0.3f; |
| 62 | if (r < pivot) { |
| 63 | return Math.max(0.5f, 0.65f*cubic(r/pivot)); |
| 64 | } else { |
| 65 | return Math.min(1.0f, 0.65f*cubic(1 - (r-pivot)/(1-pivot))); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Returns the interpolated view alpha for the effect we want when scrolling pages. |
| 71 | */ |
| 72 | public static float viewAlphaInterpolator(float r) { |
| 73 | final float pivot = 0.6f; |
| 74 | if (r < pivot) { |
| 75 | return r/pivot; |
| 76 | } else { |
| 77 | return 1.0f; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public boolean onSetAlpha(int alpha) { |
| 83 | super.onSetAlpha(alpha); |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public void setAlpha(float alpha) { |
| 89 | final float viewAlpha = viewAlphaInterpolator(alpha); |
| 90 | final float dimmedAlpha = highlightAlphaInterpolator(alpha); |
| 91 | mAlpha = (int) (viewAlpha * 255); |
| 92 | mDimmedAlpha = (int) (dimmedAlpha * 255); |
| 93 | super.setAlpha(viewAlpha); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 98 | super.onLayout(changed, left, top, right, bottom); |
| 99 | |
| 100 | if (mDimmedView == null) { |
| 101 | isDimmedViewUpdatePass = true; |
| 102 | mDimmedView = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), |
| 103 | Bitmap.Config.ARGB_8888); |
| 104 | mDimmedViewCanvas = new Canvas(mDimmedView); |
| 105 | mDimmedViewCanvas.concat(getMatrix()); |
| 106 | |
| 107 | draw(mDimmedViewCanvas); |
| 108 | |
| 109 | // MAKE THE DIMMED VERSION |
| 110 | int dimmedColor = getContext().getResources().getColor(R.color.dimmed_view_color); |
| 111 | mDimmedViewCanvas.drawColor(dimmedColor, PorterDuff.Mode.SRC_IN); |
| 112 | |
| 113 | isDimmedViewUpdatePass = false; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | protected void onDraw(Canvas canvas) { |
| 119 | if (isDimmedViewUpdatePass) { |
| 120 | canvas.save(); |
| 121 | final float alpha = getAlpha(); |
| 122 | super.setAlpha(1.0f); |
| 123 | super.onDraw(canvas); |
| 124 | super.setAlpha(alpha); |
| 125 | canvas.restore(); |
| 126 | } else { |
| 127 | if (mAlpha > 0) { |
| 128 | super.onDraw(canvas); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | if (mDimmedView != null && mDimmedAlpha > 0) { |
| 133 | mDimmedPaint.setAlpha(mDimmedAlpha); |
| 134 | canvas.drawBitmap(mDimmedView, mScrollX, mScrollY, mDimmedPaint); |
| 135 | } |
| 136 | } |
| 137 | } |