blob: 386cb55d300c7ce41daffbf12730f71d9a87c6dd [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;
Adam Cohenfc53cd22011-07-20 15:45:11 -070044 private boolean mHasDrawn = false;
Michael Jurkaa63c4522010-08-19 13:52:27 -070045
Patrick Dubroya669d792010-11-23 14:40:33 -080046 ValueAnimator mAnim;
Patrick Dubroya669d792010-11-23 14:40:33 -080047 private float mOffsetX = 0.0f;
48 private float mOffsetY = 0.0f;
Joe Onorato00acb122009-08-04 16:04:30 -040049
Adam Cohen8dfcba42011-07-07 16:38:18 -070050 private DragLayer.LayoutParams mLayoutParams;
Joe Onorato00acb122009-08-04 16:04:30 -040051
52 /**
53 * Construct the drag view.
54 * <p>
55 * The registration point is the point inside our view that the touch events should
56 * be centered upon.
57 *
Adam Cohen8dfcba42011-07-07 16:38:18 -070058 * @param launcher The Launcher instance
Joe Onorato00acb122009-08-04 16:04:30 -040059 * @param bitmap The view that we're dragging around. We scale it up when we draw it.
60 * @param registrationX The x coordinate of the registration point.
61 * @param registrationY The y coordinate of the registration point.
62 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070063 public DragView(Launcher launcher, Bitmap bitmap, int registrationX, int registrationY,
Joe Onorato5162ea92009-09-03 09:39:42 -070064 int left, int top, int width, int height) {
Adam Cohen8dfcba42011-07-07 16:38:18 -070065 super(launcher);
66 mDragLayer = launcher.getDragLayer();
Joe Onorato00acb122009-08-04 16:04:30 -040067
Patrick Dubroyde7658b2010-09-27 11:15:43 -070068 final Resources res = getResources();
69 final int dragScale = res.getInteger(R.integer.config_dragViewExtraPixels);
70
Joe Onorato00acb122009-08-04 16:04:30 -040071 Matrix scale = new Matrix();
Patrick Dubroya669d792010-11-23 14:40:33 -080072 final float scaleFactor = (width + dragScale) / width;
73 if (scaleFactor != 1.0f) {
74 scale.setScale(scaleFactor, scaleFactor);
75 }
76
Adam Cohen8878a322011-03-28 13:18:42 -070077 final int offsetX = res.getDimensionPixelSize(R.dimen.dragViewOffsetX);
78 final int offsetY = res.getDimensionPixelSize(R.dimen.dragViewOffsetY);
Patrick Dubroya669d792010-11-23 14:40:33 -080079
80 // Animate the view into the correct position
81 mAnim = ValueAnimator.ofFloat(0.0f, 1.0f);
82 mAnim.setDuration(110);
83 mAnim.setInterpolator(new DecelerateInterpolator(2.5f));
84 mAnim.addUpdateListener(new AnimatorUpdateListener() {
85 @Override
86 public void onAnimationUpdate(ValueAnimator animation) {
87 final float value = (Float) animation.getAnimatedValue();
88
89 final int deltaX = (int) ((value * offsetX) - mOffsetX);
90 final int deltaY = (int) ((value * offsetY) - mOffsetY);
91
92 mOffsetX += deltaX;
93 mOffsetY += deltaY;
94
95 if (getParent() == null) {
96 animation.cancel();
97 } else {
Adam Cohen8dfcba42011-07-07 16:38:18 -070098 DragLayer.LayoutParams lp = mLayoutParams;
Patrick Dubroya669d792010-11-23 14:40:33 -080099 lp.x += deltaX;
100 lp.y += deltaY;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700101 mDragLayer.requestLayout();
Patrick Dubroya669d792010-11-23 14:40:33 -0800102 }
103 }
104 });
Joe Onorato00acb122009-08-04 16:04:30 -0400105
Joe Onorato5162ea92009-09-03 09:39:42 -0700106 mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height, scale, true);
Adam Cohene3e27a82011-04-15 12:07:39 -0700107 setDragRegion(new Rect(0, 0, width, height));
Joe Onorato00acb122009-08-04 16:04:30 -0400108
109 // The point in our scaled bitmap that the touch events are located
Patrick Dubroya669d792010-11-23 14:40:33 -0800110 mRegistrationX = registrationX;
111 mRegistrationY = registrationY;
Patrick Dubroy62bbb3c2011-01-17 15:29:27 -0800112
113 // Force a measure, because Workspace uses getMeasuredHeight() before the layout pass
114 int ms = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
115 measure(ms, ms);
116 }
117
118 public float getOffsetY() {
119 return mOffsetY;
Joe Onorato00acb122009-08-04 16:04:30 -0400120 }
121
Michael Jurkaa63c4522010-08-19 13:52:27 -0700122 public int getDragRegionLeft() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700123 return mDragRegion.left;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700124 }
125
126 public int getDragRegionTop() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700127 return mDragRegion.top;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700128 }
129
130 public int getDragRegionWidth() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700131 return mDragRegion.width();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700132 }
133
134 public int getDragRegionHeight() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700135 return mDragRegion.height();
136 }
137
138 public void setDragRegion(Rect r) {
139 mDragRegion = r;
140 }
141
142 public Rect getDragRegion() {
143 return mDragRegion;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700144 }
145
Joe Onorato00acb122009-08-04 16:04:30 -0400146 @Override
147 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Joe Onoratoeebd9242009-11-04 13:48:32 -0500148 setMeasuredDimension(mBitmap.getWidth(), mBitmap.getHeight());
Joe Onorato00acb122009-08-04 16:04:30 -0400149 }
150
151 @Override
152 protected void onDraw(Canvas canvas) {
153 if (false) {
154 // for debugging
155 Paint p = new Paint();
156 p.setStyle(Paint.Style.FILL);
157 p.setColor(0xaaffffff);
158 canvas.drawRect(0, 0, getWidth(), getHeight(), p);
159 }
Patrick Dubroya669d792010-11-23 14:40:33 -0800160
Adam Cohenfc53cd22011-07-20 15:45:11 -0700161 mHasDrawn = true;
Joe Onorato00acb122009-08-04 16:04:30 -0400162 canvas.drawBitmap(mBitmap, 0.0f, 0.0f, mPaint);
163 }
164
Joe Onorato00acb122009-08-04 16:04:30 -0400165 public void setPaint(Paint paint) {
166 mPaint = paint;
167 invalidate();
168 }
169
Adam Cohenfc53cd22011-07-20 15:45:11 -0700170 public boolean hasDrawn() {
171 return mHasDrawn;
172 }
173
Adam Cohen3e8f8112011-07-02 18:03:00 -0700174 @Override
175 public void setAlpha(float alpha) {
176 super.setAlpha(alpha);
177 if (mPaint == null) {
178 mPaint = new Paint();
179 }
180 mPaint.setAlpha((int) (255 * alpha));
181 invalidate();
182 }
183
Joe Onorato00acb122009-08-04 16:04:30 -0400184 /**
185 * Create a window containing this view and show it.
186 *
187 * @param windowToken obtained from v.getWindowToken() from one of your views
Adam Cohen8dfcba42011-07-07 16:38:18 -0700188 * @param touchX the x coordinate the user touched in DragLayer coordinates
189 * @param touchY the y coordinate the user touched in DragLayer coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400190 */
Adam Cohen8dfcba42011-07-07 16:38:18 -0700191 public void show(int touchX, int touchY) {
192 mDragLayer.addView(this);
193 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(0, 0);
194 lp.width = mBitmap.getWidth();
195 lp.height = mBitmap.getHeight();
196 lp.x = touchX - mRegistrationX;
197 lp.y = touchY - mRegistrationY;
198 lp.customPosition = true;
199 setLayoutParams(lp);
Joe Onorato00acb122009-08-04 16:04:30 -0400200 mLayoutParams = lp;
Patrick Dubroya669d792010-11-23 14:40:33 -0800201 mAnim.start();
Joe Onorato00acb122009-08-04 16:04:30 -0400202 }
Adam Cohen716b51e2011-06-30 12:09:54 -0700203
Joe Onorato00acb122009-08-04 16:04:30 -0400204 /**
205 * Move the window containing this view.
206 *
Adam Cohen8dfcba42011-07-07 16:38:18 -0700207 * @param touchX the x coordinate the user touched in DragLayer coordinates
208 * @param touchY the y coordinate the user touched in DragLayer coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400209 */
210 void move(int touchX, int touchY) {
Adam Cohen8dfcba42011-07-07 16:38:18 -0700211 DragLayer.LayoutParams lp = mLayoutParams;
Patrick Dubroya669d792010-11-23 14:40:33 -0800212 lp.x = touchX - mRegistrationX + (int) mOffsetX;
213 lp.y = touchY - mRegistrationY + (int) mOffsetY;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700214 mDragLayer.requestLayout();
Joe Onorato00acb122009-08-04 16:04:30 -0400215 }
216
217 void remove() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700218 post(new Runnable() {
219 public void run() {
Adam Cohen8dfcba42011-07-07 16:38:18 -0700220 mDragLayer.removeView(DragView.this);
Adam Cohen3e8f8112011-07-02 18:03:00 -0700221 }
222 });
Joe Onorato00acb122009-08-04 16:04:30 -0400223 }
Patrick Dubroy6f133422011-02-24 12:16:12 -0800224
225 int[] getPosition(int[] result) {
Adam Cohen8dfcba42011-07-07 16:38:18 -0700226 DragLayer.LayoutParams lp = mLayoutParams;
Patrick Dubroy6f133422011-02-24 12:16:12 -0800227 if (result == null) result = new int[2];
228 result[0] = lp.x;
229 result[1] = lp.y;
230 return result;
231 }
Joe Onorato00acb122009-08-04 16:04:30 -0400232}
233