Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [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 | |
| 17 | |
| 18 | package com.android.launcher2; |
| 19 | |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 20 | import android.animation.ValueAnimator; |
| 21 | import android.animation.ValueAnimator.AnimatorUpdateListener; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 22 | import android.content.res.Resources; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 23 | import android.graphics.Bitmap; |
| 24 | import android.graphics.Canvas; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 25 | import android.graphics.Paint; |
Winson Chung | b8c69f3 | 2011-10-19 21:36:08 -0700 | [diff] [blame] | 26 | import android.graphics.Point; |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 27 | import android.graphics.PorterDuff; |
| 28 | import android.graphics.PorterDuffColorFilter; |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 29 | import android.graphics.Rect; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 30 | import android.view.View; |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 31 | import android.view.animation.DecelerateInterpolator; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 32 | |
Adam Cohen | 120980b | 2010-12-08 11:05:37 -0800 | [diff] [blame] | 33 | import com.android.launcher.R; |
| 34 | |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 35 | public class DragView extends View { |
Winson Chung | 867ca62 | 2012-02-21 15:48:35 -0800 | [diff] [blame] | 36 | private static float sDragAlpha = 1f; |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 37 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 38 | private Bitmap mBitmap; |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 39 | private Bitmap mCrossFadeBitmap; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 40 | private Paint mPaint; |
| 41 | private int mRegistrationX; |
| 42 | private int mRegistrationY; |
| 43 | |
Winson Chung | b8c69f3 | 2011-10-19 21:36:08 -0700 | [diff] [blame] | 44 | private Point mDragVisualizeOffset = null; |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 45 | private Rect mDragRegion = null; |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 46 | private DragLayer mDragLayer = null; |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 47 | private boolean mHasDrawn = false; |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 48 | private float mCrossFadeProgress = 0f; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 49 | |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 50 | ValueAnimator mAnim; |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 51 | private float mOffsetX = 0.0f; |
| 52 | private float mOffsetY = 0.0f; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 53 | private float mInitialScale = 1f; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 54 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 55 | /** |
| 56 | * Construct the drag view. |
| 57 | * <p> |
| 58 | * The registration point is the point inside our view that the touch events should |
| 59 | * be centered upon. |
| 60 | * |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 61 | * @param launcher The Launcher instance |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 62 | * @param bitmap The view that we're dragging around. We scale it up when we draw it. |
| 63 | * @param registrationX The x coordinate of the registration point. |
| 64 | * @param registrationY The y coordinate of the registration point. |
| 65 | */ |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 66 | public DragView(Launcher launcher, Bitmap bitmap, int registrationX, int registrationY, |
Winson Chung | 72d5984 | 2012-02-22 13:51:36 -0800 | [diff] [blame] | 67 | int left, int top, int width, int height, final float initialScale) { |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 68 | super(launcher); |
| 69 | mDragLayer = launcher.getDragLayer(); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 70 | mInitialScale = initialScale; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 71 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 72 | final Resources res = getResources(); |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 73 | final float offsetX = res.getDimensionPixelSize(R.dimen.dragViewOffsetX); |
| 74 | final float offsetY = res.getDimensionPixelSize(R.dimen.dragViewOffsetY); |
| 75 | final float scaleDps = res.getDimensionPixelSize(R.dimen.dragViewScale); |
| 76 | final float scale = (width + scaleDps) / width; |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 77 | |
| 78 | // Animate the view into the correct position |
| 79 | mAnim = ValueAnimator.ofFloat(0.0f, 1.0f); |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 80 | mAnim.setDuration(150); |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 81 | mAnim.addUpdateListener(new AnimatorUpdateListener() { |
| 82 | @Override |
| 83 | public void onAnimationUpdate(ValueAnimator animation) { |
| 84 | final float value = (Float) animation.getAnimatedValue(); |
| 85 | |
| 86 | final int deltaX = (int) ((value * offsetX) - mOffsetX); |
| 87 | final int deltaY = (int) ((value * offsetY) - mOffsetY); |
| 88 | |
| 89 | mOffsetX += deltaX; |
| 90 | mOffsetY += deltaY; |
Winson Chung | 72d5984 | 2012-02-22 13:51:36 -0800 | [diff] [blame] | 91 | setScaleX(initialScale + (value * (scale - initialScale))); |
| 92 | setScaleY(initialScale + (value * (scale - initialScale))); |
Winson Chung | 867ca62 | 2012-02-21 15:48:35 -0800 | [diff] [blame] | 93 | if (sDragAlpha != 1f) { |
| 94 | setAlpha(sDragAlpha * value + (1f - value)); |
| 95 | } |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 96 | |
| 97 | if (getParent() == null) { |
| 98 | animation.cancel(); |
| 99 | } else { |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 100 | setTranslationX(getTranslationX() + deltaX); |
| 101 | setTranslationY(getTranslationY() + deltaY); |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | }); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 105 | |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 106 | mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height); |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 107 | setDragRegion(new Rect(0, 0, width, height)); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 108 | |
| 109 | // The point in our scaled bitmap that the touch events are located |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 110 | mRegistrationX = registrationX; |
| 111 | mRegistrationY = registrationY; |
Patrick Dubroy | 62bbb3c | 2011-01-17 15:29:27 -0800 | [diff] [blame] | 112 | |
| 113 | // Force a measure, because Workspace uses getMeasuredHeight() before the layout pass |
| 114 | int ms = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); |
| 115 | measure(ms, ms); |
Michael Jurka | 6cfafb9 | 2012-02-23 18:25:43 -0800 | [diff] [blame] | 116 | mPaint = new Paint(Paint.FILTER_BITMAP_FLAG); |
Patrick Dubroy | 62bbb3c | 2011-01-17 15:29:27 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | public float getOffsetY() { |
| 120 | return mOffsetY; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 121 | } |
| 122 | |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 123 | public int getDragRegionLeft() { |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 124 | return mDragRegion.left; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | public int getDragRegionTop() { |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 128 | return mDragRegion.top; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | public int getDragRegionWidth() { |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 132 | return mDragRegion.width(); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | public int getDragRegionHeight() { |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 136 | return mDragRegion.height(); |
| 137 | } |
| 138 | |
Winson Chung | b8c69f3 | 2011-10-19 21:36:08 -0700 | [diff] [blame] | 139 | public void setDragVisualizeOffset(Point p) { |
| 140 | mDragVisualizeOffset = p; |
| 141 | } |
| 142 | |
| 143 | public Point getDragVisualizeOffset() { |
| 144 | return mDragVisualizeOffset; |
| 145 | } |
| 146 | |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 147 | public void setDragRegion(Rect r) { |
| 148 | mDragRegion = r; |
| 149 | } |
| 150 | |
| 151 | public Rect getDragRegion() { |
| 152 | return mDragRegion; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 155 | public float getInitialScale() { |
| 156 | return mInitialScale; |
| 157 | } |
| 158 | |
| 159 | public void updateInitialScaleToCurrentScale() { |
| 160 | mInitialScale = getScaleX(); |
| 161 | } |
| 162 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 163 | @Override |
| 164 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Joe Onorato | eebd924 | 2009-11-04 13:48:32 -0500 | [diff] [blame] | 165 | setMeasuredDimension(mBitmap.getWidth(), mBitmap.getHeight()); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | @Override |
| 169 | protected void onDraw(Canvas canvas) { |
Michael Jurka | 3a9fced | 2012-04-13 14:44:29 -0700 | [diff] [blame] | 170 | @SuppressWarnings("all") // suppress dead code warning |
| 171 | final boolean debug = false; |
| 172 | if (debug) { |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 173 | Paint p = new Paint(); |
| 174 | p.setStyle(Paint.Style.FILL); |
Winson Chung | eecf02d | 2012-03-02 17:14:58 -0800 | [diff] [blame] | 175 | p.setColor(0x66ffffff); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 176 | canvas.drawRect(0, 0, getWidth(), getHeight(), p); |
| 177 | } |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 178 | |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 179 | mHasDrawn = true; |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 180 | boolean crossFade = mCrossFadeProgress > 0 && mCrossFadeBitmap != null; |
| 181 | if (crossFade) { |
| 182 | int alpha = crossFade ? (int) (255 * (1 - mCrossFadeProgress)) : 255; |
| 183 | mPaint.setAlpha(alpha); |
| 184 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 185 | canvas.drawBitmap(mBitmap, 0.0f, 0.0f, mPaint); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 186 | if (crossFade) { |
| 187 | mPaint.setAlpha((int) (255 * mCrossFadeProgress)); |
| 188 | canvas.save(); |
| 189 | float sX = (mBitmap.getWidth() * 1.0f) / mCrossFadeBitmap.getWidth(); |
| 190 | float sY = (mBitmap.getHeight() * 1.0f) / mCrossFadeBitmap.getHeight(); |
| 191 | canvas.scale(sX, sY); |
| 192 | canvas.drawBitmap(mCrossFadeBitmap, 0.0f, 0.0f, mPaint); |
| 193 | canvas.restore(); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | public void setCrossFadeBitmap(Bitmap crossFadeBitmap) { |
| 198 | mCrossFadeBitmap = crossFadeBitmap; |
| 199 | } |
| 200 | |
| 201 | public void crossFade(int duration) { |
| 202 | ValueAnimator va = ValueAnimator.ofFloat(0f, 1f); |
| 203 | va.setDuration(duration); |
| 204 | va.setInterpolator(new DecelerateInterpolator(1.5f)); |
| 205 | va.addUpdateListener(new AnimatorUpdateListener() { |
| 206 | @Override |
| 207 | public void onAnimationUpdate(ValueAnimator animation) { |
| 208 | mCrossFadeProgress = animation.getAnimatedFraction(); |
| 209 | } |
| 210 | }); |
| 211 | va.start(); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 212 | } |
| 213 | |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 214 | public void setColor(int color) { |
Michael Jurka | 6cfafb9 | 2012-02-23 18:25:43 -0800 | [diff] [blame] | 215 | if (mPaint == null) { |
| 216 | mPaint = new Paint(Paint.FILTER_BITMAP_FLAG); |
| 217 | } |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 218 | if (color != 0) { |
| 219 | mPaint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP)); |
| 220 | } else { |
| 221 | mPaint.setColorFilter(null); |
| 222 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 223 | invalidate(); |
| 224 | } |
| 225 | |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 226 | public boolean hasDrawn() { |
| 227 | return mHasDrawn; |
| 228 | } |
| 229 | |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 230 | @Override |
| 231 | public void setAlpha(float alpha) { |
| 232 | super.setAlpha(alpha); |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 233 | mPaint.setAlpha((int) (255 * alpha)); |
| 234 | invalidate(); |
| 235 | } |
| 236 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 237 | /** |
| 238 | * Create a window containing this view and show it. |
| 239 | * |
| 240 | * @param windowToken obtained from v.getWindowToken() from one of your views |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 241 | * @param touchX the x coordinate the user touched in DragLayer coordinates |
| 242 | * @param touchY the y coordinate the user touched in DragLayer coordinates |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 243 | */ |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 244 | public void show(int touchX, int touchY) { |
| 245 | mDragLayer.addView(this); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 246 | |
| 247 | // Enable hw-layers on this view |
| 248 | setLayerType(View.LAYER_TYPE_HARDWARE, null); |
| 249 | |
| 250 | // Start the pick-up animation |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 251 | DragLayer.LayoutParams lp = new DragLayer.LayoutParams(0, 0); |
| 252 | lp.width = mBitmap.getWidth(); |
| 253 | lp.height = mBitmap.getHeight(); |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 254 | lp.customPosition = true; |
| 255 | setLayoutParams(lp); |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 256 | setTranslationX(touchX - mRegistrationX); |
| 257 | setTranslationY(touchY - mRegistrationY); |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 258 | mAnim.start(); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 259 | } |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 260 | |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 261 | public void cancelAnimation() { |
| 262 | if (mAnim != null && mAnim.isRunning()) { |
| 263 | mAnim.cancel(); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | public void resetLayoutParams() { |
| 268 | mOffsetX = mOffsetY = 0; |
| 269 | requestLayout(); |
| 270 | } |
| 271 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 272 | /** |
| 273 | * Move the window containing this view. |
| 274 | * |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 275 | * @param touchX the x coordinate the user touched in DragLayer coordinates |
| 276 | * @param touchY the y coordinate the user touched in DragLayer coordinates |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 277 | */ |
| 278 | void move(int touchX, int touchY) { |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 279 | setTranslationX(touchX - mRegistrationX + (int) mOffsetX); |
| 280 | setTranslationY(touchY - mRegistrationY + (int) mOffsetY); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | void remove() { |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 284 | if (getParent() != null) { |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 285 | // Disable hw-layers on this view |
| 286 | setLayerType(View.LAYER_TYPE_NONE, null); |
| 287 | |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 288 | mDragLayer.removeView(DragView.this); |
| 289 | } |
Patrick Dubroy | 6f13342 | 2011-02-24 12:16:12 -0800 | [diff] [blame] | 290 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 291 | } |
| 292 | |