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
17package com.android.camera;
18
19import com.android.gallery.R;
20
21import android.content.Context;
22import android.graphics.Canvas;
23import android.graphics.Paint;
24import android.graphics.RectF;
25import android.text.Layout;
26import android.util.AttributeSet;
27import android.widget.TextView;
28
29/**
30 * TextView that draws a bubble behind the text. We cannot use a
31 * LineBackgroundSpan because we want to make the bubble taller than the text
32 * and TextView's clip is too aggressive.
33 */
34public class ActionMenuButton extends TextView {
35    private static final int CORNER_RADIUS = 8;
36    private static final int PADDING_H = 5;
37    private static final int PADDING_V = 1;
38
39    private final RectF mRect = new RectF();
40    private Paint mPaint;
41
42    public ActionMenuButton(Context context) {
43        super(context);
44        init();
45    }
46
47    public ActionMenuButton(Context context, AttributeSet attrs) {
48        super(context, attrs);
49        init();
50    }
51
52    public ActionMenuButton(Context context, AttributeSet attrs, int defStyle) {
53        super(context, attrs, defStyle);
54        init();
55    }
56
57    private void init() {
58        setFocusable(true);
59        // We need extra padding below to prevent the bubble being cut.
60        setPadding(PADDING_H, 0, PADDING_H, PADDING_V);
61
62        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
63        mPaint.setColor(getContext().getResources()
64                .getColor(R.color.bubble_dark_background));
65    }
66
67    @Override
68    protected void drawableStateChanged() {
69        invalidate();
70        super.drawableStateChanged();
71    }
72
73    @Override
74    public void draw(Canvas canvas) {
75        final Layout layout = getLayout();
76        final RectF rect = mRect;
77        final int left = getCompoundPaddingLeft();
78        final int top = getExtendedPaddingTop();
79
80        rect.set(left + layout.getLineLeft(0) - PADDING_H,
81                 top + layout.getLineTop(0) - PADDING_V,
82                 Math.min(left + layout.getLineRight(0) + PADDING_H,
83                          mScrollX + mRight - mLeft),
84                 top + layout.getLineBottom(0) + PADDING_V);
85        canvas.drawRoundRect(rect, CORNER_RADIUS, CORNER_RADIUS, mPaint);
86
87        super.draw(canvas);
88    }
89}
90