blob: 7be70a25ad7900efa22ee6fd04586d55b7042a97 [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;
Winson Chungb8c69f32011-10-19 21:36:08 -070027import android.graphics.Point;
Adam Cohene3e27a82011-04-15 12:07:39 -070028import android.graphics.Rect;
Joe Onorato00acb122009-08-04 16:04:30 -040029import android.view.View;
Patrick Dubroya669d792010-11-23 14:40:33 -080030import android.view.animation.DecelerateInterpolator;
Joe Onorato00acb122009-08-04 16:04:30 -040031
Adam Cohen120980b2010-12-08 11:05:37 -080032import com.android.launcher.R;
33
Patrick Dubroya669d792010-11-23 14:40:33 -080034public class DragView extends View {
Winson Chung42c29ae2012-02-06 16:43:52 -080035 private static float sDragAlpha = 0.8f;
36
Joe Onorato00acb122009-08-04 16:04:30 -040037 private Bitmap mBitmap;
38 private Paint mPaint;
39 private int mRegistrationX;
40 private int mRegistrationY;
41
Winson Chungb8c69f32011-10-19 21:36:08 -070042 private Point mDragVisualizeOffset = null;
Adam Cohene3e27a82011-04-15 12:07:39 -070043 private Rect mDragRegion = null;
Adam Cohen8dfcba42011-07-07 16:38:18 -070044 private DragLayer mDragLayer = null;
Adam Cohenfc53cd22011-07-20 15:45:11 -070045 private boolean mHasDrawn = false;
Michael Jurkaa63c4522010-08-19 13:52:27 -070046
Patrick Dubroya669d792010-11-23 14:40:33 -080047 ValueAnimator mAnim;
Patrick Dubroya669d792010-11-23 14:40:33 -080048 private float mOffsetX = 0.0f;
49 private float mOffsetY = 0.0f;
Joe Onorato00acb122009-08-04 16:04:30 -040050
Adam Cohen8dfcba42011-07-07 16:38:18 -070051 private DragLayer.LayoutParams mLayoutParams;
Joe Onorato00acb122009-08-04 16:04:30 -040052
53 /**
54 * Construct the drag view.
55 * <p>
56 * The registration point is the point inside our view that the touch events should
57 * be centered upon.
58 *
Adam Cohen8dfcba42011-07-07 16:38:18 -070059 * @param launcher The Launcher instance
Joe Onorato00acb122009-08-04 16:04:30 -040060 * @param bitmap The view that we're dragging around. We scale it up when we draw it.
61 * @param registrationX The x coordinate of the registration point.
62 * @param registrationY The y coordinate of the registration point.
63 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070064 public DragView(Launcher launcher, Bitmap bitmap, int registrationX, int registrationY,
Joe Onorato5162ea92009-09-03 09:39:42 -070065 int left, int top, int width, int height) {
Adam Cohen8dfcba42011-07-07 16:38:18 -070066 super(launcher);
67 mDragLayer = launcher.getDragLayer();
Joe Onorato00acb122009-08-04 16:04:30 -040068
Patrick Dubroyde7658b2010-09-27 11:15:43 -070069 final Resources res = getResources();
Winson Chung42c29ae2012-02-06 16:43:52 -080070 final float scale = res.getInteger(R.integer.config_dragViewScaleFactor) / 100f;
71 final float offsetX = res.getDimensionPixelSize(R.dimen.dragViewOffsetX);
72 final float offsetY = res.getDimensionPixelSize(R.dimen.dragViewOffsetY);
Patrick Dubroya669d792010-11-23 14:40:33 -080073
74 // Animate the view into the correct position
75 mAnim = ValueAnimator.ofFloat(0.0f, 1.0f);
Winson Chung42c29ae2012-02-06 16:43:52 -080076 mAnim.setDuration(150);
Patrick Dubroya669d792010-11-23 14:40:33 -080077 mAnim.setInterpolator(new DecelerateInterpolator(2.5f));
78 mAnim.addUpdateListener(new AnimatorUpdateListener() {
79 @Override
80 public void onAnimationUpdate(ValueAnimator animation) {
81 final float value = (Float) animation.getAnimatedValue();
82
83 final int deltaX = (int) ((value * offsetX) - mOffsetX);
84 final int deltaY = (int) ((value * offsetY) - mOffsetY);
85
86 mOffsetX += deltaX;
87 mOffsetY += deltaY;
Winson Chung42c29ae2012-02-06 16:43:52 -080088 setScaleX(1f + (value * (scale - 1f)));
89 setScaleY(1f + (value * (scale - 1f)));
90 setAlpha(sDragAlpha * value + (1f - value));
Patrick Dubroya669d792010-11-23 14:40:33 -080091
92 if (getParent() == null) {
93 animation.cancel();
94 } else {
Adam Cohen8dfcba42011-07-07 16:38:18 -070095 DragLayer.LayoutParams lp = mLayoutParams;
Patrick Dubroya669d792010-11-23 14:40:33 -080096 lp.x += deltaX;
97 lp.y += deltaY;
Winson Chung42c29ae2012-02-06 16:43:52 -080098 lp.width = mBitmap.getWidth();
99 lp.height = mBitmap.getHeight();
Adam Cohen8dfcba42011-07-07 16:38:18 -0700100 mDragLayer.requestLayout();
Patrick Dubroya669d792010-11-23 14:40:33 -0800101 }
102 }
103 });
Joe Onorato00acb122009-08-04 16:04:30 -0400104
Winson Chung42c29ae2012-02-06 16:43:52 -0800105 mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height);
Adam Cohene3e27a82011-04-15 12:07:39 -0700106 setDragRegion(new Rect(0, 0, width, height));
Joe Onorato00acb122009-08-04 16:04:30 -0400107
108 // The point in our scaled bitmap that the touch events are located
Patrick Dubroya669d792010-11-23 14:40:33 -0800109 mRegistrationX = registrationX;
110 mRegistrationY = registrationY;
Patrick Dubroy62bbb3c2011-01-17 15:29:27 -0800111
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 Onorato00acb122009-08-04 16:04:30 -0400119 }
120
Michael Jurkaa63c4522010-08-19 13:52:27 -0700121 public int getDragRegionLeft() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700122 return mDragRegion.left;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700123 }
124
125 public int getDragRegionTop() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700126 return mDragRegion.top;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700127 }
128
129 public int getDragRegionWidth() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700130 return mDragRegion.width();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700131 }
132
133 public int getDragRegionHeight() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700134 return mDragRegion.height();
135 }
136
Winson Chungb8c69f32011-10-19 21:36:08 -0700137 public void setDragVisualizeOffset(Point p) {
138 mDragVisualizeOffset = p;
139 }
140
141 public Point getDragVisualizeOffset() {
142 return mDragVisualizeOffset;
143 }
144
Adam Cohene3e27a82011-04-15 12:07:39 -0700145 public void setDragRegion(Rect r) {
146 mDragRegion = r;
147 }
148
149 public Rect getDragRegion() {
150 return mDragRegion;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700151 }
152
Joe Onorato00acb122009-08-04 16:04:30 -0400153 @Override
154 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Joe Onoratoeebd9242009-11-04 13:48:32 -0500155 setMeasuredDimension(mBitmap.getWidth(), mBitmap.getHeight());
Joe Onorato00acb122009-08-04 16:04:30 -0400156 }
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 Dubroya669d792010-11-23 14:40:33 -0800167
Adam Cohenfc53cd22011-07-20 15:45:11 -0700168 mHasDrawn = true;
Joe Onorato00acb122009-08-04 16:04:30 -0400169 canvas.drawBitmap(mBitmap, 0.0f, 0.0f, mPaint);
170 }
171
Joe Onorato00acb122009-08-04 16:04:30 -0400172 public void setPaint(Paint paint) {
173 mPaint = paint;
174 invalidate();
175 }
176
Adam Cohenfc53cd22011-07-20 15:45:11 -0700177 public boolean hasDrawn() {
178 return mHasDrawn;
179 }
180
Adam Cohen3e8f8112011-07-02 18:03:00 -0700181 @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 Onorato00acb122009-08-04 16:04:30 -0400191 /**
192 * Create a window containing this view and show it.
193 *
194 * @param windowToken obtained from v.getWindowToken() from one of your views
Adam Cohen8dfcba42011-07-07 16:38:18 -0700195 * @param touchX the x coordinate the user touched in DragLayer coordinates
196 * @param touchY the y coordinate the user touched in DragLayer coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400197 */
Adam Cohen8dfcba42011-07-07 16:38:18 -0700198 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 Onorato00acb122009-08-04 16:04:30 -0400207 mLayoutParams = lp;
Patrick Dubroya669d792010-11-23 14:40:33 -0800208 mAnim.start();
Joe Onorato00acb122009-08-04 16:04:30 -0400209 }
Adam Cohen716b51e2011-06-30 12:09:54 -0700210
Winson Chung42c29ae2012-02-06 16:43:52 -0800211 public void cancelAnimation() {
212 if (mAnim != null && mAnim.isRunning()) {
213 mAnim.cancel();
214 }
215 }
216
217 public void resetLayoutParams() {
218 DragLayer.LayoutParams lp = mLayoutParams;
219 lp.x = lp.y = 0;
220 mOffsetX = mOffsetY = 0;
221 }
222
Joe Onorato00acb122009-08-04 16:04:30 -0400223 /**
224 * Move the window containing this view.
225 *
Adam Cohen8dfcba42011-07-07 16:38:18 -0700226 * @param touchX the x coordinate the user touched in DragLayer coordinates
227 * @param touchY the y coordinate the user touched in DragLayer coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400228 */
229 void move(int touchX, int touchY) {
Adam Cohen8dfcba42011-07-07 16:38:18 -0700230 DragLayer.LayoutParams lp = mLayoutParams;
Patrick Dubroya669d792010-11-23 14:40:33 -0800231 lp.x = touchX - mRegistrationX + (int) mOffsetX;
232 lp.y = touchY - mRegistrationY + (int) mOffsetY;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700233 mDragLayer.requestLayout();
Joe Onorato00acb122009-08-04 16:04:30 -0400234 }
235
236 void remove() {
Winson Chung42c29ae2012-02-06 16:43:52 -0800237 if (getParent() != null) {
238 mDragLayer.removeView(DragView.this);
239 }
Joe Onorato00acb122009-08-04 16:04:30 -0400240 }
Patrick Dubroy6f133422011-02-24 12:16:12 -0800241
242 int[] getPosition(int[] result) {
Adam Cohen8dfcba42011-07-07 16:38:18 -0700243 DragLayer.LayoutParams lp = mLayoutParams;
Patrick Dubroy6f133422011-02-24 12:16:12 -0800244 if (result == null) result = new int[2];
245 result[0] = lp.x;
246 result[1] = lp.y;
247 return result;
248 }
Joe Onorato00acb122009-08-04 16:04:30 -0400249}
250