blob: 78d72b30fa0499f737db5f6232e5c09d9bc4189c [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
Patrick Dubroya669d792010-11-23 14:40:33 -080032public class DragView extends View {
Winson Chung867ca622012-02-21 15:48:35 -080033 private static float sDragAlpha = 1f;
Winson Chung7bd1bbb2012-02-13 18:29:29 -080034
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;
Winson Chung043f2af2012-03-01 16:09:54 -080050 private float mInitialScale = 1f;
Winson Chungeeb5bbc2013-11-13 15:47:05 -080051 // The intrinsic icon scale factor is the scale factor for a drag icon over the workspace
52 // size. This is ignored for non-icons.
53 private float mIntrinsicIconScale = 1f;
Joe Onorato00acb122009-08-04 16:04:30 -040054
Joe Onorato00acb122009-08-04 16:04:30 -040055 /**
56 * Construct the drag view.
57 * <p>
58 * The registration point is the point inside our view that the touch events should
59 * be centered upon.
60 *
Adam Cohen8dfcba42011-07-07 16:38:18 -070061 * @param launcher The Launcher instance
Joe Onorato00acb122009-08-04 16:04:30 -040062 * @param bitmap The view that we're dragging around. We scale it up when we draw it.
63 * @param registrationX The x coordinate of the registration point.
64 * @param registrationY The y coordinate of the registration point.
65 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070066 public DragView(Launcher launcher, Bitmap bitmap, int registrationX, int registrationY,
Winson Chung72d59842012-02-22 13:51:36 -080067 int left, int top, int width, int height, final float initialScale) {
Adam Cohen8dfcba42011-07-07 16:38:18 -070068 super(launcher);
69 mDragLayer = launcher.getDragLayer();
Winson Chung043f2af2012-03-01 16:09:54 -080070 mInitialScale = initialScale;
Joe Onorato00acb122009-08-04 16:04:30 -040071
Patrick Dubroyde7658b2010-09-27 11:15:43 -070072 final Resources res = getResources();
Winson Chung7bd1bbb2012-02-13 18:29:29 -080073 final float scaleDps = res.getDimensionPixelSize(R.dimen.dragViewScale);
74 final float scale = (width + scaleDps) / width;
Patrick Dubroya669d792010-11-23 14:40:33 -080075
Adam Cohen307fe232012-08-16 17:55:58 -070076 // Set the initial scale to avoid any jumps
77 setScaleX(initialScale);
78 setScaleY(initialScale);
79
Patrick Dubroya669d792010-11-23 14:40:33 -080080 // Animate the view into the correct position
Michael Jurkaf1ad6082013-03-13 12:55:46 +010081 mAnim = LauncherAnimUtils.ofFloat(this, 0f, 1f);
Winson Chung7bd1bbb2012-02-13 18:29:29 -080082 mAnim.setDuration(150);
Patrick Dubroya669d792010-11-23 14:40:33 -080083 mAnim.addUpdateListener(new AnimatorUpdateListener() {
84 @Override
85 public void onAnimationUpdate(ValueAnimator animation) {
86 final float value = (Float) animation.getAnimatedValue();
87
Sunny Goyal560616d2015-02-26 11:26:19 -080088 final int deltaX = (int) (-mOffsetX);
89 final int deltaY = (int) (-mOffsetY);
Patrick Dubroya669d792010-11-23 14:40:33 -080090
91 mOffsetX += deltaX;
92 mOffsetY += deltaY;
Winson Chung72d59842012-02-22 13:51:36 -080093 setScaleX(initialScale + (value * (scale - initialScale)));
94 setScaleY(initialScale + (value * (scale - initialScale)));
Winson Chung867ca622012-02-21 15:48:35 -080095 if (sDragAlpha != 1f) {
96 setAlpha(sDragAlpha * value + (1f - value));
97 }
Patrick Dubroya669d792010-11-23 14:40:33 -080098
99 if (getParent() == null) {
100 animation.cancel();
101 } else {
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800102 setTranslationX(getTranslationX() + deltaX);
103 setTranslationY(getTranslationY() + deltaY);
Patrick Dubroya669d792010-11-23 14:40:33 -0800104 }
105 }
106 });
Joe Onorato00acb122009-08-04 16:04:30 -0400107
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800108 mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height);
Adam Cohene3e27a82011-04-15 12:07:39 -0700109 setDragRegion(new Rect(0, 0, width, height));
Joe Onorato00acb122009-08-04 16:04:30 -0400110
111 // The point in our scaled bitmap that the touch events are located
Patrick Dubroya669d792010-11-23 14:40:33 -0800112 mRegistrationX = registrationX;
113 mRegistrationY = registrationY;
Patrick Dubroy62bbb3c2011-01-17 15:29:27 -0800114
115 // Force a measure, because Workspace uses getMeasuredHeight() before the layout pass
116 int ms = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
117 measure(ms, ms);
Michael Jurka6cfafb92012-02-23 18:25:43 -0800118 mPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
Patrick Dubroy62bbb3c2011-01-17 15:29:27 -0800119 }
120
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800121 /** Sets the scale of the view over the normal workspace icon size. */
122 public void setIntrinsicIconScaleFactor(float scale) {
123 mIntrinsicIconScale = scale;
124 }
125
126 public float getIntrinsicIconScaleFactor() {
127 return mIntrinsicIconScale;
128 }
129
Patrick Dubroy62bbb3c2011-01-17 15:29:27 -0800130 public float getOffsetY() {
131 return mOffsetY;
Joe Onorato00acb122009-08-04 16:04:30 -0400132 }
133
Michael Jurkaa63c4522010-08-19 13:52:27 -0700134 public int getDragRegionLeft() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700135 return mDragRegion.left;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700136 }
137
138 public int getDragRegionTop() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700139 return mDragRegion.top;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700140 }
141
142 public int getDragRegionWidth() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700143 return mDragRegion.width();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700144 }
145
146 public int getDragRegionHeight() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700147 return mDragRegion.height();
148 }
149
Winson Chungb8c69f32011-10-19 21:36:08 -0700150 public void setDragVisualizeOffset(Point p) {
151 mDragVisualizeOffset = p;
152 }
153
154 public Point getDragVisualizeOffset() {
155 return mDragVisualizeOffset;
156 }
157
Adam Cohene3e27a82011-04-15 12:07:39 -0700158 public void setDragRegion(Rect r) {
159 mDragRegion = r;
160 }
161
162 public Rect getDragRegion() {
163 return mDragRegion;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700164 }
165
Winson Chung043f2af2012-03-01 16:09:54 -0800166 public float getInitialScale() {
167 return mInitialScale;
168 }
169
170 public void updateInitialScaleToCurrentScale() {
171 mInitialScale = getScaleX();
172 }
173
Joe Onorato00acb122009-08-04 16:04:30 -0400174 @Override
175 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Joe Onoratoeebd9242009-11-04 13:48:32 -0500176 setMeasuredDimension(mBitmap.getWidth(), mBitmap.getHeight());
Joe Onorato00acb122009-08-04 16:04:30 -0400177 }
178
179 @Override
180 protected void onDraw(Canvas canvas) {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700181 @SuppressWarnings("all") // suppress dead code warning
182 final boolean debug = false;
183 if (debug) {
Joe Onorato00acb122009-08-04 16:04:30 -0400184 Paint p = new Paint();
185 p.setStyle(Paint.Style.FILL);
Winson Chungeecf02d2012-03-02 17:14:58 -0800186 p.setColor(0x66ffffff);
Joe Onorato00acb122009-08-04 16:04:30 -0400187 canvas.drawRect(0, 0, getWidth(), getHeight(), p);
188 }
Patrick Dubroya669d792010-11-23 14:40:33 -0800189
Adam Cohenfc53cd22011-07-20 15:45:11 -0700190 mHasDrawn = true;
Adam Cohened66b2b2012-01-23 17:28:51 -0800191 boolean crossFade = mCrossFadeProgress > 0 && mCrossFadeBitmap != null;
192 if (crossFade) {
193 int alpha = crossFade ? (int) (255 * (1 - mCrossFadeProgress)) : 255;
194 mPaint.setAlpha(alpha);
195 }
Joe Onorato00acb122009-08-04 16:04:30 -0400196 canvas.drawBitmap(mBitmap, 0.0f, 0.0f, mPaint);
Adam Cohened66b2b2012-01-23 17:28:51 -0800197 if (crossFade) {
198 mPaint.setAlpha((int) (255 * mCrossFadeProgress));
199 canvas.save();
200 float sX = (mBitmap.getWidth() * 1.0f) / mCrossFadeBitmap.getWidth();
201 float sY = (mBitmap.getHeight() * 1.0f) / mCrossFadeBitmap.getHeight();
202 canvas.scale(sX, sY);
203 canvas.drawBitmap(mCrossFadeBitmap, 0.0f, 0.0f, mPaint);
204 canvas.restore();
205 }
206 }
207
208 public void setCrossFadeBitmap(Bitmap crossFadeBitmap) {
209 mCrossFadeBitmap = crossFadeBitmap;
210 }
211
212 public void crossFade(int duration) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100213 ValueAnimator va = LauncherAnimUtils.ofFloat(this, 0f, 1f);
Adam Cohened66b2b2012-01-23 17:28:51 -0800214 va.setDuration(duration);
215 va.setInterpolator(new DecelerateInterpolator(1.5f));
216 va.addUpdateListener(new AnimatorUpdateListener() {
217 @Override
218 public void onAnimationUpdate(ValueAnimator animation) {
219 mCrossFadeProgress = animation.getAnimatedFraction();
220 }
221 });
222 va.start();
Joe Onorato00acb122009-08-04 16:04:30 -0400223 }
224
Winson Chung61967cb2012-02-28 18:11:33 -0800225 public void setColor(int color) {
Michael Jurka6cfafb92012-02-23 18:25:43 -0800226 if (mPaint == null) {
227 mPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
228 }
Winson Chung61967cb2012-02-28 18:11:33 -0800229 if (color != 0) {
230 mPaint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP));
231 } else {
232 mPaint.setColorFilter(null);
233 }
Joe Onorato00acb122009-08-04 16:04:30 -0400234 invalidate();
235 }
236
Adam Cohenfc53cd22011-07-20 15:45:11 -0700237 public boolean hasDrawn() {
238 return mHasDrawn;
239 }
240
Adam Cohen3e8f8112011-07-02 18:03:00 -0700241 @Override
242 public void setAlpha(float alpha) {
243 super.setAlpha(alpha);
Adam Cohen3e8f8112011-07-02 18:03:00 -0700244 mPaint.setAlpha((int) (255 * alpha));
245 invalidate();
246 }
247
Joe Onorato00acb122009-08-04 16:04:30 -0400248 /**
249 * Create a window containing this view and show it.
250 *
251 * @param windowToken obtained from v.getWindowToken() from one of your views
Adam Cohen8dfcba42011-07-07 16:38:18 -0700252 * @param touchX the x coordinate the user touched in DragLayer coordinates
253 * @param touchY the y coordinate the user touched in DragLayer coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400254 */
Adam Cohen8dfcba42011-07-07 16:38:18 -0700255 public void show(int touchX, int touchY) {
256 mDragLayer.addView(this);
Winson Chung043f2af2012-03-01 16:09:54 -0800257
Winson Chung043f2af2012-03-01 16:09:54 -0800258 // Start the pick-up animation
Adam Cohen8dfcba42011-07-07 16:38:18 -0700259 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(0, 0);
260 lp.width = mBitmap.getWidth();
261 lp.height = mBitmap.getHeight();
Adam Cohen8dfcba42011-07-07 16:38:18 -0700262 lp.customPosition = true;
263 setLayoutParams(lp);
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800264 setTranslationX(touchX - mRegistrationX);
265 setTranslationY(touchY - mRegistrationY);
Michael Jurkaca993832012-06-29 15:17:04 -0700266 // Post the animation to skip other expensive work happening on the first frame
267 post(new Runnable() {
268 public void run() {
269 mAnim.start();
270 }
271 });
Joe Onorato00acb122009-08-04 16:04:30 -0400272 }
Adam Cohen716b51e2011-06-30 12:09:54 -0700273
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800274 public void cancelAnimation() {
275 if (mAnim != null && mAnim.isRunning()) {
276 mAnim.cancel();
277 }
278 }
279
280 public void resetLayoutParams() {
281 mOffsetX = mOffsetY = 0;
282 requestLayout();
283 }
284
Joe Onorato00acb122009-08-04 16:04:30 -0400285 /**
286 * Move the window containing this view.
287 *
Adam Cohen8dfcba42011-07-07 16:38:18 -0700288 * @param touchX the x coordinate the user touched in DragLayer coordinates
289 * @param touchY the y coordinate the user touched in DragLayer coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400290 */
291 void move(int touchX, int touchY) {
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800292 setTranslationX(touchX - mRegistrationX + (int) mOffsetX);
293 setTranslationY(touchY - mRegistrationY + (int) mOffsetY);
Joe Onorato00acb122009-08-04 16:04:30 -0400294 }
295
296 void remove() {
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800297 if (getParent() != null) {
298 mDragLayer.removeView(DragView.this);
299 }
Patrick Dubroy6f133422011-02-24 12:16:12 -0800300 }
Joe Onorato00acb122009-08-04 16:04:30 -0400301}
302