blob: 076f574ded132ba9aa026f5493e8fab9d82c87c3 [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;
42
43 private boolean mBackgroundSizeChanged;
44 private Drawable mBackground;
45 private float mCornerRadius;
46 private float mPaddingH;
47 private float mPaddingV;
48
49 public BubbleTextView(Context context) {
50 super(context);
51 init();
52 }
53
54 public BubbleTextView(Context context, AttributeSet attrs) {
55 super(context, attrs);
56 init();
57 }
58
59 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
60 super(context, attrs, defStyle);
61 init();
62 }
63
64 private void init() {
65 setFocusable(true);
66 mBackground = getBackground();
67 setBackgroundDrawable(null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068
69 mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
70 mPaint.setColor(getContext().getResources().getColor(R.color.bubble_dark_background));
71
72 final float scale = getContext().getResources().getDisplayMetrics().density;
73 mCornerRadius = CORNER_RADIUS * scale;
74 mPaddingH = PADDING_H * scale;
75 //noinspection PointlessArithmeticExpression
76 mPaddingV = PADDING_V * scale;
77 }
78
79 @Override
80 protected boolean setFrame(int left, int top, int right, int bottom) {
81 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
82 mBackgroundSizeChanged = true;
83 }
84 return super.setFrame(left, top, right, bottom);
85 }
86
87 @Override
88 protected boolean verifyDrawable(Drawable who) {
89 return who == mBackground || super.verifyDrawable(who);
90 }
91
92 @Override
93 protected void drawableStateChanged() {
94 Drawable d = mBackground;
95 if (d != null && d.isStateful()) {
96 d.setState(getDrawableState());
97 }
98 super.drawableStateChanged();
99 }
100
101 @Override
102 public void draw(Canvas canvas) {
103 final Drawable background = mBackground;
104 if (background != null) {
105 final int scrollX = mScrollX;
106 final int scrollY = mScrollY;
107
108 if (mBackgroundSizeChanged) {
109 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
110 mBackgroundSizeChanged = false;
111 }
112
113 if ((scrollX | scrollY) == 0) {
114 background.draw(canvas);
115 } else {
116 canvas.translate(scrollX, scrollY);
117 background.draw(canvas);
118 canvas.translate(-scrollX, -scrollY);
119 }
120 }
121
122 final Layout layout = getLayout();
123 final RectF rect = mRect;
124 final int left = getCompoundPaddingLeft();
125 final int top = getExtendedPaddingTop();
126
127 rect.set(left + layout.getLineLeft(0) - mPaddingH,
128 top + layout.getLineTop(0) - mPaddingV,
129 Math.min(left + layout.getLineRight(0) + mPaddingH, mScrollX + mRight - mLeft),
130 top + layout.getLineBottom(0) + mPaddingV);
131 canvas.drawRoundRect(rect, mCornerRadius, mCornerRadius, mPaint);
132
133 super.draw(canvas);
134 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400135
136 @Override
137 protected void onAttachedToWindow() {
138 super.onAttachedToWindow();
139 mBackground.setCallback(this);
140 }
141
142 @Override
143 protected void onDetachedFromWindow() {
144 super.onDetachedFromWindow();
145 mBackground.setCallback(null);
146 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700147
148 @Override
149 protected boolean onSetAlpha(int alpha) {
150 mPaint.setAlpha(alpha);
151 return super.onSetAlpha(alpha);
152 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153}