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