blob: f4a3d4499a58cbf9ef41bb0fdbc0b5ef838f5476 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
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 Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
19import android.widget.TextView;
20import android.content.Context;
21import android.util.AttributeSet;
22import android.graphics.Canvas;
23import android.graphics.Paint;
24import android.graphics.RectF;
25import android.graphics.drawable.Drawable;
26import android.text.Layout;
27
Romain Guyedcce092010-03-04 13:03:17 -080028import com.android.launcher.R;
29
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030/**
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 */
35public class BubbleTextView extends TextView {
Joe Onoratobf15cb42009-08-07 14:33:40 -070036 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 Project31dd5032009-03-03 19:32:27 -080039
40 private final RectF mRect = new RectF();
41 private Paint mPaint;
Winson Chunge22a8e92010-11-12 13:40:58 -080042 private int mPrevAlpha = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043
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 Project31dd5032009-03-03 19:32:27 -080069
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 Onorato9c1289c2009-08-17 11:03:03 -0400136
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 Chungaffd7b42010-08-20 15:11:56 -0700148
149 @Override
150 protected boolean onSetAlpha(int alpha) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800151 if (mPrevAlpha != alpha) {
152 mPrevAlpha = alpha;
153 mPaint.setAlpha(alpha);
154 super.onSetAlpha(alpha);
155 }
156 return true;
Winson Chungaffd7b42010-08-20 15:11:56 -0700157 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158}