blob: b1a6266cce04f12bfa8aab64da9cd0f4b73f5101 [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
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Joe Onorato00acb122009-08-04 16:04:30 -040018
Patrick Dubroya669d792010-11-23 14:40:33 -080019import android.animation.ValueAnimator;
20import android.animation.ValueAnimator.AnimatorUpdateListener;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070021import android.content.res.Resources;
Joe Onorato00acb122009-08-04 16:04:30 -040022import android.graphics.Bitmap;
23import android.graphics.Canvas;
Joe Onorato00acb122009-08-04 16:04:30 -040024import android.graphics.Paint;
Winson Chungb8c69f32011-10-19 21:36:08 -070025import android.graphics.Point;
Winson Chung61967cb2012-02-28 18:11:33 -080026import android.graphics.PorterDuff;
27import android.graphics.PorterDuffColorFilter;
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 Cohen091440a2015-03-18 14:16:05 -070032import com.android.launcher3.util.Thunk;
33
Patrick Dubroya669d792010-11-23 14:40:33 -080034public class DragView extends View {
Adam Cohen091440a2015-03-18 14:16:05 -070035 @Thunk static float sDragAlpha = 1f;
Winson Chung7bd1bbb2012-02-13 18:29:29 -080036
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 Cohen091440a2015-03-18 14:16:05 -070047 @Thunk float mCrossFadeProgress = 0f;
Michael Jurkaa63c4522010-08-19 13:52:27 -070048
Patrick Dubroya669d792010-11-23 14:40:33 -080049 ValueAnimator mAnim;
Adam Cohen091440a2015-03-18 14:16:05 -070050 @Thunk float mOffsetX = 0.0f;
51 @Thunk float mOffsetY = 0.0f;
Winson Chung043f2af2012-03-01 16:09:54 -080052 private float mInitialScale = 1f;
Winson Chungeeb5bbc2013-11-13 15:47:05 -080053 // The intrinsic icon scale factor is the scale factor for a drag icon over the workspace
54 // size. This is ignored for non-icons.
55 private float mIntrinsicIconScale = 1f;
Joe Onorato00acb122009-08-04 16:04:30 -040056
Joe Onorato00acb122009-08-04 16:04:30 -040057 /**
58 * Construct the drag view.
59 * <p>
60 * The registration point is the point inside our view that the touch events should
61 * be centered upon.
62 *
Adam Cohen8dfcba42011-07-07 16:38:18 -070063 * @param launcher The Launcher instance
Joe Onorato00acb122009-08-04 16:04:30 -040064 * @param bitmap The view that we're dragging around. We scale it up when we draw it.
65 * @param registrationX The x coordinate of the registration point.
66 * @param registrationY The y coordinate of the registration point.
67 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070068 public DragView(Launcher launcher, Bitmap bitmap, int registrationX, int registrationY,
Winson Chung72d59842012-02-22 13:51:36 -080069 int left, int top, int width, int height, final float initialScale) {
Adam Cohen8dfcba42011-07-07 16:38:18 -070070 super(launcher);
71 mDragLayer = launcher.getDragLayer();
Winson Chung043f2af2012-03-01 16:09:54 -080072 mInitialScale = initialScale;
Joe Onorato00acb122009-08-04 16:04:30 -040073
Patrick Dubroyde7658b2010-09-27 11:15:43 -070074 final Resources res = getResources();
Winson Chung7bd1bbb2012-02-13 18:29:29 -080075 final float scaleDps = res.getDimensionPixelSize(R.dimen.dragViewScale);
76 final float scale = (width + scaleDps) / width;
Patrick Dubroya669d792010-11-23 14:40:33 -080077
Adam Cohen307fe232012-08-16 17:55:58 -070078 // Set the initial scale to avoid any jumps
79 setScaleX(initialScale);
80 setScaleY(initialScale);
81
Patrick Dubroya669d792010-11-23 14:40:33 -080082 // Animate the view into the correct position
Michael Jurkaf1ad6082013-03-13 12:55:46 +010083 mAnim = LauncherAnimUtils.ofFloat(this, 0f, 1f);
Winson Chung7bd1bbb2012-02-13 18:29:29 -080084 mAnim.setDuration(150);
Patrick Dubroya669d792010-11-23 14:40:33 -080085 mAnim.addUpdateListener(new AnimatorUpdateListener() {
86 @Override
87 public void onAnimationUpdate(ValueAnimator animation) {
88 final float value = (Float) animation.getAnimatedValue();
89
Sunny Goyal560616d2015-02-26 11:26:19 -080090 final int deltaX = (int) (-mOffsetX);
91 final int deltaY = (int) (-mOffsetY);
Patrick Dubroya669d792010-11-23 14:40:33 -080092
93 mOffsetX += deltaX;
94 mOffsetY += deltaY;
Winson Chung72d59842012-02-22 13:51:36 -080095 setScaleX(initialScale + (value * (scale - initialScale)));
96 setScaleY(initialScale + (value * (scale - initialScale)));
Winson Chung867ca622012-02-21 15:48:35 -080097 if (sDragAlpha != 1f) {
98 setAlpha(sDragAlpha * value + (1f - value));
99 }
Patrick Dubroya669d792010-11-23 14:40:33 -0800100
101 if (getParent() == null) {
102 animation.cancel();
103 } else {
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800104 setTranslationX(getTranslationX() + deltaX);
105 setTranslationY(getTranslationY() + deltaY);
Patrick Dubroya669d792010-11-23 14:40:33 -0800106 }
107 }
108 });
Joe Onorato00acb122009-08-04 16:04:30 -0400109
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800110 mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height);
Adam Cohene3e27a82011-04-15 12:07:39 -0700111 setDragRegion(new Rect(0, 0, width, height));
Joe Onorato00acb122009-08-04 16:04:30 -0400112
113 // The point in our scaled bitmap that the touch events are located
Patrick Dubroya669d792010-11-23 14:40:33 -0800114 mRegistrationX = registrationX;
115 mRegistrationY = registrationY;
Patrick Dubroy62bbb3c2011-01-17 15:29:27 -0800116
117 // Force a measure, because Workspace uses getMeasuredHeight() before the layout pass
118 int ms = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
119 measure(ms, ms);
Michael Jurka6cfafb92012-02-23 18:25:43 -0800120 mPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
Patrick Dubroy62bbb3c2011-01-17 15:29:27 -0800121 }
122
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800123 /** Sets the scale of the view over the normal workspace icon size. */
124 public void setIntrinsicIconScaleFactor(float scale) {
125 mIntrinsicIconScale = scale;
126 }
127
128 public float getIntrinsicIconScaleFactor() {
129 return mIntrinsicIconScale;
130 }
131
Patrick Dubroy62bbb3c2011-01-17 15:29:27 -0800132 public float getOffsetY() {
133 return mOffsetY;
Joe Onorato00acb122009-08-04 16:04:30 -0400134 }
135
Michael Jurkaa63c4522010-08-19 13:52:27 -0700136 public int getDragRegionLeft() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700137 return mDragRegion.left;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700138 }
139
140 public int getDragRegionTop() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700141 return mDragRegion.top;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700142 }
143
144 public int getDragRegionWidth() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700145 return mDragRegion.width();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700146 }
147
148 public int getDragRegionHeight() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700149 return mDragRegion.height();
150 }
151
Winson Chungb8c69f32011-10-19 21:36:08 -0700152 public void setDragVisualizeOffset(Point p) {
153 mDragVisualizeOffset = p;
154 }
155
156 public Point getDragVisualizeOffset() {
157 return mDragVisualizeOffset;
158 }
159
Adam Cohene3e27a82011-04-15 12:07:39 -0700160 public void setDragRegion(Rect r) {
161 mDragRegion = r;
162 }
163
164 public Rect getDragRegion() {
165 return mDragRegion;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700166 }
167
Winson Chung043f2af2012-03-01 16:09:54 -0800168 public float getInitialScale() {
169 return mInitialScale;
170 }
171
172 public void updateInitialScaleToCurrentScale() {
173 mInitialScale = getScaleX();
174 }
175
Joe Onorato00acb122009-08-04 16:04:30 -0400176 @Override
177 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Joe Onoratoeebd9242009-11-04 13:48:32 -0500178 setMeasuredDimension(mBitmap.getWidth(), mBitmap.getHeight());
Joe Onorato00acb122009-08-04 16:04:30 -0400179 }
180
181 @Override
182 protected void onDraw(Canvas canvas) {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700183 @SuppressWarnings("all") // suppress dead code warning
184 final boolean debug = false;
185 if (debug) {
Joe Onorato00acb122009-08-04 16:04:30 -0400186 Paint p = new Paint();
187 p.setStyle(Paint.Style.FILL);
Winson Chungeecf02d2012-03-02 17:14:58 -0800188 p.setColor(0x66ffffff);
Joe Onorato00acb122009-08-04 16:04:30 -0400189 canvas.drawRect(0, 0, getWidth(), getHeight(), p);
190 }
Patrick Dubroya669d792010-11-23 14:40:33 -0800191
Adam Cohenfc53cd22011-07-20 15:45:11 -0700192 mHasDrawn = true;
Adam Cohened66b2b2012-01-23 17:28:51 -0800193 boolean crossFade = mCrossFadeProgress > 0 && mCrossFadeBitmap != null;
194 if (crossFade) {
195 int alpha = crossFade ? (int) (255 * (1 - mCrossFadeProgress)) : 255;
196 mPaint.setAlpha(alpha);
197 }
Joe Onorato00acb122009-08-04 16:04:30 -0400198 canvas.drawBitmap(mBitmap, 0.0f, 0.0f, mPaint);
Adam Cohened66b2b2012-01-23 17:28:51 -0800199 if (crossFade) {
200 mPaint.setAlpha((int) (255 * mCrossFadeProgress));
201 canvas.save();
202 float sX = (mBitmap.getWidth() * 1.0f) / mCrossFadeBitmap.getWidth();
203 float sY = (mBitmap.getHeight() * 1.0f) / mCrossFadeBitmap.getHeight();
204 canvas.scale(sX, sY);
205 canvas.drawBitmap(mCrossFadeBitmap, 0.0f, 0.0f, mPaint);
206 canvas.restore();
207 }
208 }
209
210 public void setCrossFadeBitmap(Bitmap crossFadeBitmap) {
211 mCrossFadeBitmap = crossFadeBitmap;
212 }
213
214 public void crossFade(int duration) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100215 ValueAnimator va = LauncherAnimUtils.ofFloat(this, 0f, 1f);
Adam Cohened66b2b2012-01-23 17:28:51 -0800216 va.setDuration(duration);
217 va.setInterpolator(new DecelerateInterpolator(1.5f));
218 va.addUpdateListener(new AnimatorUpdateListener() {
219 @Override
220 public void onAnimationUpdate(ValueAnimator animation) {
221 mCrossFadeProgress = animation.getAnimatedFraction();
222 }
223 });
224 va.start();
Joe Onorato00acb122009-08-04 16:04:30 -0400225 }
226
Winson Chung61967cb2012-02-28 18:11:33 -0800227 public void setColor(int color) {
Michael Jurka6cfafb92012-02-23 18:25:43 -0800228 if (mPaint == null) {
229 mPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
230 }
Winson Chung61967cb2012-02-28 18:11:33 -0800231 if (color != 0) {
232 mPaint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP));
233 } else {
234 mPaint.setColorFilter(null);
235 }
Joe Onorato00acb122009-08-04 16:04:30 -0400236 invalidate();
237 }
238
Adam Cohenfc53cd22011-07-20 15:45:11 -0700239 public boolean hasDrawn() {
240 return mHasDrawn;
241 }
242
Adam Cohen3e8f8112011-07-02 18:03:00 -0700243 @Override
244 public void setAlpha(float alpha) {
245 super.setAlpha(alpha);
Adam Cohen3e8f8112011-07-02 18:03:00 -0700246 mPaint.setAlpha((int) (255 * alpha));
247 invalidate();
248 }
249
Joe Onorato00acb122009-08-04 16:04:30 -0400250 /**
251 * Create a window containing this view and show it.
252 *
253 * @param windowToken obtained from v.getWindowToken() from one of your views
Adam Cohen8dfcba42011-07-07 16:38:18 -0700254 * @param touchX the x coordinate the user touched in DragLayer coordinates
255 * @param touchY the y coordinate the user touched in DragLayer coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400256 */
Adam Cohen8dfcba42011-07-07 16:38:18 -0700257 public void show(int touchX, int touchY) {
258 mDragLayer.addView(this);
Winson Chung043f2af2012-03-01 16:09:54 -0800259
Winson Chung043f2af2012-03-01 16:09:54 -0800260 // Start the pick-up animation
Adam Cohen8dfcba42011-07-07 16:38:18 -0700261 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(0, 0);
262 lp.width = mBitmap.getWidth();
263 lp.height = mBitmap.getHeight();
Adam Cohen8dfcba42011-07-07 16:38:18 -0700264 lp.customPosition = true;
265 setLayoutParams(lp);
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800266 setTranslationX(touchX - mRegistrationX);
267 setTranslationY(touchY - mRegistrationY);
Michael Jurkaca993832012-06-29 15:17:04 -0700268 // Post the animation to skip other expensive work happening on the first frame
269 post(new Runnable() {
270 public void run() {
271 mAnim.start();
272 }
273 });
Joe Onorato00acb122009-08-04 16:04:30 -0400274 }
Adam Cohen716b51e2011-06-30 12:09:54 -0700275
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800276 public void cancelAnimation() {
277 if (mAnim != null && mAnim.isRunning()) {
278 mAnim.cancel();
279 }
280 }
281
282 public void resetLayoutParams() {
283 mOffsetX = mOffsetY = 0;
284 requestLayout();
285 }
286
Joe Onorato00acb122009-08-04 16:04:30 -0400287 /**
288 * Move the window containing this view.
289 *
Adam Cohen8dfcba42011-07-07 16:38:18 -0700290 * @param touchX the x coordinate the user touched in DragLayer coordinates
291 * @param touchY the y coordinate the user touched in DragLayer coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400292 */
293 void move(int touchX, int touchY) {
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800294 setTranslationX(touchX - mRegistrationX + (int) mOffsetX);
295 setTranslationY(touchY - mRegistrationY + (int) mOffsetY);
Joe Onorato00acb122009-08-04 16:04:30 -0400296 }
297
298 void remove() {
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800299 if (getParent() != null) {
300 mDragLayer.removeView(DragView.this);
301 }
Patrick Dubroy6f133422011-02-24 12:16:12 -0800302 }
Joe Onorato00acb122009-08-04 16:04:30 -0400303}
304