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