The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 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 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 18 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 19 | import android.content.Context; |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 20 | import android.content.res.Resources; |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 21 | import android.graphics.Bitmap; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 22 | import android.graphics.Canvas; |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 23 | import android.graphics.Color; |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 24 | import android.graphics.Rect; |
Michael Jurka | 137142e | 2011-01-05 20:57:04 -0800 | [diff] [blame] | 25 | import android.graphics.Region; |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 26 | import android.graphics.Region.Op; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 27 | import android.graphics.drawable.Drawable; |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 28 | import android.util.AttributeSet; |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 29 | import android.util.TypedValue; |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 30 | import android.view.MotionEvent; |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 31 | import android.widget.TextView; |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 32 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 33 | /** |
| 34 | * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan |
| 35 | * because we want to make the bubble taller than the text and TextView's clip is |
| 36 | * too aggressive. |
| 37 | */ |
Michael Jurka | 08ee770 | 2011-08-11 16:53:35 -0700 | [diff] [blame] | 38 | public class BubbleTextView extends TextView { |
Winson Chung | 8812703 | 2010-12-13 12:11:33 -0800 | [diff] [blame] | 39 | static final float SHADOW_LARGE_RADIUS = 4.0f; |
| 40 | static final float SHADOW_SMALL_RADIUS = 1.75f; |
| 41 | static final float SHADOW_Y_OFFSET = 2.0f; |
Winson Chung | c7aef8c | 2011-09-15 18:53:04 -0700 | [diff] [blame] | 42 | static final int SHADOW_LARGE_COLOUR = 0xDD000000; |
| 43 | static final int SHADOW_SMALL_COLOUR = 0xCC000000; |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 44 | static final float PADDING_H = 8.0f; |
| 45 | static final float PADDING_V = 3.0f; |
| 46 | |
Winson Chung | e22a8e9 | 2010-11-12 13:40:58 -0800 | [diff] [blame] | 47 | private int mPrevAlpha = -1; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 48 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 49 | private HolographicOutlineHelper mOutlineHelper; |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 50 | private final Canvas mTempCanvas = new Canvas(); |
| 51 | private final Rect mTempRect = new Rect(); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 52 | private boolean mDidInvalidateForPressedState; |
| 53 | private Bitmap mPressedOrFocusedBackground; |
| 54 | private int mFocusedOutlineColor; |
| 55 | private int mFocusedGlowColor; |
| 56 | private int mPressedOutlineColor; |
| 57 | private int mPressedGlowColor; |
| 58 | |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 59 | private int mTextColor; |
| 60 | private boolean mShadowsEnabled = true; |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 61 | private boolean mIsTextVisible; |
| 62 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 63 | private boolean mBackgroundSizeChanged; |
| 64 | private Drawable mBackground; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 65 | |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 66 | private boolean mStayPressed; |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 67 | private CheckLongPressHelper mLongPressHelper; |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 68 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 69 | public BubbleTextView(Context context) { |
| 70 | super(context); |
| 71 | init(); |
| 72 | } |
| 73 | |
| 74 | public BubbleTextView(Context context, AttributeSet attrs) { |
| 75 | super(context, attrs); |
| 76 | init(); |
| 77 | } |
| 78 | |
| 79 | public BubbleTextView(Context context, AttributeSet attrs, int defStyle) { |
| 80 | super(context, attrs, defStyle); |
| 81 | init(); |
| 82 | } |
| 83 | |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 84 | public void onFinishInflate() { |
| 85 | super.onFinishInflate(); |
| 86 | |
| 87 | // Ensure we are using the right text size |
| 88 | LauncherAppState app = LauncherAppState.getInstance(); |
| 89 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
| 90 | setTextSize(TypedValue.COMPLEX_UNIT_SP, grid.iconTextSize); |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 91 | setTextColor(getResources().getColor(R.color.workspace_icon_text_color)); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 92 | } |
| 93 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 94 | private void init() { |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 95 | mLongPressHelper = new CheckLongPressHelper(this); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 96 | mBackground = getBackground(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 97 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 98 | mOutlineHelper = HolographicOutlineHelper.obtain(getContext()); |
| 99 | |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 100 | final Resources res = getContext().getResources(); |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 101 | mFocusedOutlineColor = mFocusedGlowColor = mPressedOutlineColor = mPressedGlowColor = |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 102 | res.getColor(R.color.outline_color); |
Michael Jurka | e7e3f6c | 2011-02-01 21:08:29 -0800 | [diff] [blame] | 103 | |
| 104 | setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 107 | public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) { |
| 108 | Bitmap b = info.getIcon(iconCache); |
Winson Chung | cdef044 | 2013-09-19 17:32:58 -0700 | [diff] [blame] | 109 | LauncherAppState app = LauncherAppState.getInstance(); |
| 110 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 111 | |
Winson Chung | 5400049 | 2013-10-14 16:29:29 -0700 | [diff] [blame] | 112 | setCompoundDrawables(null, |
Winson Chung | 0dbd734 | 2013-10-13 22:46:20 -0700 | [diff] [blame] | 113 | Utilities.createIconDrawable(b), null, null); |
Winson Chung | cdef044 | 2013-09-19 17:32:58 -0700 | [diff] [blame] | 114 | setCompoundDrawablePadding((int) ((grid.folderIconSizePx - grid.iconSizePx) / 2f)); |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 115 | setText(info.title); |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 116 | setTag(info); |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 117 | } |
| 118 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 119 | @Override |
| 120 | protected boolean setFrame(int left, int top, int right, int bottom) { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 121 | if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 122 | mBackgroundSizeChanged = true; |
| 123 | } |
| 124 | return super.setFrame(left, top, right, bottom); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | protected boolean verifyDrawable(Drawable who) { |
| 129 | return who == mBackground || super.verifyDrawable(who); |
| 130 | } |
| 131 | |
| 132 | @Override |
Michael Jurka | 816474f | 2012-06-25 14:49:02 -0700 | [diff] [blame] | 133 | public void setTag(Object tag) { |
| 134 | if (tag != null) { |
| 135 | LauncherModel.checkItemInfo((ItemInfo) tag); |
| 136 | } |
| 137 | super.setTag(tag); |
| 138 | } |
| 139 | |
| 140 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 141 | protected void drawableStateChanged() { |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 142 | if (isPressed()) { |
| 143 | // In this case, we have already created the pressed outline on ACTION_DOWN, |
| 144 | // so we just need to do an invalidate to trigger draw |
| 145 | if (!mDidInvalidateForPressedState) { |
Patrick Dubroy | 3499d8c | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 146 | setCellLayoutPressedOrFocusedIcon(); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 147 | } |
| 148 | } else { |
| 149 | // Otherwise, either clear the pressed/focused background, or create a background |
| 150 | // for the focused state |
| 151 | final boolean backgroundEmptyBefore = mPressedOrFocusedBackground == null; |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 152 | if (!mStayPressed) { |
| 153 | mPressedOrFocusedBackground = null; |
| 154 | } |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 155 | if (isFocused()) { |
Gilles Debunne | c786952 | 2011-11-28 18:03:56 -0800 | [diff] [blame] | 156 | if (getLayout() == null) { |
Patrick Dubroy | a017c03 | 2011-03-09 15:58:32 -0800 | [diff] [blame] | 157 | // In some cases, we get focus before we have been layed out. Set the |
| 158 | // background to null so that it will get created when the view is drawn. |
| 159 | mPressedOrFocusedBackground = null; |
| 160 | } else { |
| 161 | mPressedOrFocusedBackground = createGlowingOutline( |
| 162 | mTempCanvas, mFocusedGlowColor, mFocusedOutlineColor); |
| 163 | } |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 164 | mStayPressed = false; |
Patrick Dubroy | 3499d8c | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 165 | setCellLayoutPressedOrFocusedIcon(); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 166 | } |
| 167 | final boolean backgroundEmptyNow = mPressedOrFocusedBackground == null; |
| 168 | if (!backgroundEmptyBefore && backgroundEmptyNow) { |
Patrick Dubroy | 3499d8c | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 169 | setCellLayoutPressedOrFocusedIcon(); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 173 | Drawable d = mBackground; |
| 174 | if (d != null && d.isStateful()) { |
| 175 | d.setState(getDrawableState()); |
| 176 | } |
| 177 | super.drawableStateChanged(); |
| 178 | } |
| 179 | |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 180 | /** |
Patrick Dubroy | cd95371 | 2011-02-28 15:16:42 -0800 | [diff] [blame] | 181 | * Draw this BubbleTextView into the given Canvas. |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 182 | * |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 183 | * @param destCanvas the canvas to draw on |
| 184 | * @param padding the horizontal and vertical padding to use when drawing |
| 185 | */ |
| 186 | private void drawWithPadding(Canvas destCanvas, int padding) { |
| 187 | final Rect clipRect = mTempRect; |
| 188 | getDrawingRect(clipRect); |
| 189 | |
| 190 | // adjust the clip rect so that we don't include the text label |
| 191 | clipRect.bottom = |
| 192 | getExtendedPaddingTop() - (int) BubbleTextView.PADDING_V + getLayout().getLineTop(0); |
| 193 | |
| 194 | // Draw the View into the bitmap. |
| 195 | // The translate of scrollX and scrollY is necessary when drawing TextViews, because |
| 196 | // they set scrollX and scrollY to large values to achieve centered text |
| 197 | destCanvas.save(); |
Winson Chung | eecf02d | 2012-03-02 17:14:58 -0800 | [diff] [blame] | 198 | destCanvas.scale(getScaleX(), getScaleY(), |
| 199 | (getWidth() + padding) / 2, (getHeight() + padding) / 2); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 200 | destCanvas.translate(-getScrollX() + padding / 2, -getScrollY() + padding / 2); |
| 201 | destCanvas.clipRect(clipRect, Op.REPLACE); |
Patrick Dubroy | 3499d8c | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 202 | draw(destCanvas); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 203 | destCanvas.restore(); |
| 204 | } |
| 205 | |
Adam Cohen | 7397e62 | 2013-10-24 12:11:34 -0700 | [diff] [blame^] | 206 | public void setGlowColor(int color) { |
| 207 | mFocusedOutlineColor = mFocusedGlowColor = mPressedOutlineColor = mPressedGlowColor = color; |
| 208 | } |
| 209 | |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 210 | /** |
| 211 | * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location. |
| 212 | * Responsibility for the bitmap is transferred to the caller. |
| 213 | */ |
| 214 | private Bitmap createGlowingOutline(Canvas canvas, int outlineColor, int glowColor) { |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 215 | final int padding = mOutlineHelper.mMaxOuterBlurRadius; |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 216 | final Bitmap b = Bitmap.createBitmap( |
| 217 | getWidth() + padding, getHeight() + padding, Bitmap.Config.ARGB_8888); |
| 218 | |
| 219 | canvas.setBitmap(b); |
| 220 | drawWithPadding(canvas, padding); |
| 221 | mOutlineHelper.applyExtraThickExpensiveOutlineWithBlur(b, canvas, glowColor, outlineColor); |
Adam Cohen | aaf473c | 2011-08-03 12:02:47 -0700 | [diff] [blame] | 222 | canvas.setBitmap(null); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 223 | |
| 224 | return b; |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public boolean onTouchEvent(MotionEvent event) { |
| 229 | // Call the superclass onTouchEvent first, because sometimes it changes the state to |
| 230 | // isPressed() on an ACTION_UP |
| 231 | boolean result = super.onTouchEvent(event); |
| 232 | |
| 233 | switch (event.getAction()) { |
| 234 | case MotionEvent.ACTION_DOWN: |
| 235 | // So that the pressed outline is visible immediately when isPressed() is true, |
| 236 | // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time |
| 237 | // to create it) |
| 238 | if (mPressedOrFocusedBackground == null) { |
| 239 | mPressedOrFocusedBackground = createGlowingOutline( |
| 240 | mTempCanvas, mPressedGlowColor, mPressedOutlineColor); |
| 241 | } |
| 242 | // Invalidate so the pressed state is visible, or set a flag so we know that we |
| 243 | // have to call invalidate as soon as the state is "pressed" |
| 244 | if (isPressed()) { |
| 245 | mDidInvalidateForPressedState = true; |
Michael Jurka | e6235dd | 2011-10-04 15:02:05 -0700 | [diff] [blame] | 246 | setCellLayoutPressedOrFocusedIcon(); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 247 | } else { |
| 248 | mDidInvalidateForPressedState = false; |
| 249 | } |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 250 | |
| 251 | mLongPressHelper.postCheckForLongPress(); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 252 | break; |
| 253 | case MotionEvent.ACTION_CANCEL: |
| 254 | case MotionEvent.ACTION_UP: |
| 255 | // If we've touched down and up on an item, and it's still not "pressed", then |
| 256 | // destroy the pressed outline |
| 257 | if (!isPressed()) { |
| 258 | mPressedOrFocusedBackground = null; |
| 259 | } |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 260 | |
| 261 | mLongPressHelper.cancelLongPress(); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 262 | break; |
| 263 | } |
| 264 | return result; |
| 265 | } |
| 266 | |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 267 | void setStayPressed(boolean stayPressed) { |
| 268 | mStayPressed = stayPressed; |
| 269 | if (!stayPressed) { |
| 270 | mPressedOrFocusedBackground = null; |
| 271 | } |
Patrick Dubroy | 3499d8c | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 272 | setCellLayoutPressedOrFocusedIcon(); |
| 273 | } |
| 274 | |
| 275 | void setCellLayoutPressedOrFocusedIcon() { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 276 | if (getParent() instanceof ShortcutAndWidgetContainer) { |
| 277 | ShortcutAndWidgetContainer parent = (ShortcutAndWidgetContainer) getParent(); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 278 | if (parent != null) { |
| 279 | CellLayout layout = (CellLayout) parent.getParent(); |
| 280 | layout.setPressedOrFocusedIcon((mPressedOrFocusedBackground != null) ? this : null); |
| 281 | } |
Patrick Dubroy | d69e113 | 2011-03-15 10:29:01 -0700 | [diff] [blame] | 282 | } |
Patrick Dubroy | 3499d8c | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 283 | } |
| 284 | |
Winson Chung | 1e9cbfe | 2011-09-30 16:52:26 -0700 | [diff] [blame] | 285 | void clearPressedOrFocusedBackground() { |
| 286 | mPressedOrFocusedBackground = null; |
| 287 | setCellLayoutPressedOrFocusedIcon(); |
| 288 | } |
| 289 | |
Patrick Dubroy | 3499d8c | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 290 | Bitmap getPressedOrFocusedBackground() { |
| 291 | return mPressedOrFocusedBackground; |
| 292 | } |
| 293 | |
| 294 | int getPressedOrFocusedBackgroundPadding() { |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 295 | return mOutlineHelper.mMaxOuterBlurRadius / 2; |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 296 | } |
Patrick Dubroy | a017c03 | 2011-03-09 15:58:32 -0800 | [diff] [blame] | 297 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 298 | @Override |
| 299 | public void draw(Canvas canvas) { |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 300 | if (!mShadowsEnabled) { |
| 301 | super.draw(canvas); |
| 302 | return; |
| 303 | } |
| 304 | |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 305 | final Drawable background = mBackground; |
| 306 | if (background != null) { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 307 | final int scrollX = getScrollX(); |
| 308 | final int scrollY = getScrollY(); |
Winson Chung | 8812703 | 2010-12-13 12:11:33 -0800 | [diff] [blame] | 309 | |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 310 | if (mBackgroundSizeChanged) { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 311 | background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop()); |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 312 | mBackgroundSizeChanged = false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 313 | } |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 314 | |
| 315 | if ((scrollX | scrollY) == 0) { |
| 316 | background.draw(canvas); |
| 317 | } else { |
| 318 | canvas.translate(scrollX, scrollY); |
| 319 | background.draw(canvas); |
| 320 | canvas.translate(-scrollX, -scrollY); |
| 321 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 322 | } |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 323 | |
| 324 | // If text is transparent, don't draw any shadow |
Andrew Flynn | bc239a1 | 2012-03-06 11:39:49 -0800 | [diff] [blame] | 325 | if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) { |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 326 | getPaint().clearShadowLayer(); |
| 327 | super.draw(canvas); |
| 328 | return; |
| 329 | } |
| 330 | |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 331 | // We enhance the shadow by drawing the shadow twice |
Michael Jurka | e7e3f6c | 2011-02-01 21:08:29 -0800 | [diff] [blame] | 332 | getPaint().setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR); |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 333 | super.draw(canvas); |
| 334 | canvas.save(Canvas.CLIP_SAVE_FLAG); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 335 | canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(), |
| 336 | getScrollX() + getWidth(), |
| 337 | getScrollY() + getHeight(), Region.Op.INTERSECT); |
Michael Jurka | e7e3f6c | 2011-02-01 21:08:29 -0800 | [diff] [blame] | 338 | getPaint().setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR); |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 339 | super.draw(canvas); |
| 340 | canvas.restore(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 341 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 342 | |
| 343 | @Override |
| 344 | protected void onAttachedToWindow() { |
| 345 | super.onAttachedToWindow(); |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 346 | if (mBackground != null) mBackground.setCallback(this); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | @Override |
| 350 | protected void onDetachedFromWindow() { |
| 351 | super.onDetachedFromWindow(); |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 352 | if (mBackground != null) mBackground.setCallback(null); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 353 | } |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 354 | |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 355 | @Override |
| 356 | public void setTextColor(int color) { |
| 357 | mTextColor = color; |
| 358 | super.setTextColor(color); |
| 359 | } |
| 360 | |
| 361 | public void setShadowsEnabled(boolean enabled) { |
| 362 | mShadowsEnabled = enabled; |
| 363 | getPaint().clearShadowLayer(); |
| 364 | invalidate(); |
| 365 | } |
| 366 | |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 367 | public void setTextVisibility(boolean visible) { |
| 368 | Resources res = getResources(); |
| 369 | if (visible) { |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 370 | super.setTextColor(mTextColor); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 371 | } else { |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 372 | super.setTextColor(res.getColor(android.R.color.transparent)); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 373 | } |
| 374 | mIsTextVisible = visible; |
| 375 | } |
| 376 | |
| 377 | public boolean isTextVisible() { |
| 378 | return mIsTextVisible; |
| 379 | } |
| 380 | |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 381 | @Override |
| 382 | protected boolean onSetAlpha(int alpha) { |
Winson Chung | e22a8e9 | 2010-11-12 13:40:58 -0800 | [diff] [blame] | 383 | if (mPrevAlpha != alpha) { |
| 384 | mPrevAlpha = alpha; |
Winson Chung | e22a8e9 | 2010-11-12 13:40:58 -0800 | [diff] [blame] | 385 | super.onSetAlpha(alpha); |
| 386 | } |
| 387 | return true; |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 388 | } |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 389 | |
| 390 | @Override |
| 391 | public void cancelLongPress() { |
| 392 | super.cancelLongPress(); |
| 393 | |
| 394 | mLongPressHelper.cancelLongPress(); |
| 395 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 396 | } |