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