blob: 1c75e470fc2e0836e773ea830136b00f8b332086 [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;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070022import android.content.res.Resources;
Joe Onorato00acb122009-08-04 16:04:30 -040023import android.graphics.Bitmap;
24import android.graphics.Canvas;
25import android.graphics.Matrix;
26import android.graphics.Paint;
Adam Cohene3e27a82011-04-15 12:07:39 -070027import android.graphics.Rect;
Joe Onorato00acb122009-08-04 16:04:30 -040028import android.os.IBinder;
Joe Onorato00acb122009-08-04 16:04:30 -040029import android.view.View;
Joe Onorato00acb122009-08-04 16:04:30 -040030import android.view.WindowManager;
31import android.view.WindowManagerImpl;
Patrick Dubroya669d792010-11-23 14:40:33 -080032import android.view.animation.DecelerateInterpolator;
Joe Onorato00acb122009-08-04 16:04:30 -040033
Adam Cohen120980b2010-12-08 11:05:37 -080034import com.android.launcher.R;
35
Patrick Dubroya669d792010-11-23 14:40:33 -080036public class DragView extends View {
Joe Onorato00acb122009-08-04 16:04:30 -040037 private Bitmap mBitmap;
38 private Paint mPaint;
39 private int mRegistrationX;
40 private int mRegistrationY;
41
Adam Cohene3e27a82011-04-15 12:07:39 -070042 private Rect mDragRegion = null;
Adam Cohen8dfcba42011-07-07 16:38:18 -070043 private DragLayer mDragLayer = null;
Michael Jurkaa63c4522010-08-19 13:52:27 -070044
Patrick Dubroya669d792010-11-23 14:40:33 -080045 ValueAnimator mAnim;
Patrick Dubroya669d792010-11-23 14:40:33 -080046 private float mOffsetX = 0.0f;
47 private float mOffsetY = 0.0f;
Joe Onorato00acb122009-08-04 16:04:30 -040048
Adam Cohen8dfcba42011-07-07 16:38:18 -070049 private DragLayer.LayoutParams mLayoutParams;
Joe Onorato00acb122009-08-04 16:04:30 -040050
51 /**
Patrick Dubroya669d792010-11-23 14:40:33 -080052 * 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 Onorato00acb122009-08-04 16:04:30 -040059 * 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 Cohen8dfcba42011-07-07 16:38:18 -070064 * @param launcher The Launcher instance
Joe Onorato00acb122009-08-04 16:04:30 -040065 * @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 Cohen8dfcba42011-07-07 16:38:18 -070069 public DragView(Launcher launcher, Bitmap bitmap, int registrationX, int registrationY,
Joe Onorato5162ea92009-09-03 09:39:42 -070070 int left, int top, int width, int height) {
Adam Cohen8dfcba42011-07-07 16:38:18 -070071 super(launcher);
72 mDragLayer = launcher.getDragLayer();
Joe Onorato00acb122009-08-04 16:04:30 -040073
Patrick Dubroyde7658b2010-09-27 11:15:43 -070074 final Resources res = getResources();
75 final int dragScale = res.getInteger(R.integer.config_dragViewExtraPixels);
76
Joe Onorato00acb122009-08-04 16:04:30 -040077 Matrix scale = new Matrix();
Patrick Dubroya669d792010-11-23 14:40:33 -080078 final float scaleFactor = (width + dragScale) / width;
79 if (scaleFactor != 1.0f) {
80 scale.setScale(scaleFactor, scaleFactor);
81 }
82
Adam Cohen8878a322011-03-28 13:18:42 -070083 final int offsetX = res.getDimensionPixelSize(R.dimen.dragViewOffsetX);
84 final int offsetY = res.getDimensionPixelSize(R.dimen.dragViewOffsetY);
Patrick Dubroya669d792010-11-23 14:40:33 -080085
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 Cohen8dfcba42011-07-07 16:38:18 -0700104 DragLayer.LayoutParams lp = mLayoutParams;
Patrick Dubroya669d792010-11-23 14:40:33 -0800105 lp.x += deltaX;
106 lp.y += deltaY;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700107 mDragLayer.requestLayout();
Patrick Dubroya669d792010-11-23 14:40:33 -0800108 }
109 }
110 });
Joe Onorato00acb122009-08-04 16:04:30 -0400111
Joe Onorato5162ea92009-09-03 09:39:42 -0700112 mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height, scale, true);
Adam Cohene3e27a82011-04-15 12:07:39 -0700113 setDragRegion(new Rect(0, 0, width, height));
Joe Onorato00acb122009-08-04 16:04:30 -0400114
115 // The point in our scaled bitmap that the touch events are located
Patrick Dubroya669d792010-11-23 14:40:33 -0800116 mRegistrationX = registrationX;
117 mRegistrationY = registrationY;
Patrick Dubroy62bbb3c2011-01-17 15:29:27 -0800118
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 Onorato00acb122009-08-04 16:04:30 -0400126 }
127
Patrick Dubroya669d792010-11-23 14:40:33 -0800128 public void setOnDrawRunnable(Runnable r) {
129 mOnDrawRunnable = r;
130 }
131
Michael Jurkaa63c4522010-08-19 13:52:27 -0700132 public int getDragRegionLeft() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700133 return mDragRegion.left;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700134 }
135
136 public int getDragRegionTop() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700137 return mDragRegion.top;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700138 }
139
140 public int getDragRegionWidth() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700141 return mDragRegion.width();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700142 }
143
144 public int getDragRegionHeight() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700145 return mDragRegion.height();
146 }
147
148 public void setDragRegion(Rect r) {
149 mDragRegion = r;
150 }
151
152 public Rect getDragRegion() {
153 return mDragRegion;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700154 }
155
Joe Onorato00acb122009-08-04 16:04:30 -0400156 @Override
157 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Joe Onoratoeebd9242009-11-04 13:48:32 -0500158 setMeasuredDimension(mBitmap.getWidth(), mBitmap.getHeight());
Joe Onorato00acb122009-08-04 16:04:30 -0400159 }
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 Dubroya669d792010-11-23 14:40:33 -0800170
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 Onorato00acb122009-08-04 16:04:30 -0400177 }
Patrick Dubroya669d792010-11-23 14:40:33 -0800178
Joe Onorato00acb122009-08-04 16:04:30 -0400179 canvas.drawBitmap(mBitmap, 0.0f, 0.0f, mPaint);
180 }
181
Joe Onorato00acb122009-08-04 16:04:30 -0400182 public void setPaint(Paint paint) {
183 mPaint = paint;
184 invalidate();
185 }
186
Adam Cohen3e8f8112011-07-02 18:03:00 -0700187 @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 Onorato00acb122009-08-04 16:04:30 -0400197 /**
198 * Create a window containing this view and show it.
199 *
200 * @param windowToken obtained from v.getWindowToken() from one of your views
Adam Cohen8dfcba42011-07-07 16:38:18 -0700201 * @param touchX the x coordinate the user touched in DragLayer coordinates
202 * @param touchY the y coordinate the user touched in DragLayer coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400203 */
Adam Cohen8dfcba42011-07-07 16:38:18 -0700204 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 Onorato00acb122009-08-04 16:04:30 -0400213 mLayoutParams = lp;
Patrick Dubroya669d792010-11-23 14:40:33 -0800214 mAnim.start();
Joe Onorato00acb122009-08-04 16:04:30 -0400215 }
Adam Cohen716b51e2011-06-30 12:09:54 -0700216
Joe Onorato00acb122009-08-04 16:04:30 -0400217 /**
218 * Move the window containing this view.
219 *
Adam Cohen8dfcba42011-07-07 16:38:18 -0700220 * @param touchX the x coordinate the user touched in DragLayer coordinates
221 * @param touchY the y coordinate the user touched in DragLayer coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400222 */
223 void move(int touchX, int touchY) {
Adam Cohen8dfcba42011-07-07 16:38:18 -0700224 DragLayer.LayoutParams lp = mLayoutParams;
Patrick Dubroya669d792010-11-23 14:40:33 -0800225 lp.x = touchX - mRegistrationX + (int) mOffsetX;
226 lp.y = touchY - mRegistrationY + (int) mOffsetY;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700227 mDragLayer.requestLayout();
Joe Onorato00acb122009-08-04 16:04:30 -0400228 }
229
230 void remove() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700231 post(new Runnable() {
232 public void run() {
Adam Cohen8dfcba42011-07-07 16:38:18 -0700233 mDragLayer.removeView(DragView.this);
Adam Cohen3e8f8112011-07-02 18:03:00 -0700234 }
235 });
Joe Onorato00acb122009-08-04 16:04:30 -0400236 }
Patrick Dubroy6f133422011-02-24 12:16:12 -0800237
238 int[] getPosition(int[] result) {
Adam Cohen8dfcba42011-07-07 16:38:18 -0700239 DragLayer.LayoutParams lp = mLayoutParams;
Patrick Dubroy6f133422011-02-24 12:16:12 -0800240 if (result == null) result = new int[2];
241 result[0] = lp.x;
242 result[1] = lp.y;
243 return result;
244 }
Joe Onorato00acb122009-08-04 16:04:30 -0400245}
246