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