blob: 90b2083a099a46945899ca3d29d9fbb8517eff6a [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 Chung7bd1bbb2012-02-13 18:29:29 -080035 private static float sDragAlpha = 0.8f;
36
Joe Onorato00acb122009-08-04 16:04:30 -040037 private Bitmap mBitmap;
Adam Cohened66b2b2012-01-23 17:28:51 -080038 private Bitmap mCrossFadeBitmap;
Joe Onorato00acb122009-08-04 16:04:30 -040039 private Paint mPaint;
40 private int mRegistrationX;
41 private int mRegistrationY;
42
Winson Chungb8c69f32011-10-19 21:36:08 -070043 private Point mDragVisualizeOffset = null;
Adam Cohene3e27a82011-04-15 12:07:39 -070044 private Rect mDragRegion = null;
Adam Cohen8dfcba42011-07-07 16:38:18 -070045 private DragLayer mDragLayer = null;
Adam Cohenfc53cd22011-07-20 15:45:11 -070046 private boolean mHasDrawn = false;
Adam Cohened66b2b2012-01-23 17:28:51 -080047 private float mCrossFadeProgress = 0f;
Michael Jurkaa63c4522010-08-19 13:52:27 -070048
Patrick Dubroya669d792010-11-23 14:40:33 -080049 ValueAnimator mAnim;
Patrick Dubroya669d792010-11-23 14:40:33 -080050 private float mOffsetX = 0.0f;
51 private float mOffsetY = 0.0f;
Joe Onorato00acb122009-08-04 16:04:30 -040052
Joe Onorato00acb122009-08-04 16:04:30 -040053 /**
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 Chung7bd1bbb2012-02-13 18:29:29 -080070 final float offsetX = res.getDimensionPixelSize(R.dimen.dragViewOffsetX);
71 final float offsetY = res.getDimensionPixelSize(R.dimen.dragViewOffsetY);
72 final float scaleDps = res.getDimensionPixelSize(R.dimen.dragViewScale);
73 final float scale = (width + scaleDps) / width;
Patrick Dubroya669d792010-11-23 14:40:33 -080074
75 // Animate the view into the correct position
76 mAnim = ValueAnimator.ofFloat(0.0f, 1.0f);
Winson Chung7bd1bbb2012-02-13 18:29:29 -080077 mAnim.setDuration(150);
Patrick Dubroya669d792010-11-23 14:40:33 -080078 mAnim.setInterpolator(new DecelerateInterpolator(2.5f));
79 mAnim.addUpdateListener(new AnimatorUpdateListener() {
80 @Override
81 public void onAnimationUpdate(ValueAnimator animation) {
82 final float value = (Float) animation.getAnimatedValue();
83
84 final int deltaX = (int) ((value * offsetX) - mOffsetX);
85 final int deltaY = (int) ((value * offsetY) - mOffsetY);
86
87 mOffsetX += deltaX;
88 mOffsetY += deltaY;
Winson Chung7bd1bbb2012-02-13 18:29:29 -080089 setScaleX(1f + (value * (scale - 1f)));
90 setScaleY(1f + (value * (scale - 1f)));
91 setAlpha(sDragAlpha * value + (1f - value));
Patrick Dubroya669d792010-11-23 14:40:33 -080092
93 if (getParent() == null) {
94 animation.cancel();
95 } else {
Winson Chung7bd1bbb2012-02-13 18:29:29 -080096 setTranslationX(getTranslationX() + deltaX);
97 setTranslationY(getTranslationY() + deltaY);
Patrick Dubroya669d792010-11-23 14:40:33 -080098 }
99 }
100 });
Joe Onorato00acb122009-08-04 16:04:30 -0400101
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800102 mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height);
Adam Cohene3e27a82011-04-15 12:07:39 -0700103 setDragRegion(new Rect(0, 0, width, height));
Joe Onorato00acb122009-08-04 16:04:30 -0400104
105 // The point in our scaled bitmap that the touch events are located
Patrick Dubroya669d792010-11-23 14:40:33 -0800106 mRegistrationX = registrationX;
107 mRegistrationY = registrationY;
Patrick Dubroy62bbb3c2011-01-17 15:29:27 -0800108
109 // Force a measure, because Workspace uses getMeasuredHeight() before the layout pass
110 int ms = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
111 measure(ms, ms);
112 }
113
114 public float getOffsetY() {
115 return mOffsetY;
Joe Onorato00acb122009-08-04 16:04:30 -0400116 }
117
Michael Jurkaa63c4522010-08-19 13:52:27 -0700118 public int getDragRegionLeft() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700119 return mDragRegion.left;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700120 }
121
122 public int getDragRegionTop() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700123 return mDragRegion.top;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700124 }
125
126 public int getDragRegionWidth() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700127 return mDragRegion.width();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700128 }
129
130 public int getDragRegionHeight() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700131 return mDragRegion.height();
132 }
133
Winson Chungb8c69f32011-10-19 21:36:08 -0700134 public void setDragVisualizeOffset(Point p) {
135 mDragVisualizeOffset = p;
136 }
137
138 public Point getDragVisualizeOffset() {
139 return mDragVisualizeOffset;
140 }
141
Adam Cohene3e27a82011-04-15 12:07:39 -0700142 public void setDragRegion(Rect r) {
143 mDragRegion = r;
144 }
145
146 public Rect getDragRegion() {
147 return mDragRegion;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700148 }
149
Joe Onorato00acb122009-08-04 16:04:30 -0400150 @Override
151 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Joe Onoratoeebd9242009-11-04 13:48:32 -0500152 setMeasuredDimension(mBitmap.getWidth(), mBitmap.getHeight());
Joe Onorato00acb122009-08-04 16:04:30 -0400153 }
154
155 @Override
156 protected void onDraw(Canvas canvas) {
157 if (false) {
158 // for debugging
159 Paint p = new Paint();
160 p.setStyle(Paint.Style.FILL);
161 p.setColor(0xaaffffff);
162 canvas.drawRect(0, 0, getWidth(), getHeight(), p);
163 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800164 if (mPaint == null) {
165 mPaint = new Paint();
166 }
Patrick Dubroya669d792010-11-23 14:40:33 -0800167
Adam Cohenfc53cd22011-07-20 15:45:11 -0700168 mHasDrawn = true;
Adam Cohened66b2b2012-01-23 17:28:51 -0800169 boolean crossFade = mCrossFadeProgress > 0 && mCrossFadeBitmap != null;
170 if (crossFade) {
171 int alpha = crossFade ? (int) (255 * (1 - mCrossFadeProgress)) : 255;
172 mPaint.setAlpha(alpha);
173 }
Joe Onorato00acb122009-08-04 16:04:30 -0400174 canvas.drawBitmap(mBitmap, 0.0f, 0.0f, mPaint);
Adam Cohened66b2b2012-01-23 17:28:51 -0800175 if (crossFade) {
176 mPaint.setAlpha((int) (255 * mCrossFadeProgress));
177 canvas.save();
178 float sX = (mBitmap.getWidth() * 1.0f) / mCrossFadeBitmap.getWidth();
179 float sY = (mBitmap.getHeight() * 1.0f) / mCrossFadeBitmap.getHeight();
180 canvas.scale(sX, sY);
181 canvas.drawBitmap(mCrossFadeBitmap, 0.0f, 0.0f, mPaint);
182 canvas.restore();
183 }
184 }
185
186 public void setCrossFadeBitmap(Bitmap crossFadeBitmap) {
187 mCrossFadeBitmap = crossFadeBitmap;
188 }
189
190 public void crossFade(int duration) {
191 ValueAnimator va = ValueAnimator.ofFloat(0f, 1f);
192 va.setDuration(duration);
193 va.setInterpolator(new DecelerateInterpolator(1.5f));
194 va.addUpdateListener(new AnimatorUpdateListener() {
195 @Override
196 public void onAnimationUpdate(ValueAnimator animation) {
197 mCrossFadeProgress = animation.getAnimatedFraction();
198 }
199 });
200 va.start();
Joe Onorato00acb122009-08-04 16:04:30 -0400201 }
202
Joe Onorato00acb122009-08-04 16:04:30 -0400203 public void setPaint(Paint paint) {
204 mPaint = paint;
205 invalidate();
206 }
207
Adam Cohenfc53cd22011-07-20 15:45:11 -0700208 public boolean hasDrawn() {
209 return mHasDrawn;
210 }
211
Adam Cohen3e8f8112011-07-02 18:03:00 -0700212 @Override
213 public void setAlpha(float alpha) {
214 super.setAlpha(alpha);
215 if (mPaint == null) {
216 mPaint = new Paint();
217 }
218 mPaint.setAlpha((int) (255 * alpha));
219 invalidate();
220 }
221
Joe Onorato00acb122009-08-04 16:04:30 -0400222 /**
223 * Create a window containing this view and show it.
224 *
225 * @param windowToken obtained from v.getWindowToken() from one of your views
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 */
Adam Cohen8dfcba42011-07-07 16:38:18 -0700229 public void show(int touchX, int touchY) {
230 mDragLayer.addView(this);
231 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(0, 0);
232 lp.width = mBitmap.getWidth();
233 lp.height = mBitmap.getHeight();
Adam Cohen8dfcba42011-07-07 16:38:18 -0700234 lp.customPosition = true;
235 setLayoutParams(lp);
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800236 setTranslationX(touchX - mRegistrationX);
237 setTranslationY(touchY - mRegistrationY);
Patrick Dubroya669d792010-11-23 14:40:33 -0800238 mAnim.start();
Joe Onorato00acb122009-08-04 16:04:30 -0400239 }
Adam Cohen716b51e2011-06-30 12:09:54 -0700240
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800241 public void cancelAnimation() {
242 if (mAnim != null && mAnim.isRunning()) {
243 mAnim.cancel();
244 }
245 }
246
247 public void resetLayoutParams() {
248 mOffsetX = mOffsetY = 0;
249 requestLayout();
250 }
251
Joe Onorato00acb122009-08-04 16:04:30 -0400252 /**
253 * Move the window containing this view.
254 *
Adam Cohen8dfcba42011-07-07 16:38:18 -0700255 * @param touchX the x coordinate the user touched in DragLayer coordinates
256 * @param touchY the y coordinate the user touched in DragLayer coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400257 */
258 void move(int touchX, int touchY) {
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800259 setTranslationX(touchX - mRegistrationX + (int) mOffsetX);
260 setTranslationY(touchY - mRegistrationY + (int) mOffsetY);
Joe Onorato00acb122009-08-04 16:04:30 -0400261 }
262
263 void remove() {
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800264 if (getParent() != null) {
265 mDragLayer.removeView(DragView.this);
266 }
Patrick Dubroy6f133422011-02-24 12:16:12 -0800267 }
Joe Onorato00acb122009-08-04 16:04:30 -0400268}
269