BubbleTextView.java revision bdb5c5342adc550559fd723af461e53248f2fba8
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.launcher2;
18
19import com.android.launcher.R;
20
21import android.content.Context;
22import android.content.res.Resources;
23import android.graphics.Bitmap;
24import android.graphics.Canvas;
25import android.graphics.Color;
26import android.graphics.Paint;
27import android.graphics.Rect;
28import android.graphics.Region;
29import android.graphics.Region.Op;
30import android.graphics.drawable.Drawable;
31import android.util.AttributeSet;
32import android.view.MotionEvent;
33import android.view.View;
34import android.widget.TextView;
35
36/**
37 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
38 * because we want to make the bubble taller than the text and TextView's clip is
39 * too aggressive.
40 */
41public class BubbleTextView extends TextView implements VisibilityChangedBroadcaster {
42    static final float CORNER_RADIUS = 4.0f;
43    static final float SHADOW_LARGE_RADIUS = 4.0f;
44    static final float SHADOW_SMALL_RADIUS = 1.75f;
45    static final float SHADOW_Y_OFFSET = 2.0f;
46    static final int SHADOW_LARGE_COLOUR = 0xCC000000;
47    static final int SHADOW_SMALL_COLOUR = 0xBB000000;
48    static final float PADDING_H = 8.0f;
49    static final float PADDING_V = 3.0f;
50
51    private Paint mPaint;
52    private float mBubbleColorAlpha;
53    private int mPrevAlpha = -1;
54
55    private final HolographicOutlineHelper mOutlineHelper = new HolographicOutlineHelper();
56    private final Canvas mTempCanvas = new Canvas();
57    private final Rect mTempRect = new Rect();
58    private final Paint mTempPaint = new Paint();
59    private boolean mDidInvalidateForPressedState;
60    private Bitmap mPressedOrFocusedBackground;
61    private int mFocusedOutlineColor;
62    private int mFocusedGlowColor;
63    private int mPressedOutlineColor;
64    private int mPressedGlowColor;
65
66    private boolean mBackgroundSizeChanged;
67    private Drawable mBackground;
68
69    private VisibilityChangedListener mOnVisibilityChangedListener;
70
71    public BubbleTextView(Context context) {
72        super(context);
73        init();
74    }
75
76    public BubbleTextView(Context context, AttributeSet attrs) {
77        super(context, attrs);
78        init();
79    }
80
81    public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
82        super(context, attrs, defStyle);
83        init();
84    }
85
86    private void init() {
87        mBackground = getBackground();
88        setFocusable(true);
89        setBackgroundDrawable(null);
90
91        final Resources res = getContext().getResources();
92        int bubbleColor = res.getColor(R.color.bubble_dark_background);
93        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
94        mPaint.setColor(bubbleColor);
95        mBubbleColorAlpha = Color.alpha(bubbleColor) / 255.0f;
96        mFocusedOutlineColor = res.getColor(R.color.workspace_item_focused_outline_color);
97        mFocusedGlowColor = res.getColor(R.color.workspace_item_focused_glow_color);
98        mPressedOutlineColor = res.getColor(R.color.workspace_item_pressed_outline_color);
99        mPressedGlowColor = res.getColor(R.color.workspace_item_pressed_glow_color);
100    }
101
102    public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
103        Bitmap b = info.getIcon(iconCache);
104
105        setCompoundDrawablesWithIntrinsicBounds(null,
106                new FastBitmapDrawable(b),
107                null, null);
108        setText(info.title);
109        setTag(info);
110    }
111
112    @Override
113    protected boolean setFrame(int left, int top, int right, int bottom) {
114        if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
115            mBackgroundSizeChanged = true;
116        }
117        return super.setFrame(left, top, right, bottom);
118    }
119
120    @Override
121    protected boolean verifyDrawable(Drawable who) {
122        return who == mBackground || super.verifyDrawable(who);
123    }
124
125    @Override
126    protected void drawableStateChanged() {
127        if (isPressed()) {
128            // In this case, we have already created the pressed outline on ACTION_DOWN,
129            // so we just need to do an invalidate to trigger draw
130            if (!mDidInvalidateForPressedState) {
131                invalidate();
132            }
133        } else {
134            // Otherwise, either clear the pressed/focused background, or create a background
135            // for the focused state
136            final boolean backgroundEmptyBefore = mPressedOrFocusedBackground == null;
137            mPressedOrFocusedBackground = null;
138            if (isFocused()) {
139                mPressedOrFocusedBackground = createGlowingOutline(
140                        mTempCanvas, mFocusedGlowColor, mFocusedOutlineColor);
141                invalidate();
142            }
143            final boolean backgroundEmptyNow = mPressedOrFocusedBackground == null;
144            if (!backgroundEmptyBefore && backgroundEmptyNow) {
145                invalidate();
146            }
147        }
148
149        Drawable d = mBackground;
150        if (d != null && d.isStateful()) {
151            d.setState(getDrawableState());
152        }
153        super.drawableStateChanged();
154    }
155
156    /**
157     * Draw the View v into the given Canvas.
158     *
159     * @param v the view to draw
160     * @param destCanvas the canvas to draw on
161     * @param padding the horizontal and vertical padding to use when drawing
162     */
163    private void drawWithPadding(Canvas destCanvas, int padding) {
164        final Rect clipRect = mTempRect;
165        getDrawingRect(clipRect);
166
167        // adjust the clip rect so that we don't include the text label
168        clipRect.bottom =
169            getExtendedPaddingTop() - (int) BubbleTextView.PADDING_V + getLayout().getLineTop(0);
170
171        // Draw the View into the bitmap.
172        // The translate of scrollX and scrollY is necessary when drawing TextViews, because
173        // they set scrollX and scrollY to large values to achieve centered text
174        destCanvas.save();
175        destCanvas.translate(-getScrollX() + padding / 2, -getScrollY() + padding / 2);
176        destCanvas.clipRect(clipRect, Op.REPLACE);
177        draw(destCanvas);
178        destCanvas.restore();
179    }
180
181    /**
182     * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location.
183     * Responsibility for the bitmap is transferred to the caller.
184     */
185    private Bitmap createGlowingOutline(Canvas canvas, int outlineColor, int glowColor) {
186        final int padding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS;
187        final Bitmap b = Bitmap.createBitmap(
188                getWidth() + padding, getHeight() + padding, Bitmap.Config.ARGB_8888);
189
190        canvas.setBitmap(b);
191        drawWithPadding(canvas, padding);
192        mOutlineHelper.applyExtraThickExpensiveOutlineWithBlur(b, canvas, glowColor, outlineColor);
193
194        return b;
195    }
196
197    @Override
198    public boolean onTouchEvent(MotionEvent event) {
199        // Call the superclass onTouchEvent first, because sometimes it changes the state to
200        // isPressed() on an ACTION_UP
201        boolean result = super.onTouchEvent(event);
202
203        switch (event.getAction()) {
204            case MotionEvent.ACTION_DOWN:
205                // So that the pressed outline is visible immediately when isPressed() is true,
206                // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
207                // to create it)
208                if (mPressedOrFocusedBackground == null) {
209                    mPressedOrFocusedBackground = createGlowingOutline(
210                            mTempCanvas, mPressedGlowColor, mPressedOutlineColor);
211                }
212                // Invalidate so the pressed state is visible, or set a flag so we know that we
213                // have to call invalidate as soon as the state is "pressed"
214                if (isPressed()) {
215                    mDidInvalidateForPressedState = true;
216                    invalidate();
217                } else {
218                    mDidInvalidateForPressedState = false;
219                }
220                break;
221            case MotionEvent.ACTION_CANCEL:
222            case MotionEvent.ACTION_UP:
223                // If we've touched down and up on an item, and it's still not "pressed", then
224                // destroy the pressed outline
225                if (!isPressed()) {
226                    mPressedOrFocusedBackground = null;
227                }
228                break;
229        }
230        return result;
231    }
232
233    public void setVisibilityChangedListener(VisibilityChangedListener listener) {
234        mOnVisibilityChangedListener = listener;
235    }
236
237    @Override
238    protected void onVisibilityChanged(View changedView, int visibility) {
239        if (mOnVisibilityChangedListener != null) {
240            mOnVisibilityChangedListener.receiveVisibilityChangedMessage(this);
241        }
242        super.onVisibilityChanged(changedView, visibility);
243    }
244
245    @Override
246    public void draw(Canvas canvas) {
247        if (mPressedOrFocusedBackground != null && (isPressed() || isFocused())) {
248            // The blue glow can extend outside of our clip region, so we first temporarily expand
249            // the canvas's clip region
250            canvas.save(Canvas.CLIP_SAVE_FLAG);
251            int padding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS / 2;
252            canvas.clipRect(-padding + mScrollX, -padding + mScrollY,
253                    getWidth() + padding + mScrollX, getHeight() + padding + mScrollY,
254                    Region.Op.REPLACE);
255            // draw blue glow
256            canvas.drawBitmap(mPressedOrFocusedBackground,
257                    mScrollX - padding, mScrollY - padding, mTempPaint);
258            canvas.restore();
259        }
260
261        final Drawable background = mBackground;
262        if (background != null) {
263            final int scrollX = mScrollX;
264            final int scrollY = mScrollY;
265
266            if (mBackgroundSizeChanged) {
267                background.setBounds(0, 0,  mRight - mLeft, mBottom - mTop);
268                mBackgroundSizeChanged = false;
269            }
270
271            if ((scrollX | scrollY) == 0) {
272                background.draw(canvas);
273            } else {
274                canvas.translate(scrollX, scrollY);
275                background.draw(canvas);
276                canvas.translate(-scrollX, -scrollY);
277            }
278        }
279        // We enhance the shadow by drawing the shadow twice
280        setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
281        super.draw(canvas);
282        canvas.save(Canvas.CLIP_SAVE_FLAG);
283        canvas.clipRect(mScrollX, mScrollY + getExtendedPaddingTop(), mScrollX + getWidth(),
284                mScrollY + getHeight(), Region.Op.REPLACE);
285        setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
286        super.draw(canvas);
287        canvas.restore();
288    }
289
290    @Override
291    protected void onAttachedToWindow() {
292        super.onAttachedToWindow();
293        if (mBackground != null) mBackground.setCallback(this);
294    }
295
296    @Override
297    protected void onDetachedFromWindow() {
298        super.onDetachedFromWindow();
299        if (mBackground != null) mBackground.setCallback(null);
300    }
301
302    @Override
303    protected boolean onSetAlpha(int alpha) {
304        if (mPrevAlpha != alpha) {
305            mPrevAlpha = alpha;
306            mPaint.setAlpha((int) (alpha * mBubbleColorAlpha));
307            super.onSetAlpha(alpha);
308        }
309        return true;
310    }
311}
312