blob: f2c31e93fab91adde1d16323be8adef543f92578 [file] [log] [blame]
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001/*
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
17package com.android.launcher;
18
19import android.widget.TextView;
20import android.content.Context;
21import android.util.AttributeSet;
22import android.graphics.Canvas;
23import android.graphics.Paint;
24import android.graphics.RectF;
The Android Open Source Projectd097a182008-12-17 18:05:58 -080025import android.graphics.drawable.Drawable;
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -070026import 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 */
33public 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 Projectd097a182008-12-17 18:05:58 -080041 private boolean mBackgroundSizeChanged;
42 private Drawable mBackground;
43
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -070044 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 Projectd097a182008-12-17 18:05:58 -080061 mBackground = getBackground();
62 setBackgroundDrawable(null);
63 mBackground.setCallback(this);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -070064
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 Projectd097a182008-12-17 18:05:58 -080070 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 Projectc8f00b62008-10-21 07:00:00 -0700112 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 Projectd097a182008-12-17 18:05:58 -0800119 Math.min(left + layout.getLineRight(0) + PADDING_H, mScrollX + mRight - mLeft),
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700120 top + layout.getLineBottom(0) + PADDING_V);
121 canvas.drawRoundRect(rect, CORNER_RADIUS, CORNER_RADIUS, mPaint);
122
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800123 super.draw(canvas);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700124 }
125}