blob: ab997446d0582be47fdd9e4e310d0b4bf3d64187 [file] [log] [blame]
Joe Onorato00acb122009-08-04 16:04:30 -04001/*
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
18package com.android.launcher2;
19
Patrick Dubroya669d792010-11-23 14:40:33 -080020import android.animation.ValueAnimator;
21import android.animation.ValueAnimator.AnimatorUpdateListener;
Joe Onorato00acb122009-08-04 16:04:30 -040022import android.content.Context;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070023import android.content.res.Resources;
Joe Onorato00acb122009-08-04 16:04:30 -040024import android.graphics.Bitmap;
25import android.graphics.Canvas;
26import android.graphics.Matrix;
27import android.graphics.Paint;
28import android.graphics.PixelFormat;
Joe Onorato00acb122009-08-04 16:04:30 -040029import android.os.IBinder;
Joe Onorato00acb122009-08-04 16:04:30 -040030import android.view.Gravity;
31import android.view.View;
32import android.view.ViewGroup;
Joe Onorato00acb122009-08-04 16:04:30 -040033import android.view.WindowManager;
34import android.view.WindowManagerImpl;
Patrick Dubroya669d792010-11-23 14:40:33 -080035import android.view.animation.DecelerateInterpolator;
Joe Onorato00acb122009-08-04 16:04:30 -040036
Adam Cohen120980b2010-12-08 11:05:37 -080037import com.android.launcher.R;
38
Patrick Dubroya669d792010-11-23 14:40:33 -080039public class DragView extends View {
Joe Onorato00acb122009-08-04 16:04:30 -040040 private Bitmap mBitmap;
41 private Paint mPaint;
42 private int mRegistrationX;
43 private int mRegistrationY;
44
Michael Jurkaa63c4522010-08-19 13:52:27 -070045 private int mDragRegionLeft = 0;
46 private int mDragRegionTop = 0;
47 private int mDragRegionWidth;
48 private int mDragRegionHeight;
49
Patrick Dubroya669d792010-11-23 14:40:33 -080050 ValueAnimator mAnim;
Patrick Dubroya669d792010-11-23 14:40:33 -080051 private float mOffsetX = 0.0f;
52 private float mOffsetY = 0.0f;
Joe Onorato00acb122009-08-04 16:04:30 -040053
54 private WindowManager.LayoutParams mLayoutParams;
55 private WindowManager mWindowManager;
56
57 /**
Patrick Dubroya669d792010-11-23 14:40:33 -080058 * 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 Onorato00acb122009-08-04 16:04:30 -040065 * 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 Onorato5162ea92009-09-03 09:39:42 -070075 public DragView(Context context, Bitmap bitmap, int registrationX, int registrationY,
76 int left, int top, int width, int height) {
Joe Onorato00acb122009-08-04 16:04:30 -040077 super(context);
78
Patrick Dubroyde7658b2010-09-27 11:15:43 -070079 final Resources res = getResources();
80 final int dragScale = res.getInteger(R.integer.config_dragViewExtraPixels);
81
Joe Onorato00acb122009-08-04 16:04:30 -040082 mWindowManager = WindowManagerImpl.getDefault();
Joe Onorato00acb122009-08-04 16:04:30 -040083
Joe Onorato00acb122009-08-04 16:04:30 -040084 Matrix scale = new Matrix();
Patrick Dubroya669d792010-11-23 14:40:33 -080085 final float scaleFactor = (width + dragScale) / width;
86 if (scaleFactor != 1.0f) {
87 scale.setScale(scaleFactor, scaleFactor);
88 }
89
Adam Cohen8878a322011-03-28 13:18:42 -070090 final int offsetX = res.getDimensionPixelSize(R.dimen.dragViewOffsetX);
91 final int offsetY = res.getDimensionPixelSize(R.dimen.dragViewOffsetY);
Patrick Dubroya669d792010-11-23 14:40:33 -080092
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 Onorato00acb122009-08-04 16:04:30 -0400118
Joe Onorato5162ea92009-09-03 09:39:42 -0700119 mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height, scale, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700120 setDragRegion(0, 0, width, height);
Joe Onorato00acb122009-08-04 16:04:30 -0400121
122 // The point in our scaled bitmap that the touch events are located
Patrick Dubroya669d792010-11-23 14:40:33 -0800123 mRegistrationX = registrationX;
124 mRegistrationY = registrationY;
Patrick Dubroy62bbb3c2011-01-17 15:29:27 -0800125
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 Onorato00acb122009-08-04 16:04:30 -0400133 }
134
Michael Jurkaa63c4522010-08-19 13:52:27 -0700135 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 Dubroya669d792010-11-23 14:40:33 -0800142 public void setOnDrawRunnable(Runnable r) {
143 mOnDrawRunnable = r;
144 }
145
Michael Jurkaa63c4522010-08-19 13:52:27 -0700146 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 Onorato00acb122009-08-04 16:04:30 -0400162 @Override
163 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Joe Onoratoeebd9242009-11-04 13:48:32 -0500164 setMeasuredDimension(mBitmap.getWidth(), mBitmap.getHeight());
Joe Onorato00acb122009-08-04 16:04:30 -0400165 }
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 Dubroya669d792010-11-23 14:40:33 -0800176
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 Onorato00acb122009-08-04 16:04:30 -0400183 }
Patrick Dubroya669d792010-11-23 14:40:33 -0800184
Joe Onorato00acb122009-08-04 16:04:30 -0400185 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 Onorato00acb122009-08-04 16:04:30 -0400194 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 Dubroya669d792010-11-23 14:40:33 -0800215 touchX - mRegistrationX, touchY - mRegistrationY,
Joe Onorato00acb122009-08-04 16:04:30 -0400216 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 Dubroya669d792010-11-23 14:40:33 -0800229 mAnim.start();
Joe Onorato00acb122009-08-04 16:04:30 -0400230 }
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 Dubroya669d792010-11-23 14:40:33 -0800240 lp.x = touchX - mRegistrationX + (int) mOffsetX;
241 lp.y = touchY - mRegistrationY + (int) mOffsetY;
Joe Onorato00acb122009-08-04 16:04:30 -0400242 mWindowManager.updateViewLayout(this, lp);
243 }
244
245 void remove() {
246 mWindowManager.removeView(this);
247 }
Patrick Dubroy6f133422011-02-24 12:16:12 -0800248
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 Onorato00acb122009-08-04 16:04:30 -0400256}
257