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; |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 27 | import android.graphics.Rect; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 28 | import android.os.IBinder; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 29 | import android.view.View; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 30 | import android.view.WindowManager; |
| 31 | import android.view.WindowManagerImpl; |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 32 | import android.view.animation.DecelerateInterpolator; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 33 | |
Adam Cohen | 120980b | 2010-12-08 11:05:37 -0800 | [diff] [blame] | 34 | import com.android.launcher.R; |
| 35 | |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 36 | public class DragView extends View { |
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 | |
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; |
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 | /** |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 52 | * A callback to be called the first time this view is drawn. |
| 53 | * This allows the originator of the drag to dim or hide the original view as soon |
| 54 | * as the DragView is drawn. |
| 55 | */ |
| 56 | private Runnable mOnDrawRunnable = null; |
| 57 | |
| 58 | /** |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 59 | * Construct the drag view. |
| 60 | * <p> |
| 61 | * The registration point is the point inside our view that the touch events should |
| 62 | * be centered upon. |
| 63 | * |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 64 | * @param launcher The Launcher instance |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 65 | * @param bitmap The view that we're dragging around. We scale it up when we draw it. |
| 66 | * @param registrationX The x coordinate of the registration point. |
| 67 | * @param registrationY The y coordinate of the registration point. |
| 68 | */ |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 69 | public DragView(Launcher launcher, Bitmap bitmap, int registrationX, int registrationY, |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 70 | int left, int top, int width, int height) { |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 71 | super(launcher); |
| 72 | mDragLayer = launcher.getDragLayer(); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 73 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 74 | final Resources res = getResources(); |
| 75 | final int dragScale = res.getInteger(R.integer.config_dragViewExtraPixels); |
| 76 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 77 | Matrix scale = new Matrix(); |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 78 | final float scaleFactor = (width + dragScale) / width; |
| 79 | if (scaleFactor != 1.0f) { |
| 80 | scale.setScale(scaleFactor, scaleFactor); |
| 81 | } |
| 82 | |
Adam Cohen | 8878a32 | 2011-03-28 13:18:42 -0700 | [diff] [blame] | 83 | final int offsetX = res.getDimensionPixelSize(R.dimen.dragViewOffsetX); |
| 84 | final int offsetY = res.getDimensionPixelSize(R.dimen.dragViewOffsetY); |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 85 | |
| 86 | // Animate the view into the correct position |
| 87 | mAnim = ValueAnimator.ofFloat(0.0f, 1.0f); |
| 88 | mAnim.setDuration(110); |
| 89 | mAnim.setInterpolator(new DecelerateInterpolator(2.5f)); |
| 90 | mAnim.addUpdateListener(new AnimatorUpdateListener() { |
| 91 | @Override |
| 92 | public void onAnimationUpdate(ValueAnimator animation) { |
| 93 | final float value = (Float) animation.getAnimatedValue(); |
| 94 | |
| 95 | final int deltaX = (int) ((value * offsetX) - mOffsetX); |
| 96 | final int deltaY = (int) ((value * offsetY) - mOffsetY); |
| 97 | |
| 98 | mOffsetX += deltaX; |
| 99 | mOffsetY += deltaY; |
| 100 | |
| 101 | if (getParent() == null) { |
| 102 | animation.cancel(); |
| 103 | } else { |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 104 | DragLayer.LayoutParams lp = mLayoutParams; |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 105 | lp.x += deltaX; |
| 106 | lp.y += deltaY; |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 107 | mDragLayer.requestLayout(); |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | }); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 111 | |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 112 | mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height, scale, true); |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 113 | setDragRegion(new Rect(0, 0, width, height)); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 114 | |
| 115 | // The point in our scaled bitmap that the touch events are located |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 116 | mRegistrationX = registrationX; |
| 117 | mRegistrationY = registrationY; |
Patrick Dubroy | 62bbb3c | 2011-01-17 15:29:27 -0800 | [diff] [blame] | 118 | |
| 119 | // Force a measure, because Workspace uses getMeasuredHeight() before the layout pass |
| 120 | int ms = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); |
| 121 | measure(ms, ms); |
| 122 | } |
| 123 | |
| 124 | public float getOffsetY() { |
| 125 | return mOffsetY; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 126 | } |
| 127 | |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 128 | public void setOnDrawRunnable(Runnable r) { |
| 129 | mOnDrawRunnable = r; |
| 130 | } |
| 131 | |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 132 | public int getDragRegionLeft() { |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 133 | return mDragRegion.left; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | public int getDragRegionTop() { |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 137 | return mDragRegion.top; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | public int getDragRegionWidth() { |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 141 | return mDragRegion.width(); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | public int getDragRegionHeight() { |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 145 | return mDragRegion.height(); |
| 146 | } |
| 147 | |
| 148 | public void setDragRegion(Rect r) { |
| 149 | mDragRegion = r; |
| 150 | } |
| 151 | |
| 152 | public Rect getDragRegion() { |
| 153 | return mDragRegion; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 156 | @Override |
| 157 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Joe Onorato | eebd924 | 2009-11-04 13:48:32 -0500 | [diff] [blame] | 158 | setMeasuredDimension(mBitmap.getWidth(), mBitmap.getHeight()); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | @Override |
| 162 | protected void onDraw(Canvas canvas) { |
| 163 | if (false) { |
| 164 | // for debugging |
| 165 | Paint p = new Paint(); |
| 166 | p.setStyle(Paint.Style.FILL); |
| 167 | p.setColor(0xaaffffff); |
| 168 | canvas.drawRect(0, 0, getWidth(), getHeight(), p); |
| 169 | } |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 170 | |
| 171 | // Call the callback if we haven't already been detached |
| 172 | if (getParent() != null) { |
| 173 | if (mOnDrawRunnable != null) { |
| 174 | mOnDrawRunnable.run(); |
| 175 | mOnDrawRunnable = null; |
| 176 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 177 | } |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 178 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 179 | canvas.drawBitmap(mBitmap, 0.0f, 0.0f, mPaint); |
| 180 | } |
| 181 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 182 | public void setPaint(Paint paint) { |
| 183 | mPaint = paint; |
| 184 | invalidate(); |
| 185 | } |
| 186 | |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 187 | @Override |
| 188 | public void setAlpha(float alpha) { |
| 189 | super.setAlpha(alpha); |
| 190 | if (mPaint == null) { |
| 191 | mPaint = new Paint(); |
| 192 | } |
| 193 | mPaint.setAlpha((int) (255 * alpha)); |
| 194 | invalidate(); |
| 195 | } |
| 196 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 197 | /** |
| 198 | * Create a window containing this view and show it. |
| 199 | * |
| 200 | * @param windowToken obtained from v.getWindowToken() from one of your views |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 201 | * @param touchX the x coordinate the user touched in DragLayer coordinates |
| 202 | * @param touchY the y coordinate the user touched in DragLayer coordinates |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 203 | */ |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 204 | public void show(int touchX, int touchY) { |
| 205 | mDragLayer.addView(this); |
| 206 | DragLayer.LayoutParams lp = new DragLayer.LayoutParams(0, 0); |
| 207 | lp.width = mBitmap.getWidth(); |
| 208 | lp.height = mBitmap.getHeight(); |
| 209 | lp.x = touchX - mRegistrationX; |
| 210 | lp.y = touchY - mRegistrationY; |
| 211 | lp.customPosition = true; |
| 212 | setLayoutParams(lp); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 213 | mLayoutParams = lp; |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 214 | mAnim.start(); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 215 | } |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 216 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 217 | /** |
| 218 | * Move the window containing this view. |
| 219 | * |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 220 | * @param touchX the x coordinate the user touched in DragLayer coordinates |
| 221 | * @param touchY the y coordinate the user touched in DragLayer coordinates |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 222 | */ |
| 223 | void move(int touchX, int touchY) { |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 224 | DragLayer.LayoutParams lp = mLayoutParams; |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 225 | lp.x = touchX - mRegistrationX + (int) mOffsetX; |
| 226 | lp.y = touchY - mRegistrationY + (int) mOffsetY; |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 227 | mDragLayer.requestLayout(); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | void remove() { |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 231 | post(new Runnable() { |
| 232 | public void run() { |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 233 | mDragLayer.removeView(DragView.this); |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 234 | } |
| 235 | }); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 236 | } |
Patrick Dubroy | 6f13342 | 2011-02-24 12:16:12 -0800 | [diff] [blame] | 237 | |
| 238 | int[] getPosition(int[] result) { |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 239 | DragLayer.LayoutParams lp = mLayoutParams; |
Patrick Dubroy | 6f13342 | 2011-02-24 12:16:12 -0800 | [diff] [blame] | 240 | if (result == null) result = new int[2]; |
| 241 | result[0] = lp.x; |
| 242 | result[1] = lp.y; |
| 243 | return result; |
| 244 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 245 | } |
| 246 | |