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 { |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 35 | private Bitmap mBitmap; |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 36 | private Bitmap mCrossFadeBitmap; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 37 | private Paint mPaint; |
| 38 | private int mRegistrationX; |
| 39 | private int mRegistrationY; |
| 40 | |
Winson Chung | b8c69f3 | 2011-10-19 21:36:08 -0700 | [diff] [blame] | 41 | private Point mDragVisualizeOffset = null; |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 42 | private Rect mDragRegion = null; |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 43 | private DragLayer mDragLayer = null; |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 44 | private boolean mHasDrawn = false; |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 45 | private float mCrossFadeProgress = 0f; |
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 | a1cdab0 | 2012-02-13 13:03:52 -0800 | [diff] [blame] | 70 | final int dragScale = res.getInteger(R.integer.config_dragViewExtraPixels); |
| 71 | |
| 72 | Matrix scale = new Matrix(); |
| 73 | final float scaleFactor = (width + dragScale) / width; |
| 74 | if (scaleFactor != 1.0f) { |
| 75 | scale.setScale(scaleFactor, scaleFactor); |
| 76 | } |
| 77 | |
| 78 | final int offsetX = res.getDimensionPixelSize(R.dimen.dragViewOffsetX); |
| 79 | final int offsetY = res.getDimensionPixelSize(R.dimen.dragViewOffsetY); |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 80 | |
| 81 | // Animate the view into the correct position |
| 82 | mAnim = ValueAnimator.ofFloat(0.0f, 1.0f); |
Winson Chung | a1cdab0 | 2012-02-13 13:03:52 -0800 | [diff] [blame] | 83 | mAnim.setDuration(110); |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 84 | mAnim.setInterpolator(new DecelerateInterpolator(2.5f)); |
| 85 | mAnim.addUpdateListener(new AnimatorUpdateListener() { |
| 86 | @Override |
| 87 | public void onAnimationUpdate(ValueAnimator animation) { |
| 88 | final float value = (Float) animation.getAnimatedValue(); |
| 89 | |
| 90 | final int deltaX = (int) ((value * offsetX) - mOffsetX); |
| 91 | final int deltaY = (int) ((value * offsetY) - mOffsetY); |
| 92 | |
| 93 | mOffsetX += deltaX; |
| 94 | mOffsetY += deltaY; |
| 95 | |
| 96 | if (getParent() == null) { |
| 97 | animation.cancel(); |
| 98 | } else { |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 99 | DragLayer.LayoutParams lp = mLayoutParams; |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 100 | lp.x += deltaX; |
| 101 | lp.y += deltaY; |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 102 | mDragLayer.requestLayout(); |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | }); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 106 | |
Winson Chung | a1cdab0 | 2012-02-13 13:03:52 -0800 | [diff] [blame] | 107 | mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height, scale, true); |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 108 | setDragRegion(new Rect(0, 0, width, height)); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 109 | |
| 110 | // The point in our scaled bitmap that the touch events are located |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 111 | mRegistrationX = registrationX; |
| 112 | mRegistrationY = registrationY; |
Patrick Dubroy | 62bbb3c | 2011-01-17 15:29:27 -0800 | [diff] [blame] | 113 | |
| 114 | // Force a measure, because Workspace uses getMeasuredHeight() before the layout pass |
| 115 | int ms = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); |
| 116 | measure(ms, ms); |
| 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 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 155 | @Override |
| 156 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Joe Onorato | eebd924 | 2009-11-04 13:48:32 -0500 | [diff] [blame] | 157 | setMeasuredDimension(mBitmap.getWidth(), mBitmap.getHeight()); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | @Override |
| 161 | protected void onDraw(Canvas canvas) { |
| 162 | if (false) { |
| 163 | // for debugging |
| 164 | Paint p = new Paint(); |
| 165 | p.setStyle(Paint.Style.FILL); |
| 166 | p.setColor(0xaaffffff); |
| 167 | canvas.drawRect(0, 0, getWidth(), getHeight(), p); |
| 168 | } |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 169 | if (mPaint == null) { |
| 170 | mPaint = new Paint(); |
| 171 | } |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 172 | |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 173 | mHasDrawn = true; |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 174 | boolean crossFade = mCrossFadeProgress > 0 && mCrossFadeBitmap != null; |
| 175 | if (crossFade) { |
| 176 | int alpha = crossFade ? (int) (255 * (1 - mCrossFadeProgress)) : 255; |
| 177 | mPaint.setAlpha(alpha); |
| 178 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 179 | canvas.drawBitmap(mBitmap, 0.0f, 0.0f, mPaint); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 180 | if (crossFade) { |
| 181 | mPaint.setAlpha((int) (255 * mCrossFadeProgress)); |
| 182 | canvas.save(); |
| 183 | float sX = (mBitmap.getWidth() * 1.0f) / mCrossFadeBitmap.getWidth(); |
| 184 | float sY = (mBitmap.getHeight() * 1.0f) / mCrossFadeBitmap.getHeight(); |
| 185 | canvas.scale(sX, sY); |
| 186 | canvas.drawBitmap(mCrossFadeBitmap, 0.0f, 0.0f, mPaint); |
| 187 | canvas.restore(); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | public void setCrossFadeBitmap(Bitmap crossFadeBitmap) { |
| 192 | mCrossFadeBitmap = crossFadeBitmap; |
| 193 | } |
| 194 | |
| 195 | public void crossFade(int duration) { |
| 196 | ValueAnimator va = ValueAnimator.ofFloat(0f, 1f); |
| 197 | va.setDuration(duration); |
| 198 | va.setInterpolator(new DecelerateInterpolator(1.5f)); |
| 199 | va.addUpdateListener(new AnimatorUpdateListener() { |
| 200 | @Override |
| 201 | public void onAnimationUpdate(ValueAnimator animation) { |
| 202 | mCrossFadeProgress = animation.getAnimatedFraction(); |
| 203 | } |
| 204 | }); |
| 205 | va.start(); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 206 | } |
| 207 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 208 | public void setPaint(Paint paint) { |
| 209 | mPaint = paint; |
| 210 | invalidate(); |
| 211 | } |
| 212 | |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 213 | public boolean hasDrawn() { |
| 214 | return mHasDrawn; |
| 215 | } |
| 216 | |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 217 | @Override |
| 218 | public void setAlpha(float alpha) { |
| 219 | super.setAlpha(alpha); |
| 220 | if (mPaint == null) { |
| 221 | mPaint = new Paint(); |
| 222 | } |
| 223 | mPaint.setAlpha((int) (255 * alpha)); |
| 224 | invalidate(); |
| 225 | } |
| 226 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 227 | /** |
| 228 | * Create a window containing this view and show it. |
| 229 | * |
| 230 | * @param windowToken obtained from v.getWindowToken() from one of your views |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 231 | * @param touchX the x coordinate the user touched in DragLayer coordinates |
| 232 | * @param touchY the y coordinate the user touched in DragLayer coordinates |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 233 | */ |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 234 | public void show(int touchX, int touchY) { |
| 235 | mDragLayer.addView(this); |
| 236 | DragLayer.LayoutParams lp = new DragLayer.LayoutParams(0, 0); |
| 237 | lp.width = mBitmap.getWidth(); |
| 238 | lp.height = mBitmap.getHeight(); |
| 239 | lp.x = touchX - mRegistrationX; |
| 240 | lp.y = touchY - mRegistrationY; |
| 241 | lp.customPosition = true; |
| 242 | setLayoutParams(lp); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 243 | mLayoutParams = lp; |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 244 | mAnim.start(); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 245 | } |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 246 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 247 | /** |
| 248 | * Move the window containing this view. |
| 249 | * |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 250 | * @param touchX the x coordinate the user touched in DragLayer coordinates |
| 251 | * @param touchY the y coordinate the user touched in DragLayer coordinates |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 252 | */ |
| 253 | void move(int touchX, int touchY) { |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 254 | DragLayer.LayoutParams lp = mLayoutParams; |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 255 | lp.x = touchX - mRegistrationX + (int) mOffsetX; |
| 256 | lp.y = touchY - mRegistrationY + (int) mOffsetY; |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 257 | mDragLayer.requestLayout(); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | void remove() { |
Winson Chung | a1cdab0 | 2012-02-13 13:03:52 -0800 | [diff] [blame] | 261 | mDragLayer.removeView(DragView.this); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 262 | } |
Patrick Dubroy | 6f13342 | 2011-02-24 12:16:12 -0800 | [diff] [blame] | 263 | |
| 264 | int[] getPosition(int[] result) { |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 265 | DragLayer.LayoutParams lp = mLayoutParams; |
Patrick Dubroy | 6f13342 | 2011-02-24 12:16:12 -0800 | [diff] [blame] | 266 | if (result == null) result = new int[2]; |
| 267 | result[0] = lp.x; |
| 268 | result[1] = lp.y; |
| 269 | return result; |
| 270 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 271 | } |
| 272 | |