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