The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [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.camera; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.graphics.Canvas; |
| 21 | import android.graphics.Paint; |
| 22 | import android.graphics.RectF; |
| 23 | import android.text.Layout; |
| 24 | import android.util.AttributeSet; |
| 25 | import android.widget.TextView; |
| 26 | |
| 27 | /** |
Chih-Chung Chang | 5ceee69 | 2009-04-01 00:40:11 -0700 | [diff] [blame] | 28 | * TextView that draws a bubble behind the text. We cannot use a |
| 29 | * LineBackgroundSpan because we want to make the bubble taller than the text |
| 30 | * and TextView's clip is too aggressive. |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 31 | */ |
| 32 | public class ActionMenuButton extends TextView { |
Chih-Chung Chang | 233b3b9 | 2009-04-08 05:21:00 -0700 | [diff] [blame^] | 33 | private static final int CORNER_RADIUS = 8; |
| 34 | private static final int PADDING_H = 5; |
| 35 | private static final int PADDING_V = 1; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 36 | |
The Android Open Source Project | 8d0dd0e | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 37 | private static final int[] RESTRICTED_STATE_SET = { |
| 38 | R.attr.state_restricted |
| 39 | }; |
| 40 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 41 | private final RectF mRect = new RectF(); |
| 42 | private Paint mPaint; |
The Android Open Source Project | 8d0dd0e | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 43 | private boolean mRestricted = false; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 44 | |
| 45 | public ActionMenuButton(Context context) { |
| 46 | super(context); |
| 47 | init(); |
| 48 | } |
| 49 | |
| 50 | public ActionMenuButton(Context context, AttributeSet attrs) { |
| 51 | super(context, attrs); |
| 52 | init(); |
| 53 | } |
| 54 | |
| 55 | public ActionMenuButton(Context context, AttributeSet attrs, int defStyle) { |
| 56 | super(context, attrs, defStyle); |
| 57 | init(); |
| 58 | } |
| 59 | |
| 60 | private void init() { |
| 61 | setFocusable(true); |
Chih-Chung Chang | 233b3b9 | 2009-04-08 05:21:00 -0700 | [diff] [blame^] | 62 | setPadding(PADDING_H, PADDING_V, PADDING_H, PADDING_V); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 63 | |
| 64 | mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
Chih-Chung Chang | 5ceee69 | 2009-04-01 00:40:11 -0700 | [diff] [blame] | 65 | mPaint.setColor(getContext().getResources() |
| 66 | .getColor(R.color.bubble_dark_background)); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 67 | } |
| 68 | |
The Android Open Source Project | 8d0dd0e | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 69 | public void setRestricted(boolean restricted) { |
| 70 | if (restricted != mRestricted) { |
| 71 | mRestricted = restricted; |
| 72 | refreshDrawableState(); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | public boolean isRestricted() { |
| 77 | return mRestricted; |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | protected int[] onCreateDrawableState(int extraSpace) { |
| 82 | int[] drawableState = super.onCreateDrawableState(extraSpace + 1); |
| 83 | if (isRestricted()) { |
| 84 | mergeDrawableStates(drawableState, RESTRICTED_STATE_SET); |
| 85 | } |
| 86 | return drawableState; |
| 87 | } |
| 88 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 89 | @Override |
| 90 | protected void drawableStateChanged() { |
| 91 | invalidate(); |
| 92 | super.drawableStateChanged(); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public void draw(Canvas canvas) { |
| 97 | final Layout layout = getLayout(); |
| 98 | final RectF rect = mRect; |
| 99 | final int left = getCompoundPaddingLeft(); |
| 100 | final int top = getExtendedPaddingTop(); |
| 101 | |
| 102 | rect.set(left + layout.getLineLeft(0) - PADDING_H, |
Chih-Chung Chang | 5ceee69 | 2009-04-01 00:40:11 -0700 | [diff] [blame] | 103 | top + layout.getLineTop(0) - PADDING_V, |
| 104 | Math.min(left + layout.getLineRight(0) + PADDING_H, |
| 105 | mScrollX + mRight - mLeft), |
| 106 | top + layout.getLineBottom(0) + PADDING_V); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 107 | canvas.drawRoundRect(rect, CORNER_RADIUS, CORNER_RADIUS, mPaint); |
| 108 | |
| 109 | super.draw(canvas); |
| 110 | } |
| 111 | } |