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; |
| 25 | import android.graphics.Rect; |
| 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 | |
| 41 | public BubbleTextView(Context context) { |
| 42 | super(context); |
| 43 | init(); |
| 44 | } |
| 45 | |
| 46 | public BubbleTextView(Context context, AttributeSet attrs) { |
| 47 | super(context, attrs); |
| 48 | init(); |
| 49 | } |
| 50 | |
| 51 | public BubbleTextView(Context context, AttributeSet attrs, int defStyle) { |
| 52 | super(context, attrs, defStyle); |
| 53 | init(); |
| 54 | } |
| 55 | |
| 56 | private void init() { |
| 57 | setFocusable(true); |
| 58 | |
| 59 | mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 60 | mPaint.setColor(getContext().getResources().getColor(R.color.bubble_dark_background)); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public void onDraw(Canvas canvas) { |
| 65 | final Layout layout = getLayout(); |
| 66 | final RectF rect = mRect; |
| 67 | final int left = getCompoundPaddingLeft(); |
| 68 | final int top = getExtendedPaddingTop(); |
| 69 | |
| 70 | rect.set(left + layout.getLineLeft(0) - PADDING_H, |
| 71 | top + layout.getLineTop(0) - PADDING_V, |
| 72 | left + layout.getLineRight(0) + PADDING_H, |
| 73 | top + layout.getLineBottom(0) + PADDING_V); |
| 74 | canvas.drawRoundRect(rect, CORNER_RADIUS, CORNER_RADIUS, mPaint); |
| 75 | |
| 76 | super.onDraw(canvas); |
| 77 | } |
| 78 | } |