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, |
Winson Chung | 72d5984 | 2012-02-22 13:51:36 -0800 | [diff] [blame] | 65 | int left, int top, int width, int height, final float initialScale) { |
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); |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 78 | mAnim.addUpdateListener(new AnimatorUpdateListener() { |
| 79 | @Override |
| 80 | public void onAnimationUpdate(ValueAnimator animation) { |
| 81 | final float value = (Float) animation.getAnimatedValue(); |
| 82 | |
| 83 | final int deltaX = (int) ((value * offsetX) - mOffsetX); |
| 84 | final int deltaY = (int) ((value * offsetY) - mOffsetY); |
| 85 | |
| 86 | mOffsetX += deltaX; |
| 87 | mOffsetY += deltaY; |
Winson Chung | 72d5984 | 2012-02-22 13:51:36 -0800 | [diff] [blame] | 88 | setScaleX(initialScale + (value * (scale - initialScale))); |
| 89 | setScaleY(initialScale + (value * (scale - initialScale))); |
Winson Chung | 867ca62 | 2012-02-21 15:48:35 -0800 | [diff] [blame] | 90 | if (sDragAlpha != 1f) { |
| 91 | setAlpha(sDragAlpha * value + (1f - value)); |
| 92 | } |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 93 | |
| 94 | if (getParent() == null) { |
| 95 | animation.cancel(); |
| 96 | } else { |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 97 | setTranslationX(getTranslationX() + deltaX); |
| 98 | setTranslationY(getTranslationY() + deltaY); |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | }); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 102 | |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 103 | mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height); |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 104 | setDragRegion(new Rect(0, 0, width, height)); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 105 | |
| 106 | // The point in our scaled bitmap that the touch events are located |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 107 | mRegistrationX = registrationX; |
| 108 | mRegistrationY = registrationY; |
Patrick Dubroy | 62bbb3c | 2011-01-17 15:29:27 -0800 | [diff] [blame] | 109 | |
| 110 | // Force a measure, because Workspace uses getMeasuredHeight() before the layout pass |
| 111 | int ms = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); |
| 112 | measure(ms, ms); |
Michael Jurka | 6cfafb9 | 2012-02-23 18:25:43 -0800 | [diff] [blame^] | 113 | mPaint = new Paint(Paint.FILTER_BITMAP_FLAG); |
Patrick Dubroy | 62bbb3c | 2011-01-17 15:29:27 -0800 | [diff] [blame] | 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 | } |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 166 | |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 167 | mHasDrawn = true; |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 168 | boolean crossFade = mCrossFadeProgress > 0 && mCrossFadeBitmap != null; |
| 169 | if (crossFade) { |
| 170 | int alpha = crossFade ? (int) (255 * (1 - mCrossFadeProgress)) : 255; |
| 171 | mPaint.setAlpha(alpha); |
| 172 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 173 | canvas.drawBitmap(mBitmap, 0.0f, 0.0f, mPaint); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 174 | if (crossFade) { |
| 175 | mPaint.setAlpha((int) (255 * mCrossFadeProgress)); |
| 176 | canvas.save(); |
| 177 | float sX = (mBitmap.getWidth() * 1.0f) / mCrossFadeBitmap.getWidth(); |
| 178 | float sY = (mBitmap.getHeight() * 1.0f) / mCrossFadeBitmap.getHeight(); |
| 179 | canvas.scale(sX, sY); |
| 180 | canvas.drawBitmap(mCrossFadeBitmap, 0.0f, 0.0f, mPaint); |
| 181 | canvas.restore(); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | public void setCrossFadeBitmap(Bitmap crossFadeBitmap) { |
| 186 | mCrossFadeBitmap = crossFadeBitmap; |
| 187 | } |
| 188 | |
| 189 | public void crossFade(int duration) { |
| 190 | ValueAnimator va = ValueAnimator.ofFloat(0f, 1f); |
| 191 | va.setDuration(duration); |
| 192 | va.setInterpolator(new DecelerateInterpolator(1.5f)); |
| 193 | va.addUpdateListener(new AnimatorUpdateListener() { |
| 194 | @Override |
| 195 | public void onAnimationUpdate(ValueAnimator animation) { |
| 196 | mCrossFadeProgress = animation.getAnimatedFraction(); |
| 197 | } |
| 198 | }); |
| 199 | va.start(); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 200 | } |
| 201 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 202 | public void setPaint(Paint paint) { |
| 203 | mPaint = paint; |
Michael Jurka | 6cfafb9 | 2012-02-23 18:25:43 -0800 | [diff] [blame^] | 204 | if (mPaint == null) { |
| 205 | mPaint = new Paint(Paint.FILTER_BITMAP_FLAG); |
| 206 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 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); |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 217 | mPaint.setAlpha((int) (255 * alpha)); |
| 218 | invalidate(); |
| 219 | } |
| 220 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 221 | /** |
| 222 | * Create a window containing this view and show it. |
| 223 | * |
| 224 | * @param windowToken obtained from v.getWindowToken() from one of your views |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 225 | * @param touchX the x coordinate the user touched in DragLayer coordinates |
| 226 | * @param touchY the y coordinate the user touched in DragLayer coordinates |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 227 | */ |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 228 | public void show(int touchX, int touchY) { |
| 229 | mDragLayer.addView(this); |
| 230 | DragLayer.LayoutParams lp = new DragLayer.LayoutParams(0, 0); |
| 231 | lp.width = mBitmap.getWidth(); |
| 232 | lp.height = mBitmap.getHeight(); |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 233 | lp.customPosition = true; |
| 234 | setLayoutParams(lp); |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 235 | setTranslationX(touchX - mRegistrationX); |
| 236 | setTranslationY(touchY - mRegistrationY); |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 237 | mAnim.start(); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 238 | } |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 239 | |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 240 | public void cancelAnimation() { |
| 241 | if (mAnim != null && mAnim.isRunning()) { |
| 242 | mAnim.cancel(); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | public void resetLayoutParams() { |
| 247 | mOffsetX = mOffsetY = 0; |
| 248 | requestLayout(); |
| 249 | } |
| 250 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 251 | /** |
| 252 | * Move the window containing this view. |
| 253 | * |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 254 | * @param touchX the x coordinate the user touched in DragLayer coordinates |
| 255 | * @param touchY the y coordinate the user touched in DragLayer coordinates |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 256 | */ |
| 257 | void move(int touchX, int touchY) { |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 258 | setTranslationX(touchX - mRegistrationX + (int) mOffsetX); |
| 259 | setTranslationY(touchY - mRegistrationY + (int) mOffsetY); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | void remove() { |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 263 | if (getParent() != null) { |
| 264 | mDragLayer.removeView(DragView.this); |
| 265 | } |
Patrick Dubroy | 6f13342 | 2011-02-24 12:16:12 -0800 | [diff] [blame] | 266 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 267 | } |
| 268 | |