blob: dd94175b6894749784bae8c6fba87a4b240ea9fc [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 {
Joe Onorato00acb122009-08-04 16:04:30 -040035 private Bitmap mBitmap;
36 private Paint mPaint;
37 private int mRegistrationX;
38 private int mRegistrationY;
39
Winson Chungb8c69f32011-10-19 21:36:08 -070040 private Point mDragVisualizeOffset = null;
Adam Cohene3e27a82011-04-15 12:07:39 -070041 private Rect mDragRegion = null;
Adam Cohen8dfcba42011-07-07 16:38:18 -070042 private DragLayer mDragLayer = null;
Adam Cohenfc53cd22011-07-20 15:45:11 -070043 private boolean mHasDrawn = false;
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 /**
52 * Construct the drag view.
53 * <p>
54 * The registration point is the point inside our view that the touch events should
55 * be centered upon.
56 *
Adam Cohen8dfcba42011-07-07 16:38:18 -070057 * @param launcher The Launcher instance
Joe Onorato00acb122009-08-04 16:04:30 -040058 * @param bitmap The view that we're dragging around. We scale it up when we draw it.
59 * @param registrationX The x coordinate of the registration point.
60 * @param registrationY The y coordinate of the registration point.
61 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070062 public DragView(Launcher launcher, Bitmap bitmap, int registrationX, int registrationY,
Joe Onorato5162ea92009-09-03 09:39:42 -070063 int left, int top, int width, int height) {
Adam Cohen8dfcba42011-07-07 16:38:18 -070064 super(launcher);
65 mDragLayer = launcher.getDragLayer();
Joe Onorato00acb122009-08-04 16:04:30 -040066
Patrick Dubroyde7658b2010-09-27 11:15:43 -070067 final Resources res = getResources();
68 final int dragScale = res.getInteger(R.integer.config_dragViewExtraPixels);
69
Joe Onorato00acb122009-08-04 16:04:30 -040070 Matrix scale = new Matrix();
Patrick Dubroya669d792010-11-23 14:40:33 -080071 final float scaleFactor = (width + dragScale) / width;
72 if (scaleFactor != 1.0f) {
73 scale.setScale(scaleFactor, scaleFactor);
74 }
75
Adam Cohen8878a322011-03-28 13:18:42 -070076 final int offsetX = res.getDimensionPixelSize(R.dimen.dragViewOffsetX);
77 final int offsetY = res.getDimensionPixelSize(R.dimen.dragViewOffsetY);
Patrick Dubroya669d792010-11-23 14:40:33 -080078
79 // Animate the view into the correct position
80 mAnim = ValueAnimator.ofFloat(0.0f, 1.0f);
81 mAnim.setDuration(110);
82 mAnim.setInterpolator(new DecelerateInterpolator(2.5f));
83 mAnim.addUpdateListener(new AnimatorUpdateListener() {
84 @Override
85 public void onAnimationUpdate(ValueAnimator animation) {
86 final float value = (Float) animation.getAnimatedValue();
87
88 final int deltaX = (int) ((value * offsetX) - mOffsetX);
89 final int deltaY = (int) ((value * offsetY) - mOffsetY);
90
91 mOffsetX += deltaX;
92 mOffsetY += deltaY;
93
94 if (getParent() == null) {
95 animation.cancel();
96 } else {
Adam Cohen8dfcba42011-07-07 16:38:18 -070097 DragLayer.LayoutParams lp = mLayoutParams;
Patrick Dubroya669d792010-11-23 14:40:33 -080098 lp.x += deltaX;
99 lp.y += deltaY;
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
Joe Onorato5162ea92009-09-03 09:39:42 -0700105 mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height, scale, true);
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
Joe Onorato00acb122009-08-04 16:04:30 -0400211 /**
212 * Move the window containing this view.
213 *
Adam Cohen8dfcba42011-07-07 16:38:18 -0700214 * @param touchX the x coordinate the user touched in DragLayer coordinates
215 * @param touchY the y coordinate the user touched in DragLayer coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400216 */
217 void move(int touchX, int touchY) {
Adam Cohen8dfcba42011-07-07 16:38:18 -0700218 DragLayer.LayoutParams lp = mLayoutParams;
Patrick Dubroya669d792010-11-23 14:40:33 -0800219 lp.x = touchX - mRegistrationX + (int) mOffsetX;
220 lp.y = touchY - mRegistrationY + (int) mOffsetY;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700221 mDragLayer.requestLayout();
Joe Onorato00acb122009-08-04 16:04:30 -0400222 }
223
224 void remove() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700225 post(new Runnable() {
226 public void run() {
Adam Cohen8dfcba42011-07-07 16:38:18 -0700227 mDragLayer.removeView(DragView.this);
Adam Cohen3e8f8112011-07-02 18:03:00 -0700228 }
229 });
Joe Onorato00acb122009-08-04 16:04:30 -0400230 }
Patrick Dubroy6f133422011-02-24 12:16:12 -0800231
232 int[] getPosition(int[] result) {
Adam Cohen8dfcba42011-07-07 16:38:18 -0700233 DragLayer.LayoutParams lp = mLayoutParams;
Patrick Dubroy6f133422011-02-24 12:16:12 -0800234 if (result == null) result = new int[2];
235 result[0] = lp.x;
236 result[1] = lp.y;
237 return result;
238 }
Joe Onorato00acb122009-08-04 16:04:30 -0400239}
240