The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
Joe Onorato | a590252 | 2009-07-30 13:37:37 -0700 | [diff] [blame] | 17 | package com.android.launcher2; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 18 | |
| 19 | import android.widget.TextView; |
| 20 | import android.content.Context; |
| 21 | import android.util.AttributeSet; |
| 22 | import android.graphics.Canvas; |
| 23 | import android.graphics.Paint; |
| 24 | import android.graphics.RectF; |
| 25 | import android.graphics.drawable.Drawable; |
| 26 | import android.text.Layout; |
| 27 | |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 28 | import com.android.launcher.R; |
| 29 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 30 | /** |
| 31 | * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan |
| 32 | * because we want to make the bubble taller than the text and TextView's clip is |
| 33 | * too aggressive. |
| 34 | */ |
| 35 | public class BubbleTextView extends TextView { |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 36 | static final float CORNER_RADIUS = 8.0f; |
| 37 | static final float PADDING_H = 5.0f; |
| 38 | static final float PADDING_V = 1.0f; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 39 | |
| 40 | private final RectF mRect = new RectF(); |
| 41 | private Paint mPaint; |
Winson Chung | e22a8e9 | 2010-11-12 13:40:58 -0800 | [diff] [blame] | 42 | private int mPrevAlpha = -1; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 43 | |
| 44 | private boolean mBackgroundSizeChanged; |
| 45 | private Drawable mBackground; |
| 46 | private float mCornerRadius; |
| 47 | private float mPaddingH; |
| 48 | private float mPaddingV; |
| 49 | |
| 50 | public BubbleTextView(Context context) { |
| 51 | super(context); |
| 52 | init(); |
| 53 | } |
| 54 | |
| 55 | public BubbleTextView(Context context, AttributeSet attrs) { |
| 56 | super(context, attrs); |
| 57 | init(); |
| 58 | } |
| 59 | |
| 60 | public BubbleTextView(Context context, AttributeSet attrs, int defStyle) { |
| 61 | super(context, attrs, defStyle); |
| 62 | init(); |
| 63 | } |
| 64 | |
| 65 | private void init() { |
| 66 | setFocusable(true); |
| 67 | mBackground = getBackground(); |
| 68 | setBackgroundDrawable(null); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 69 | |
| 70 | mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 71 | mPaint.setColor(getContext().getResources().getColor(R.color.bubble_dark_background)); |
| 72 | |
| 73 | final float scale = getContext().getResources().getDisplayMetrics().density; |
| 74 | mCornerRadius = CORNER_RADIUS * scale; |
| 75 | mPaddingH = PADDING_H * scale; |
| 76 | //noinspection PointlessArithmeticExpression |
| 77 | mPaddingV = PADDING_V * scale; |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | protected boolean setFrame(int left, int top, int right, int bottom) { |
| 82 | if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) { |
| 83 | mBackgroundSizeChanged = true; |
| 84 | } |
| 85 | return super.setFrame(left, top, right, bottom); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | protected boolean verifyDrawable(Drawable who) { |
| 90 | return who == mBackground || super.verifyDrawable(who); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | protected void drawableStateChanged() { |
| 95 | Drawable d = mBackground; |
| 96 | if (d != null && d.isStateful()) { |
| 97 | d.setState(getDrawableState()); |
| 98 | } |
| 99 | super.drawableStateChanged(); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public void draw(Canvas canvas) { |
| 104 | final Drawable background = mBackground; |
| 105 | if (background != null) { |
| 106 | final int scrollX = mScrollX; |
| 107 | final int scrollY = mScrollY; |
| 108 | |
| 109 | if (mBackgroundSizeChanged) { |
| 110 | background.setBounds(0, 0, mRight - mLeft, mBottom - mTop); |
| 111 | mBackgroundSizeChanged = false; |
| 112 | } |
| 113 | |
| 114 | if ((scrollX | scrollY) == 0) { |
| 115 | background.draw(canvas); |
| 116 | } else { |
| 117 | canvas.translate(scrollX, scrollY); |
| 118 | background.draw(canvas); |
| 119 | canvas.translate(-scrollX, -scrollY); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | final Layout layout = getLayout(); |
| 124 | final RectF rect = mRect; |
| 125 | final int left = getCompoundPaddingLeft(); |
| 126 | final int top = getExtendedPaddingTop(); |
| 127 | |
| 128 | rect.set(left + layout.getLineLeft(0) - mPaddingH, |
| 129 | top + layout.getLineTop(0) - mPaddingV, |
| 130 | Math.min(left + layout.getLineRight(0) + mPaddingH, mScrollX + mRight - mLeft), |
| 131 | top + layout.getLineBottom(0) + mPaddingV); |
| 132 | canvas.drawRoundRect(rect, mCornerRadius, mCornerRadius, mPaint); |
| 133 | |
| 134 | super.draw(canvas); |
| 135 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 136 | |
| 137 | @Override |
| 138 | protected void onAttachedToWindow() { |
| 139 | super.onAttachedToWindow(); |
| 140 | mBackground.setCallback(this); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | protected void onDetachedFromWindow() { |
| 145 | super.onDetachedFromWindow(); |
| 146 | mBackground.setCallback(null); |
| 147 | } |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 148 | |
| 149 | @Override |
| 150 | protected boolean onSetAlpha(int alpha) { |
Winson Chung | e22a8e9 | 2010-11-12 13:40:58 -0800 | [diff] [blame] | 151 | if (mPrevAlpha != alpha) { |
| 152 | mPrevAlpha = alpha; |
| 153 | mPaint.setAlpha(alpha); |
| 154 | super.onSetAlpha(alpha); |
| 155 | } |
| 156 | return true; |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 157 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 158 | } |