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