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 | 42c29ae | 2012-02-06 16:43:52 -0800 | [diff] [blame] | 35 | private static float sDragAlpha = 0.8f; |
| 36 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 37 | private Bitmap mBitmap; |
| 38 | private Paint mPaint; |
| 39 | private int mRegistrationX; |
| 40 | private int mRegistrationY; |
| 41 | |
Winson Chung | b8c69f3 | 2011-10-19 21:36:08 -0700 | [diff] [blame] | 42 | private Point mDragVisualizeOffset = null; |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 43 | private Rect mDragRegion = null; |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 44 | private DragLayer mDragLayer = null; |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 45 | private boolean mHasDrawn = false; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 46 | |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 47 | ValueAnimator mAnim; |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 48 | private float mOffsetX = 0.0f; |
| 49 | private float mOffsetY = 0.0f; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 50 | |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 51 | private DragLayer.LayoutParams mLayoutParams; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 52 | |
| 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 | 42c29ae | 2012-02-06 16:43:52 -0800 | [diff] [blame] | 70 | final float scale = res.getInteger(R.integer.config_dragViewScaleFactor) / 100f; |
| 71 | final float offsetX = res.getDimensionPixelSize(R.dimen.dragViewOffsetX); |
| 72 | final float offsetY = res.getDimensionPixelSize(R.dimen.dragViewOffsetY); |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 73 | |
| 74 | // Animate the view into the correct position |
| 75 | mAnim = ValueAnimator.ofFloat(0.0f, 1.0f); |
Winson Chung | 42c29ae | 2012-02-06 16:43:52 -0800 | [diff] [blame] | 76 | mAnim.setDuration(150); |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 77 | mAnim.setInterpolator(new DecelerateInterpolator(2.5f)); |
| 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 | 42c29ae | 2012-02-06 16:43:52 -0800 | [diff] [blame] | 88 | setScaleX(1f + (value * (scale - 1f))); |
| 89 | setScaleY(1f + (value * (scale - 1f))); |
| 90 | setAlpha(sDragAlpha * value + (1f - value)); |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 91 | |
| 92 | if (getParent() == null) { |
| 93 | animation.cancel(); |
| 94 | } else { |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 95 | DragLayer.LayoutParams lp = mLayoutParams; |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 96 | lp.x += deltaX; |
| 97 | lp.y += deltaY; |
Winson Chung | 42c29ae | 2012-02-06 16:43:52 -0800 | [diff] [blame] | 98 | lp.width = mBitmap.getWidth(); |
| 99 | lp.height = mBitmap.getHeight(); |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 100 | mDragLayer.requestLayout(); |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | }); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 104 | |
Winson Chung | 42c29ae | 2012-02-06 16:43:52 -0800 | [diff] [blame] | 105 | mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height); |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 106 | setDragRegion(new Rect(0, 0, width, height)); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 107 | |
| 108 | // The point in our scaled bitmap that the touch events are located |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 109 | mRegistrationX = registrationX; |
| 110 | mRegistrationY = registrationY; |
Patrick Dubroy | 62bbb3c | 2011-01-17 15:29:27 -0800 | [diff] [blame] | 111 | |
| 112 | // Force a measure, because Workspace uses getMeasuredHeight() before the layout pass |
| 113 | int ms = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); |
| 114 | measure(ms, ms); |
| 115 | } |
| 116 | |
| 117 | public float getOffsetY() { |
| 118 | return mOffsetY; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 119 | } |
| 120 | |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 121 | public int getDragRegionLeft() { |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 122 | return mDragRegion.left; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | public int getDragRegionTop() { |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 126 | return mDragRegion.top; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | public int getDragRegionWidth() { |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 130 | return mDragRegion.width(); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | public int getDragRegionHeight() { |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 134 | return mDragRegion.height(); |
| 135 | } |
| 136 | |
Winson Chung | b8c69f3 | 2011-10-19 21:36:08 -0700 | [diff] [blame] | 137 | public void setDragVisualizeOffset(Point p) { |
| 138 | mDragVisualizeOffset = p; |
| 139 | } |
| 140 | |
| 141 | public Point getDragVisualizeOffset() { |
| 142 | return mDragVisualizeOffset; |
| 143 | } |
| 144 | |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 145 | public void setDragRegion(Rect r) { |
| 146 | mDragRegion = r; |
| 147 | } |
| 148 | |
| 149 | public Rect getDragRegion() { |
| 150 | return mDragRegion; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 153 | @Override |
| 154 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Joe Onorato | eebd924 | 2009-11-04 13:48:32 -0500 | [diff] [blame] | 155 | setMeasuredDimension(mBitmap.getWidth(), mBitmap.getHeight()); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | @Override |
| 159 | protected void onDraw(Canvas canvas) { |
| 160 | if (false) { |
| 161 | // for debugging |
| 162 | Paint p = new Paint(); |
| 163 | p.setStyle(Paint.Style.FILL); |
| 164 | p.setColor(0xaaffffff); |
| 165 | canvas.drawRect(0, 0, getWidth(), getHeight(), p); |
| 166 | } |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 167 | |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 168 | mHasDrawn = true; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 169 | canvas.drawBitmap(mBitmap, 0.0f, 0.0f, mPaint); |
| 170 | } |
| 171 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 172 | public void setPaint(Paint paint) { |
| 173 | mPaint = paint; |
| 174 | invalidate(); |
| 175 | } |
| 176 | |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 177 | public boolean hasDrawn() { |
| 178 | return mHasDrawn; |
| 179 | } |
| 180 | |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 181 | @Override |
| 182 | public void setAlpha(float alpha) { |
| 183 | super.setAlpha(alpha); |
| 184 | if (mPaint == null) { |
| 185 | mPaint = new Paint(); |
| 186 | } |
| 187 | mPaint.setAlpha((int) (255 * alpha)); |
| 188 | invalidate(); |
| 189 | } |
| 190 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 191 | /** |
| 192 | * Create a window containing this view and show it. |
| 193 | * |
| 194 | * @param windowToken obtained from v.getWindowToken() from one of your views |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 195 | * @param touchX the x coordinate the user touched in DragLayer coordinates |
| 196 | * @param touchY the y coordinate the user touched in DragLayer coordinates |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 197 | */ |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 198 | public void show(int touchX, int touchY) { |
| 199 | mDragLayer.addView(this); |
| 200 | DragLayer.LayoutParams lp = new DragLayer.LayoutParams(0, 0); |
| 201 | lp.width = mBitmap.getWidth(); |
| 202 | lp.height = mBitmap.getHeight(); |
| 203 | lp.x = touchX - mRegistrationX; |
| 204 | lp.y = touchY - mRegistrationY; |
| 205 | lp.customPosition = true; |
| 206 | setLayoutParams(lp); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 207 | mLayoutParams = lp; |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 208 | mAnim.start(); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 209 | } |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 210 | |
Winson Chung | 42c29ae | 2012-02-06 16:43:52 -0800 | [diff] [blame] | 211 | public void cancelAnimation() { |
| 212 | if (mAnim != null && mAnim.isRunning()) { |
| 213 | mAnim.cancel(); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | public void resetLayoutParams() { |
| 218 | DragLayer.LayoutParams lp = mLayoutParams; |
| 219 | lp.x = lp.y = 0; |
| 220 | mOffsetX = mOffsetY = 0; |
| 221 | } |
| 222 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 223 | /** |
| 224 | * Move the window containing this view. |
| 225 | * |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 226 | * @param touchX the x coordinate the user touched in DragLayer coordinates |
| 227 | * @param touchY the y coordinate the user touched in DragLayer coordinates |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 228 | */ |
| 229 | void move(int touchX, int touchY) { |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 230 | DragLayer.LayoutParams lp = mLayoutParams; |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 231 | lp.x = touchX - mRegistrationX + (int) mOffsetX; |
| 232 | lp.y = touchY - mRegistrationY + (int) mOffsetY; |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 233 | mDragLayer.requestLayout(); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | void remove() { |
Winson Chung | 42c29ae | 2012-02-06 16:43:52 -0800 | [diff] [blame] | 237 | if (getParent() != null) { |
| 238 | mDragLayer.removeView(DragView.this); |
| 239 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 240 | } |
Patrick Dubroy | 6f13342 | 2011-02-24 12:16:12 -0800 | [diff] [blame] | 241 | |
| 242 | int[] getPosition(int[] result) { |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 243 | DragLayer.LayoutParams lp = mLayoutParams; |
Patrick Dubroy | 6f13342 | 2011-02-24 12:16:12 -0800 | [diff] [blame] | 244 | if (result == null) result = new int[2]; |
| 245 | result[0] = lp.x; |
| 246 | result[1] = lp.y; |
| 247 | return result; |
| 248 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 249 | } |
| 250 | |