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; |
| 36 | private Paint mPaint; |
| 37 | private int mRegistrationX; |
| 38 | private int mRegistrationY; |
| 39 | |
Winson Chung | b8c69f3 | 2011-10-19 21:36:08 -0700 | [diff] [blame^] | 40 | private Point mDragVisualizeOffset = null; |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 41 | private Rect mDragRegion = null; |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 42 | private DragLayer mDragLayer = null; |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 43 | private boolean mHasDrawn = false; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 44 | |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 45 | ValueAnimator mAnim; |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 46 | private float mOffsetX = 0.0f; |
| 47 | private float mOffsetY = 0.0f; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 48 | |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 49 | private DragLayer.LayoutParams mLayoutParams; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Construct the drag view. |
| 53 | * <p> |
| 54 | * The registration point is the point inside our view that the touch events should |
| 55 | * be centered upon. |
| 56 | * |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 57 | * @param launcher The Launcher instance |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 58 | * @param bitmap The view that we're dragging around. We scale it up when we draw it. |
| 59 | * @param registrationX The x coordinate of the registration point. |
| 60 | * @param registrationY The y coordinate of the registration point. |
| 61 | */ |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 62 | public DragView(Launcher launcher, Bitmap bitmap, int registrationX, int registrationY, |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 63 | int left, int top, int width, int height) { |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 64 | super(launcher); |
| 65 | mDragLayer = launcher.getDragLayer(); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 66 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 67 | final Resources res = getResources(); |
| 68 | final int dragScale = res.getInteger(R.integer.config_dragViewExtraPixels); |
| 69 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 70 | Matrix scale = new Matrix(); |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 71 | final float scaleFactor = (width + dragScale) / width; |
| 72 | if (scaleFactor != 1.0f) { |
| 73 | scale.setScale(scaleFactor, scaleFactor); |
| 74 | } |
| 75 | |
Adam Cohen | 8878a32 | 2011-03-28 13:18:42 -0700 | [diff] [blame] | 76 | final int offsetX = res.getDimensionPixelSize(R.dimen.dragViewOffsetX); |
| 77 | final int offsetY = res.getDimensionPixelSize(R.dimen.dragViewOffsetY); |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 78 | |
| 79 | // Animate the view into the correct position |
| 80 | mAnim = ValueAnimator.ofFloat(0.0f, 1.0f); |
| 81 | mAnim.setDuration(110); |
| 82 | mAnim.setInterpolator(new DecelerateInterpolator(2.5f)); |
| 83 | mAnim.addUpdateListener(new AnimatorUpdateListener() { |
| 84 | @Override |
| 85 | public void onAnimationUpdate(ValueAnimator animation) { |
| 86 | final float value = (Float) animation.getAnimatedValue(); |
| 87 | |
| 88 | final int deltaX = (int) ((value * offsetX) - mOffsetX); |
| 89 | final int deltaY = (int) ((value * offsetY) - mOffsetY); |
| 90 | |
| 91 | mOffsetX += deltaX; |
| 92 | mOffsetY += deltaY; |
| 93 | |
| 94 | if (getParent() == null) { |
| 95 | animation.cancel(); |
| 96 | } else { |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 97 | DragLayer.LayoutParams lp = mLayoutParams; |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 98 | lp.x += deltaX; |
| 99 | lp.y += deltaY; |
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 | |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 105 | mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height, scale, true); |
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 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 211 | /** |
| 212 | * Move the window containing this view. |
| 213 | * |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 214 | * @param touchX the x coordinate the user touched in DragLayer coordinates |
| 215 | * @param touchY the y coordinate the user touched in DragLayer coordinates |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 216 | */ |
| 217 | void move(int touchX, int touchY) { |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 218 | DragLayer.LayoutParams lp = mLayoutParams; |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 219 | lp.x = touchX - mRegistrationX + (int) mOffsetX; |
| 220 | lp.y = touchY - mRegistrationY + (int) mOffsetY; |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 221 | mDragLayer.requestLayout(); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | void remove() { |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 225 | post(new Runnable() { |
| 226 | public void run() { |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 227 | mDragLayer.removeView(DragView.this); |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 228 | } |
| 229 | }); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 230 | } |
Patrick Dubroy | 6f13342 | 2011-02-24 12:16:12 -0800 | [diff] [blame] | 231 | |
| 232 | int[] getPosition(int[] result) { |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 233 | DragLayer.LayoutParams lp = mLayoutParams; |
Patrick Dubroy | 6f13342 | 2011-02-24 12:16:12 -0800 | [diff] [blame] | 234 | if (result == null) result = new int[2]; |
| 235 | result[0] = lp.x; |
| 236 | result[1] = lp.y; |
| 237 | return result; |
| 238 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 239 | } |
| 240 | |