blob: 15d9c544963cd7a5e99ee62e9ee8092d77436d77 [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;
Adam Cohened66b2b2012-01-23 17:28:51 -080036 private Bitmap mCrossFadeBitmap;
Joe Onorato00acb122009-08-04 16:04:30 -040037 private Paint mPaint;
38 private int mRegistrationX;
39 private int mRegistrationY;
40
Winson Chungb8c69f32011-10-19 21:36:08 -070041 private Point mDragVisualizeOffset = null;
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;
Adam Cohened66b2b2012-01-23 17:28:51 -080045 private float mCrossFadeProgress = 0f;
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 Chunga1cdab02012-02-13 13:03:52 -080070 final int dragScale = res.getInteger(R.integer.config_dragViewExtraPixels);
71
72 Matrix scale = new Matrix();
73 final float scaleFactor = (width + dragScale) / width;
74 if (scaleFactor != 1.0f) {
75 scale.setScale(scaleFactor, scaleFactor);
76 }
77
78 final int offsetX = res.getDimensionPixelSize(R.dimen.dragViewOffsetX);
79 final int offsetY = res.getDimensionPixelSize(R.dimen.dragViewOffsetY);
Patrick Dubroya669d792010-11-23 14:40:33 -080080
81 // Animate the view into the correct position
82 mAnim = ValueAnimator.ofFloat(0.0f, 1.0f);
Winson Chunga1cdab02012-02-13 13:03:52 -080083 mAnim.setDuration(110);
Patrick Dubroya669d792010-11-23 14:40:33 -080084 mAnim.setInterpolator(new DecelerateInterpolator(2.5f));
85 mAnim.addUpdateListener(new AnimatorUpdateListener() {
86 @Override
87 public void onAnimationUpdate(ValueAnimator animation) {
88 final float value = (Float) animation.getAnimatedValue();
89
90 final int deltaX = (int) ((value * offsetX) - mOffsetX);
91 final int deltaY = (int) ((value * offsetY) - mOffsetY);
92
93 mOffsetX += deltaX;
94 mOffsetY += deltaY;
95
96 if (getParent() == null) {
97 animation.cancel();
98 } else {
Adam Cohen8dfcba42011-07-07 16:38:18 -070099 DragLayer.LayoutParams lp = mLayoutParams;
Patrick Dubroya669d792010-11-23 14:40:33 -0800100 lp.x += deltaX;
101 lp.y += deltaY;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700102 mDragLayer.requestLayout();
Patrick Dubroya669d792010-11-23 14:40:33 -0800103 }
104 }
105 });
Joe Onorato00acb122009-08-04 16:04:30 -0400106
Winson Chunga1cdab02012-02-13 13:03:52 -0800107 mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height, scale, true);
Adam Cohene3e27a82011-04-15 12:07:39 -0700108 setDragRegion(new Rect(0, 0, width, height));
Joe Onorato00acb122009-08-04 16:04:30 -0400109
110 // The point in our scaled bitmap that the touch events are located
Patrick Dubroya669d792010-11-23 14:40:33 -0800111 mRegistrationX = registrationX;
112 mRegistrationY = registrationY;
Patrick Dubroy62bbb3c2011-01-17 15:29:27 -0800113
114 // Force a measure, because Workspace uses getMeasuredHeight() before the layout pass
115 int ms = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
116 measure(ms, ms);
117 }
118
119 public float getOffsetY() {
120 return mOffsetY;
Joe Onorato00acb122009-08-04 16:04:30 -0400121 }
122
Michael Jurkaa63c4522010-08-19 13:52:27 -0700123 public int getDragRegionLeft() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700124 return mDragRegion.left;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700125 }
126
127 public int getDragRegionTop() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700128 return mDragRegion.top;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700129 }
130
131 public int getDragRegionWidth() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700132 return mDragRegion.width();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700133 }
134
135 public int getDragRegionHeight() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700136 return mDragRegion.height();
137 }
138
Winson Chungb8c69f32011-10-19 21:36:08 -0700139 public void setDragVisualizeOffset(Point p) {
140 mDragVisualizeOffset = p;
141 }
142
143 public Point getDragVisualizeOffset() {
144 return mDragVisualizeOffset;
145 }
146
Adam Cohene3e27a82011-04-15 12:07:39 -0700147 public void setDragRegion(Rect r) {
148 mDragRegion = r;
149 }
150
151 public Rect getDragRegion() {
152 return mDragRegion;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700153 }
154
Joe Onorato00acb122009-08-04 16:04:30 -0400155 @Override
156 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Joe Onoratoeebd9242009-11-04 13:48:32 -0500157 setMeasuredDimension(mBitmap.getWidth(), mBitmap.getHeight());
Joe Onorato00acb122009-08-04 16:04:30 -0400158 }
159
160 @Override
161 protected void onDraw(Canvas canvas) {
162 if (false) {
163 // for debugging
164 Paint p = new Paint();
165 p.setStyle(Paint.Style.FILL);
166 p.setColor(0xaaffffff);
167 canvas.drawRect(0, 0, getWidth(), getHeight(), p);
168 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800169 if (mPaint == null) {
170 mPaint = new Paint();
171 }
Patrick Dubroya669d792010-11-23 14:40:33 -0800172
Adam Cohenfc53cd22011-07-20 15:45:11 -0700173 mHasDrawn = true;
Adam Cohened66b2b2012-01-23 17:28:51 -0800174 boolean crossFade = mCrossFadeProgress > 0 && mCrossFadeBitmap != null;
175 if (crossFade) {
176 int alpha = crossFade ? (int) (255 * (1 - mCrossFadeProgress)) : 255;
177 mPaint.setAlpha(alpha);
178 }
Joe Onorato00acb122009-08-04 16:04:30 -0400179 canvas.drawBitmap(mBitmap, 0.0f, 0.0f, mPaint);
Adam Cohened66b2b2012-01-23 17:28:51 -0800180 if (crossFade) {
181 mPaint.setAlpha((int) (255 * mCrossFadeProgress));
182 canvas.save();
183 float sX = (mBitmap.getWidth() * 1.0f) / mCrossFadeBitmap.getWidth();
184 float sY = (mBitmap.getHeight() * 1.0f) / mCrossFadeBitmap.getHeight();
185 canvas.scale(sX, sY);
186 canvas.drawBitmap(mCrossFadeBitmap, 0.0f, 0.0f, mPaint);
187 canvas.restore();
188 }
189 }
190
191 public void setCrossFadeBitmap(Bitmap crossFadeBitmap) {
192 mCrossFadeBitmap = crossFadeBitmap;
193 }
194
195 public void crossFade(int duration) {
196 ValueAnimator va = ValueAnimator.ofFloat(0f, 1f);
197 va.setDuration(duration);
198 va.setInterpolator(new DecelerateInterpolator(1.5f));
199 va.addUpdateListener(new AnimatorUpdateListener() {
200 @Override
201 public void onAnimationUpdate(ValueAnimator animation) {
202 mCrossFadeProgress = animation.getAnimatedFraction();
203 }
204 });
205 va.start();
Joe Onorato00acb122009-08-04 16:04:30 -0400206 }
207
Joe Onorato00acb122009-08-04 16:04:30 -0400208 public void setPaint(Paint paint) {
209 mPaint = paint;
210 invalidate();
211 }
212
Adam Cohenfc53cd22011-07-20 15:45:11 -0700213 public boolean hasDrawn() {
214 return mHasDrawn;
215 }
216
Adam Cohen3e8f8112011-07-02 18:03:00 -0700217 @Override
218 public void setAlpha(float alpha) {
219 super.setAlpha(alpha);
220 if (mPaint == null) {
221 mPaint = new Paint();
222 }
223 mPaint.setAlpha((int) (255 * alpha));
224 invalidate();
225 }
226
Joe Onorato00acb122009-08-04 16:04:30 -0400227 /**
228 * Create a window containing this view and show it.
229 *
230 * @param windowToken obtained from v.getWindowToken() from one of your views
Adam Cohen8dfcba42011-07-07 16:38:18 -0700231 * @param touchX the x coordinate the user touched in DragLayer coordinates
232 * @param touchY the y coordinate the user touched in DragLayer coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400233 */
Adam Cohen8dfcba42011-07-07 16:38:18 -0700234 public void show(int touchX, int touchY) {
235 mDragLayer.addView(this);
236 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(0, 0);
237 lp.width = mBitmap.getWidth();
238 lp.height = mBitmap.getHeight();
239 lp.x = touchX - mRegistrationX;
240 lp.y = touchY - mRegistrationY;
241 lp.customPosition = true;
242 setLayoutParams(lp);
Joe Onorato00acb122009-08-04 16:04:30 -0400243 mLayoutParams = lp;
Patrick Dubroya669d792010-11-23 14:40:33 -0800244 mAnim.start();
Joe Onorato00acb122009-08-04 16:04:30 -0400245 }
Adam Cohen716b51e2011-06-30 12:09:54 -0700246
Joe Onorato00acb122009-08-04 16:04:30 -0400247 /**
248 * Move the window containing this view.
249 *
Adam Cohen8dfcba42011-07-07 16:38:18 -0700250 * @param touchX the x coordinate the user touched in DragLayer coordinates
251 * @param touchY the y coordinate the user touched in DragLayer coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400252 */
253 void move(int touchX, int touchY) {
Adam Cohen8dfcba42011-07-07 16:38:18 -0700254 DragLayer.LayoutParams lp = mLayoutParams;
Patrick Dubroya669d792010-11-23 14:40:33 -0800255 lp.x = touchX - mRegistrationX + (int) mOffsetX;
256 lp.y = touchY - mRegistrationY + (int) mOffsetY;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700257 mDragLayer.requestLayout();
Joe Onorato00acb122009-08-04 16:04:30 -0400258 }
259
260 void remove() {
Winson Chunga1cdab02012-02-13 13:03:52 -0800261 mDragLayer.removeView(DragView.this);
Joe Onorato00acb122009-08-04 16:04:30 -0400262 }
Patrick Dubroy6f133422011-02-24 12:16:12 -0800263
264 int[] getPosition(int[] result) {
Adam Cohen8dfcba42011-07-07 16:38:18 -0700265 DragLayer.LayoutParams lp = mLayoutParams;
Patrick Dubroy6f133422011-02-24 12:16:12 -0800266 if (result == null) result = new int[2];
267 result[0] = lp.x;
268 result[1] = lp.y;
269 return result;
270 }
Joe Onorato00acb122009-08-04 16:04:30 -0400271}
272