blob: b66b55c9dcc0bb08a321bde45f971389eb7d8929 [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
Daniel Sandler325dc232013-06-05 22:57:57 -040018package com.android.launcher3;
Joe Onorato00acb122009-08-04 16:04:30 -040019
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;
Joe Onorato00acb122009-08-04 16:04:30 -040025import android.graphics.Paint;
Winson Chungb8c69f32011-10-19 21:36:08 -070026import android.graphics.Point;
Winson Chung61967cb2012-02-28 18:11:33 -080027import android.graphics.PorterDuff;
28import android.graphics.PorterDuffColorFilter;
Adam Cohene3e27a82011-04-15 12:07:39 -070029import android.graphics.Rect;
Joe Onorato00acb122009-08-04 16:04:30 -040030import android.view.View;
Patrick Dubroya669d792010-11-23 14:40:33 -080031import android.view.animation.DecelerateInterpolator;
Joe Onorato00acb122009-08-04 16:04:30 -040032
Daniel Sandler325dc232013-06-05 22:57:57 -040033import com.android.launcher3.R;
Adam Cohen120980b2010-12-08 11:05:37 -080034
Patrick Dubroya669d792010-11-23 14:40:33 -080035public class DragView extends View {
Winson Chung867ca622012-02-21 15:48:35 -080036 private static float sDragAlpha = 1f;
Winson Chung7bd1bbb2012-02-13 18:29:29 -080037
Joe Onorato00acb122009-08-04 16:04:30 -040038 private Bitmap mBitmap;
Adam Cohened66b2b2012-01-23 17:28:51 -080039 private Bitmap mCrossFadeBitmap;
Joe Onorato00acb122009-08-04 16:04:30 -040040 private Paint mPaint;
41 private int mRegistrationX;
42 private int mRegistrationY;
43
Winson Chungb8c69f32011-10-19 21:36:08 -070044 private Point mDragVisualizeOffset = null;
Adam Cohene3e27a82011-04-15 12:07:39 -070045 private Rect mDragRegion = null;
Adam Cohen8dfcba42011-07-07 16:38:18 -070046 private DragLayer mDragLayer = null;
Adam Cohenfc53cd22011-07-20 15:45:11 -070047 private boolean mHasDrawn = false;
Adam Cohened66b2b2012-01-23 17:28:51 -080048 private float mCrossFadeProgress = 0f;
Michael Jurkaa63c4522010-08-19 13:52:27 -070049
Patrick Dubroya669d792010-11-23 14:40:33 -080050 ValueAnimator mAnim;
Patrick Dubroya669d792010-11-23 14:40:33 -080051 private float mOffsetX = 0.0f;
52 private float mOffsetY = 0.0f;
Winson Chung043f2af2012-03-01 16:09:54 -080053 private float mInitialScale = 1f;
Winson Chungeeb5bbc2013-11-13 15:47:05 -080054 // The intrinsic icon scale factor is the scale factor for a drag icon over the workspace
55 // size. This is ignored for non-icons.
56 private float mIntrinsicIconScale = 1f;
Joe Onorato00acb122009-08-04 16:04:30 -040057
Joe Onorato00acb122009-08-04 16:04:30 -040058 /**
59 * Construct the drag view.
60 * <p>
61 * The registration point is the point inside our view that the touch events should
62 * be centered upon.
63 *
Adam Cohen8dfcba42011-07-07 16:38:18 -070064 * @param launcher The Launcher instance
Joe Onorato00acb122009-08-04 16:04:30 -040065 * @param bitmap The view that we're dragging around. We scale it up when we draw it.
66 * @param registrationX The x coordinate of the registration point.
67 * @param registrationY The y coordinate of the registration point.
68 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070069 public DragView(Launcher launcher, Bitmap bitmap, int registrationX, int registrationY,
Winson Chung72d59842012-02-22 13:51:36 -080070 int left, int top, int width, int height, final float initialScale) {
Adam Cohen8dfcba42011-07-07 16:38:18 -070071 super(launcher);
72 mDragLayer = launcher.getDragLayer();
Winson Chung043f2af2012-03-01 16:09:54 -080073 mInitialScale = initialScale;
Joe Onorato00acb122009-08-04 16:04:30 -040074
Patrick Dubroyde7658b2010-09-27 11:15:43 -070075 final Resources res = getResources();
Winson Chung7bd1bbb2012-02-13 18:29:29 -080076 final float offsetX = res.getDimensionPixelSize(R.dimen.dragViewOffsetX);
77 final float offsetY = res.getDimensionPixelSize(R.dimen.dragViewOffsetY);
78 final float scaleDps = res.getDimensionPixelSize(R.dimen.dragViewScale);
79 final float scale = (width + scaleDps) / width;
Patrick Dubroya669d792010-11-23 14:40:33 -080080
Adam Cohen307fe232012-08-16 17:55:58 -070081 // Set the initial scale to avoid any jumps
82 setScaleX(initialScale);
83 setScaleY(initialScale);
84
Patrick Dubroya669d792010-11-23 14:40:33 -080085 // Animate the view into the correct position
Michael Jurkaf1ad6082013-03-13 12:55:46 +010086 mAnim = LauncherAnimUtils.ofFloat(this, 0f, 1f);
Winson Chung7bd1bbb2012-02-13 18:29:29 -080087 mAnim.setDuration(150);
Patrick Dubroya669d792010-11-23 14:40:33 -080088 mAnim.addUpdateListener(new AnimatorUpdateListener() {
89 @Override
90 public void onAnimationUpdate(ValueAnimator animation) {
91 final float value = (Float) animation.getAnimatedValue();
92
93 final int deltaX = (int) ((value * offsetX) - mOffsetX);
94 final int deltaY = (int) ((value * offsetY) - mOffsetY);
95
96 mOffsetX += deltaX;
97 mOffsetY += deltaY;
Winson Chung72d59842012-02-22 13:51:36 -080098 setScaleX(initialScale + (value * (scale - initialScale)));
99 setScaleY(initialScale + (value * (scale - initialScale)));
Winson Chung867ca622012-02-21 15:48:35 -0800100 if (sDragAlpha != 1f) {
101 setAlpha(sDragAlpha * value + (1f - value));
102 }
Patrick Dubroya669d792010-11-23 14:40:33 -0800103
104 if (getParent() == null) {
105 animation.cancel();
106 } else {
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800107 setTranslationX(getTranslationX() + deltaX);
108 setTranslationY(getTranslationY() + deltaY);
Patrick Dubroya669d792010-11-23 14:40:33 -0800109 }
110 }
111 });
Joe Onorato00acb122009-08-04 16:04:30 -0400112
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800113 mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height);
Adam Cohene3e27a82011-04-15 12:07:39 -0700114 setDragRegion(new Rect(0, 0, width, height));
Joe Onorato00acb122009-08-04 16:04:30 -0400115
116 // The point in our scaled bitmap that the touch events are located
Patrick Dubroya669d792010-11-23 14:40:33 -0800117 mRegistrationX = registrationX;
118 mRegistrationY = registrationY;
Patrick Dubroy62bbb3c2011-01-17 15:29:27 -0800119
120 // Force a measure, because Workspace uses getMeasuredHeight() before the layout pass
121 int ms = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
122 measure(ms, ms);
Michael Jurka6cfafb92012-02-23 18:25:43 -0800123 mPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
Patrick Dubroy62bbb3c2011-01-17 15:29:27 -0800124 }
125
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800126 /** Sets the scale of the view over the normal workspace icon size. */
127 public void setIntrinsicIconScaleFactor(float scale) {
128 mIntrinsicIconScale = scale;
129 }
130
131 public float getIntrinsicIconScaleFactor() {
132 return mIntrinsicIconScale;
133 }
134
Patrick Dubroy62bbb3c2011-01-17 15:29:27 -0800135 public float getOffsetY() {
136 return mOffsetY;
Joe Onorato00acb122009-08-04 16:04:30 -0400137 }
138
Michael Jurkaa63c4522010-08-19 13:52:27 -0700139 public int getDragRegionLeft() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700140 return mDragRegion.left;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700141 }
142
143 public int getDragRegionTop() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700144 return mDragRegion.top;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700145 }
146
147 public int getDragRegionWidth() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700148 return mDragRegion.width();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700149 }
150
151 public int getDragRegionHeight() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700152 return mDragRegion.height();
153 }
154
Winson Chungb8c69f32011-10-19 21:36:08 -0700155 public void setDragVisualizeOffset(Point p) {
156 mDragVisualizeOffset = p;
157 }
158
159 public Point getDragVisualizeOffset() {
160 return mDragVisualizeOffset;
161 }
162
Adam Cohene3e27a82011-04-15 12:07:39 -0700163 public void setDragRegion(Rect r) {
164 mDragRegion = r;
165 }
166
167 public Rect getDragRegion() {
168 return mDragRegion;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700169 }
170
Winson Chung043f2af2012-03-01 16:09:54 -0800171 public float getInitialScale() {
172 return mInitialScale;
173 }
174
175 public void updateInitialScaleToCurrentScale() {
176 mInitialScale = getScaleX();
177 }
178
Joe Onorato00acb122009-08-04 16:04:30 -0400179 @Override
180 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Joe Onoratoeebd9242009-11-04 13:48:32 -0500181 setMeasuredDimension(mBitmap.getWidth(), mBitmap.getHeight());
Joe Onorato00acb122009-08-04 16:04:30 -0400182 }
183
184 @Override
185 protected void onDraw(Canvas canvas) {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700186 @SuppressWarnings("all") // suppress dead code warning
187 final boolean debug = false;
188 if (debug) {
Joe Onorato00acb122009-08-04 16:04:30 -0400189 Paint p = new Paint();
190 p.setStyle(Paint.Style.FILL);
Winson Chungeecf02d2012-03-02 17:14:58 -0800191 p.setColor(0x66ffffff);
Joe Onorato00acb122009-08-04 16:04:30 -0400192 canvas.drawRect(0, 0, getWidth(), getHeight(), p);
193 }
Patrick Dubroya669d792010-11-23 14:40:33 -0800194
Adam Cohenfc53cd22011-07-20 15:45:11 -0700195 mHasDrawn = true;
Adam Cohened66b2b2012-01-23 17:28:51 -0800196 boolean crossFade = mCrossFadeProgress > 0 && mCrossFadeBitmap != null;
197 if (crossFade) {
198 int alpha = crossFade ? (int) (255 * (1 - mCrossFadeProgress)) : 255;
199 mPaint.setAlpha(alpha);
200 }
Joe Onorato00acb122009-08-04 16:04:30 -0400201 canvas.drawBitmap(mBitmap, 0.0f, 0.0f, mPaint);
Adam Cohened66b2b2012-01-23 17:28:51 -0800202 if (crossFade) {
203 mPaint.setAlpha((int) (255 * mCrossFadeProgress));
204 canvas.save();
205 float sX = (mBitmap.getWidth() * 1.0f) / mCrossFadeBitmap.getWidth();
206 float sY = (mBitmap.getHeight() * 1.0f) / mCrossFadeBitmap.getHeight();
207 canvas.scale(sX, sY);
208 canvas.drawBitmap(mCrossFadeBitmap, 0.0f, 0.0f, mPaint);
209 canvas.restore();
210 }
211 }
212
213 public void setCrossFadeBitmap(Bitmap crossFadeBitmap) {
214 mCrossFadeBitmap = crossFadeBitmap;
215 }
216
217 public void crossFade(int duration) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100218 ValueAnimator va = LauncherAnimUtils.ofFloat(this, 0f, 1f);
Adam Cohened66b2b2012-01-23 17:28:51 -0800219 va.setDuration(duration);
220 va.setInterpolator(new DecelerateInterpolator(1.5f));
221 va.addUpdateListener(new AnimatorUpdateListener() {
222 @Override
223 public void onAnimationUpdate(ValueAnimator animation) {
224 mCrossFadeProgress = animation.getAnimatedFraction();
225 }
226 });
227 va.start();
Joe Onorato00acb122009-08-04 16:04:30 -0400228 }
229
Winson Chung61967cb2012-02-28 18:11:33 -0800230 public void setColor(int color) {
Michael Jurka6cfafb92012-02-23 18:25:43 -0800231 if (mPaint == null) {
232 mPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
233 }
Winson Chung61967cb2012-02-28 18:11:33 -0800234 if (color != 0) {
235 mPaint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP));
236 } else {
237 mPaint.setColorFilter(null);
238 }
Joe Onorato00acb122009-08-04 16:04:30 -0400239 invalidate();
240 }
241
Adam Cohenfc53cd22011-07-20 15:45:11 -0700242 public boolean hasDrawn() {
243 return mHasDrawn;
244 }
245
Adam Cohen3e8f8112011-07-02 18:03:00 -0700246 @Override
247 public void setAlpha(float alpha) {
248 super.setAlpha(alpha);
Adam Cohen3e8f8112011-07-02 18:03:00 -0700249 mPaint.setAlpha((int) (255 * alpha));
250 invalidate();
251 }
252
Joe Onorato00acb122009-08-04 16:04:30 -0400253 /**
254 * Create a window containing this view and show it.
255 *
256 * @param windowToken obtained from v.getWindowToken() from one of your views
Adam Cohen8dfcba42011-07-07 16:38:18 -0700257 * @param touchX the x coordinate the user touched in DragLayer coordinates
258 * @param touchY the y coordinate the user touched in DragLayer coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400259 */
Adam Cohen8dfcba42011-07-07 16:38:18 -0700260 public void show(int touchX, int touchY) {
261 mDragLayer.addView(this);
Winson Chung043f2af2012-03-01 16:09:54 -0800262
Winson Chung043f2af2012-03-01 16:09:54 -0800263 // Start the pick-up animation
Adam Cohen8dfcba42011-07-07 16:38:18 -0700264 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(0, 0);
265 lp.width = mBitmap.getWidth();
266 lp.height = mBitmap.getHeight();
Adam Cohen8dfcba42011-07-07 16:38:18 -0700267 lp.customPosition = true;
268 setLayoutParams(lp);
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800269 setTranslationX(touchX - mRegistrationX);
270 setTranslationY(touchY - mRegistrationY);
Michael Jurkaca993832012-06-29 15:17:04 -0700271 // Post the animation to skip other expensive work happening on the first frame
272 post(new Runnable() {
273 public void run() {
274 mAnim.start();
275 }
276 });
Joe Onorato00acb122009-08-04 16:04:30 -0400277 }
Adam Cohen716b51e2011-06-30 12:09:54 -0700278
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800279 public void cancelAnimation() {
280 if (mAnim != null && mAnim.isRunning()) {
281 mAnim.cancel();
282 }
283 }
284
285 public void resetLayoutParams() {
286 mOffsetX = mOffsetY = 0;
287 requestLayout();
288 }
289
Joe Onorato00acb122009-08-04 16:04:30 -0400290 /**
291 * Move the window containing this view.
292 *
Adam Cohen8dfcba42011-07-07 16:38:18 -0700293 * @param touchX the x coordinate the user touched in DragLayer coordinates
294 * @param touchY the y coordinate the user touched in DragLayer coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400295 */
296 void move(int touchX, int touchY) {
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800297 setTranslationX(touchX - mRegistrationX + (int) mOffsetX);
298 setTranslationY(touchY - mRegistrationY + (int) mOffsetY);
Joe Onorato00acb122009-08-04 16:04:30 -0400299 }
300
301 void remove() {
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800302 if (getParent() != null) {
303 mDragLayer.removeView(DragView.this);
304 }
Patrick Dubroy6f133422011-02-24 12:16:12 -0800305 }
Joe Onorato00acb122009-08-04 16:04:30 -0400306}
307