The Android Open Source Project | c8f00b6 | 2008-10-21 07:00:00 -0700 | [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 | |
| 17 | package com.android.launcher; |
| 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; |
The Android Open Source Project | d097a18 | 2008-12-17 18:05:58 -0800 | [diff] [blame] | 25 | import android.graphics.drawable.Drawable; |
The Android Open Source Project | c8f00b6 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 26 | import android.text.Layout; |
| 27 | |
| 28 | /** |
| 29 | * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan |
| 30 | * because we want to make the bubble taller than the text and TextView's clip is |
| 31 | * too aggressive. |
| 32 | */ |
| 33 | public class BubbleTextView extends TextView { |
| 34 | private static final float CORNER_RADIUS = 8.0f; |
| 35 | private static final float PADDING_H = 5.0f; |
| 36 | private static final float PADDING_V = 1.0f; |
| 37 | |
| 38 | private final RectF mRect = new RectF(); |
| 39 | private Paint mPaint; |
| 40 | |
The Android Open Source Project | d097a18 | 2008-12-17 18:05:58 -0800 | [diff] [blame] | 41 | private boolean mBackgroundSizeChanged; |
| 42 | private Drawable mBackground; |
| 43 | |
The Android Open Source Project | c8f00b6 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 44 | public BubbleTextView(Context context) { |
| 45 | super(context); |
| 46 | init(); |
| 47 | } |
| 48 | |
| 49 | public BubbleTextView(Context context, AttributeSet attrs) { |
| 50 | super(context, attrs); |
| 51 | init(); |
| 52 | } |
| 53 | |
| 54 | public BubbleTextView(Context context, AttributeSet attrs, int defStyle) { |
| 55 | super(context, attrs, defStyle); |
| 56 | init(); |
| 57 | } |
| 58 | |
| 59 | private void init() { |
| 60 | setFocusable(true); |
The Android Open Source Project | d097a18 | 2008-12-17 18:05:58 -0800 | [diff] [blame] | 61 | mBackground = getBackground(); |
| 62 | setBackgroundDrawable(null); |
| 63 | mBackground.setCallback(this); |
The Android Open Source Project | c8f00b6 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 64 | |
| 65 | mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 66 | mPaint.setColor(getContext().getResources().getColor(R.color.bubble_dark_background)); |
| 67 | } |
| 68 | |
| 69 | @Override |
The Android Open Source Project | d097a18 | 2008-12-17 18:05:58 -0800 | [diff] [blame] | 70 | protected boolean setFrame(int left, int top, int right, int bottom) { |
| 71 | if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) { |
| 72 | mBackgroundSizeChanged = true; |
| 73 | } |
| 74 | return super.setFrame(left, top, right, bottom); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | protected boolean verifyDrawable(Drawable who) { |
| 79 | return who == mBackground || super.verifyDrawable(who); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | protected void drawableStateChanged() { |
| 84 | Drawable d = mBackground; |
| 85 | if (d != null && d.isStateful()) { |
| 86 | d.setState(getDrawableState()); |
| 87 | } |
| 88 | super.drawableStateChanged(); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public void draw(Canvas canvas) { |
| 93 | final Drawable background = mBackground; |
| 94 | if (background != null) { |
| 95 | final int scrollX = mScrollX; |
| 96 | final int scrollY = mScrollY; |
| 97 | |
| 98 | if (mBackgroundSizeChanged) { |
| 99 | background.setBounds(0, 0, mRight - mLeft, mBottom - mTop); |
| 100 | mBackgroundSizeChanged = false; |
| 101 | } |
| 102 | |
| 103 | if ((scrollX | scrollY) == 0) { |
| 104 | background.draw(canvas); |
| 105 | } else { |
| 106 | canvas.translate(scrollX, scrollY); |
| 107 | background.draw(canvas); |
| 108 | canvas.translate(-scrollX, -scrollY); |
| 109 | } |
| 110 | } |
| 111 | |
The Android Open Source Project | c8f00b6 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 112 | final Layout layout = getLayout(); |
| 113 | final RectF rect = mRect; |
| 114 | final int left = getCompoundPaddingLeft(); |
| 115 | final int top = getExtendedPaddingTop(); |
| 116 | |
| 117 | rect.set(left + layout.getLineLeft(0) - PADDING_H, |
| 118 | top + layout.getLineTop(0) - PADDING_V, |
The Android Open Source Project | d097a18 | 2008-12-17 18:05:58 -0800 | [diff] [blame] | 119 | Math.min(left + layout.getLineRight(0) + PADDING_H, mScrollX + mRight - mLeft), |
The Android Open Source Project | c8f00b6 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 120 | top + layout.getLineBottom(0) + PADDING_V); |
| 121 | canvas.drawRoundRect(rect, CORNER_RADIUS, CORNER_RADIUS, mPaint); |
| 122 | |
The Android Open Source Project | d097a18 | 2008-12-17 18:05:58 -0800 | [diff] [blame] | 123 | super.draw(canvas); |
The Android Open Source Project | c8f00b6 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 124 | } |
| 125 | } |