blob: a4b6704ac69b40df0be0427846e9be7241f9c067 [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
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070019import android.animation.FloatArrayEvaluator;
Patrick Dubroya669d792010-11-23 14:40:33 -080020import android.animation.ValueAnimator;
21import android.animation.ValueAnimator.AnimatorUpdateListener;
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070022import android.annotation.TargetApi;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070023import android.content.res.Resources;
Joe Onorato00acb122009-08-04 16:04:30 -040024import android.graphics.Bitmap;
25import android.graphics.Canvas;
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070026import android.graphics.Color;
27import android.graphics.ColorMatrix;
28import android.graphics.ColorMatrixColorFilter;
Joe Onorato00acb122009-08-04 16:04:30 -040029import android.graphics.Paint;
Winson Chungb8c69f32011-10-19 21:36:08 -070030import android.graphics.Point;
Adam Cohene3e27a82011-04-15 12:07:39 -070031import android.graphics.Rect;
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070032import android.os.Build;
Joe Onorato00acb122009-08-04 16:04:30 -040033import android.view.View;
Patrick Dubroya669d792010-11-23 14:40:33 -080034import android.view.animation.DecelerateInterpolator;
Joe Onorato00acb122009-08-04 16:04:30 -040035
Adam Cohen091440a2015-03-18 14:16:05 -070036import com.android.launcher3.util.Thunk;
37
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070038import java.util.Arrays;
39
Patrick Dubroya669d792010-11-23 14:40:33 -080040public class DragView extends View {
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070041 public static int COLOR_CHANGE_DURATION = 200;
42
Adam Cohen091440a2015-03-18 14:16:05 -070043 @Thunk static float sDragAlpha = 1f;
Winson Chung7bd1bbb2012-02-13 18:29:29 -080044
Joe Onorato00acb122009-08-04 16:04:30 -040045 private Bitmap mBitmap;
Adam Cohened66b2b2012-01-23 17:28:51 -080046 private Bitmap mCrossFadeBitmap;
Joe Onorato00acb122009-08-04 16:04:30 -040047 private Paint mPaint;
48 private int mRegistrationX;
49 private int mRegistrationY;
50
Winson Chungb8c69f32011-10-19 21:36:08 -070051 private Point mDragVisualizeOffset = null;
Adam Cohene3e27a82011-04-15 12:07:39 -070052 private Rect mDragRegion = null;
Adam Cohen8dfcba42011-07-07 16:38:18 -070053 private DragLayer mDragLayer = null;
Adam Cohenfc53cd22011-07-20 15:45:11 -070054 private boolean mHasDrawn = false;
Adam Cohen091440a2015-03-18 14:16:05 -070055 @Thunk float mCrossFadeProgress = 0f;
Michael Jurkaa63c4522010-08-19 13:52:27 -070056
Patrick Dubroya669d792010-11-23 14:40:33 -080057 ValueAnimator mAnim;
Adam Cohen091440a2015-03-18 14:16:05 -070058 @Thunk float mOffsetX = 0.0f;
59 @Thunk float mOffsetY = 0.0f;
Winson Chung043f2af2012-03-01 16:09:54 -080060 private float mInitialScale = 1f;
Winson Chungeeb5bbc2013-11-13 15:47:05 -080061 // The intrinsic icon scale factor is the scale factor for a drag icon over the workspace
62 // size. This is ignored for non-icons.
63 private float mIntrinsicIconScale = 1f;
Joe Onorato00acb122009-08-04 16:04:30 -040064
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070065 private float[] mCurrentFilter;
66 private ValueAnimator mFilterAnimator;
67
Joe Onorato00acb122009-08-04 16:04:30 -040068 /**
69 * Construct the drag view.
70 * <p>
71 * The registration point is the point inside our view that the touch events should
72 * be centered upon.
73 *
Adam Cohen8dfcba42011-07-07 16:38:18 -070074 * @param launcher The Launcher instance
Joe Onorato00acb122009-08-04 16:04:30 -040075 * @param bitmap The view that we're dragging around. We scale it up when we draw it.
76 * @param registrationX The x coordinate of the registration point.
77 * @param registrationY The y coordinate of the registration point.
78 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070079 public DragView(Launcher launcher, Bitmap bitmap, int registrationX, int registrationY,
Winson Chung72d59842012-02-22 13:51:36 -080080 int left, int top, int width, int height, final float initialScale) {
Adam Cohen8dfcba42011-07-07 16:38:18 -070081 super(launcher);
82 mDragLayer = launcher.getDragLayer();
Winson Chung043f2af2012-03-01 16:09:54 -080083 mInitialScale = initialScale;
Joe Onorato00acb122009-08-04 16:04:30 -040084
Patrick Dubroyde7658b2010-09-27 11:15:43 -070085 final Resources res = getResources();
Winson Chung7bd1bbb2012-02-13 18:29:29 -080086 final float scaleDps = res.getDimensionPixelSize(R.dimen.dragViewScale);
87 final float scale = (width + scaleDps) / width;
Patrick Dubroya669d792010-11-23 14:40:33 -080088
Adam Cohen307fe232012-08-16 17:55:58 -070089 // Set the initial scale to avoid any jumps
90 setScaleX(initialScale);
91 setScaleY(initialScale);
92
Patrick Dubroya669d792010-11-23 14:40:33 -080093 // Animate the view into the correct position
Michael Jurkaf1ad6082013-03-13 12:55:46 +010094 mAnim = LauncherAnimUtils.ofFloat(this, 0f, 1f);
Winson Chung7bd1bbb2012-02-13 18:29:29 -080095 mAnim.setDuration(150);
Patrick Dubroya669d792010-11-23 14:40:33 -080096 mAnim.addUpdateListener(new AnimatorUpdateListener() {
97 @Override
98 public void onAnimationUpdate(ValueAnimator animation) {
99 final float value = (Float) animation.getAnimatedValue();
100
Sunny Goyal560616d2015-02-26 11:26:19 -0800101 final int deltaX = (int) (-mOffsetX);
102 final int deltaY = (int) (-mOffsetY);
Patrick Dubroya669d792010-11-23 14:40:33 -0800103
104 mOffsetX += deltaX;
105 mOffsetY += deltaY;
Winson Chung72d59842012-02-22 13:51:36 -0800106 setScaleX(initialScale + (value * (scale - initialScale)));
107 setScaleY(initialScale + (value * (scale - initialScale)));
Winson Chung867ca622012-02-21 15:48:35 -0800108 if (sDragAlpha != 1f) {
109 setAlpha(sDragAlpha * value + (1f - value));
110 }
Patrick Dubroya669d792010-11-23 14:40:33 -0800111
112 if (getParent() == null) {
113 animation.cancel();
114 } else {
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800115 setTranslationX(getTranslationX() + deltaX);
116 setTranslationY(getTranslationY() + deltaY);
Patrick Dubroya669d792010-11-23 14:40:33 -0800117 }
118 }
119 });
Joe Onorato00acb122009-08-04 16:04:30 -0400120
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800121 mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height);
Adam Cohene3e27a82011-04-15 12:07:39 -0700122 setDragRegion(new Rect(0, 0, width, height));
Joe Onorato00acb122009-08-04 16:04:30 -0400123
124 // The point in our scaled bitmap that the touch events are located
Patrick Dubroya669d792010-11-23 14:40:33 -0800125 mRegistrationX = registrationX;
126 mRegistrationY = registrationY;
Patrick Dubroy62bbb3c2011-01-17 15:29:27 -0800127
128 // Force a measure, because Workspace uses getMeasuredHeight() before the layout pass
129 int ms = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
130 measure(ms, ms);
Michael Jurka6cfafb92012-02-23 18:25:43 -0800131 mPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
Patrick Dubroy62bbb3c2011-01-17 15:29:27 -0800132 }
133
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800134 /** Sets the scale of the view over the normal workspace icon size. */
135 public void setIntrinsicIconScaleFactor(float scale) {
136 mIntrinsicIconScale = scale;
137 }
138
139 public float getIntrinsicIconScaleFactor() {
140 return mIntrinsicIconScale;
141 }
142
Patrick Dubroy62bbb3c2011-01-17 15:29:27 -0800143 public float getOffsetY() {
144 return mOffsetY;
Joe Onorato00acb122009-08-04 16:04:30 -0400145 }
146
Michael Jurkaa63c4522010-08-19 13:52:27 -0700147 public int getDragRegionLeft() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700148 return mDragRegion.left;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700149 }
150
151 public int getDragRegionTop() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700152 return mDragRegion.top;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700153 }
154
155 public int getDragRegionWidth() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700156 return mDragRegion.width();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700157 }
158
159 public int getDragRegionHeight() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700160 return mDragRegion.height();
161 }
162
Winson Chungb8c69f32011-10-19 21:36:08 -0700163 public void setDragVisualizeOffset(Point p) {
164 mDragVisualizeOffset = p;
165 }
166
167 public Point getDragVisualizeOffset() {
168 return mDragVisualizeOffset;
169 }
170
Adam Cohene3e27a82011-04-15 12:07:39 -0700171 public void setDragRegion(Rect r) {
172 mDragRegion = r;
173 }
174
175 public Rect getDragRegion() {
176 return mDragRegion;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700177 }
178
Winson Chung043f2af2012-03-01 16:09:54 -0800179 public float getInitialScale() {
180 return mInitialScale;
181 }
182
183 public void updateInitialScaleToCurrentScale() {
184 mInitialScale = getScaleX();
185 }
186
Joe Onorato00acb122009-08-04 16:04:30 -0400187 @Override
188 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Joe Onoratoeebd9242009-11-04 13:48:32 -0500189 setMeasuredDimension(mBitmap.getWidth(), mBitmap.getHeight());
Joe Onorato00acb122009-08-04 16:04:30 -0400190 }
191
192 @Override
193 protected void onDraw(Canvas canvas) {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700194 @SuppressWarnings("all") // suppress dead code warning
195 final boolean debug = false;
196 if (debug) {
Joe Onorato00acb122009-08-04 16:04:30 -0400197 Paint p = new Paint();
198 p.setStyle(Paint.Style.FILL);
Winson Chungeecf02d2012-03-02 17:14:58 -0800199 p.setColor(0x66ffffff);
Joe Onorato00acb122009-08-04 16:04:30 -0400200 canvas.drawRect(0, 0, getWidth(), getHeight(), p);
201 }
Patrick Dubroya669d792010-11-23 14:40:33 -0800202
Adam Cohenfc53cd22011-07-20 15:45:11 -0700203 mHasDrawn = true;
Adam Cohened66b2b2012-01-23 17:28:51 -0800204 boolean crossFade = mCrossFadeProgress > 0 && mCrossFadeBitmap != null;
205 if (crossFade) {
206 int alpha = crossFade ? (int) (255 * (1 - mCrossFadeProgress)) : 255;
207 mPaint.setAlpha(alpha);
208 }
Joe Onorato00acb122009-08-04 16:04:30 -0400209 canvas.drawBitmap(mBitmap, 0.0f, 0.0f, mPaint);
Adam Cohened66b2b2012-01-23 17:28:51 -0800210 if (crossFade) {
211 mPaint.setAlpha((int) (255 * mCrossFadeProgress));
212 canvas.save();
213 float sX = (mBitmap.getWidth() * 1.0f) / mCrossFadeBitmap.getWidth();
214 float sY = (mBitmap.getHeight() * 1.0f) / mCrossFadeBitmap.getHeight();
215 canvas.scale(sX, sY);
216 canvas.drawBitmap(mCrossFadeBitmap, 0.0f, 0.0f, mPaint);
217 canvas.restore();
218 }
219 }
220
221 public void setCrossFadeBitmap(Bitmap crossFadeBitmap) {
222 mCrossFadeBitmap = crossFadeBitmap;
223 }
224
225 public void crossFade(int duration) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100226 ValueAnimator va = LauncherAnimUtils.ofFloat(this, 0f, 1f);
Adam Cohened66b2b2012-01-23 17:28:51 -0800227 va.setDuration(duration);
228 va.setInterpolator(new DecelerateInterpolator(1.5f));
229 va.addUpdateListener(new AnimatorUpdateListener() {
230 @Override
231 public void onAnimationUpdate(ValueAnimator animation) {
232 mCrossFadeProgress = animation.getAnimatedFraction();
233 }
234 });
235 va.start();
Joe Onorato00acb122009-08-04 16:04:30 -0400236 }
237
Winson Chung61967cb2012-02-28 18:11:33 -0800238 public void setColor(int color) {
Michael Jurka6cfafb92012-02-23 18:25:43 -0800239 if (mPaint == null) {
240 mPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
241 }
Winson Chung61967cb2012-02-28 18:11:33 -0800242 if (color != 0) {
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700243 ColorMatrix m1 = new ColorMatrix();
244 m1.setSaturation(0);
245
246 ColorMatrix m2 = new ColorMatrix();
247 m2.setScale(Color.red(color) / 255f, Color.green(color) / 255f,
248 Color.blue(color) / 255f, Color.alpha(color) / 255f);
249 m1.postConcat(m2);
250
251 if (Utilities.isLmpOrAbove()) {
252 animateFilterTo(m1.getArray());
253 } else {
254 mPaint.setColorFilter(new ColorMatrixColorFilter(m1));
255 invalidate();
256 }
Winson Chung61967cb2012-02-28 18:11:33 -0800257 } else {
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700258 if (!Utilities.isLmpOrAbove() || mCurrentFilter == null) {
259 mPaint.setColorFilter(null);
260 invalidate();
261 } else {
262 animateFilterTo(new ColorMatrix().getArray());
263 }
Winson Chung61967cb2012-02-28 18:11:33 -0800264 }
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700265 }
266
267 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
268 private void animateFilterTo(float[] targetFilter) {
269 float[] oldFilter = mCurrentFilter == null ? new ColorMatrix().getArray() : mCurrentFilter;
270 mCurrentFilter = Arrays.copyOf(oldFilter, oldFilter.length);
271
272 if (mFilterAnimator != null) {
273 mFilterAnimator.cancel();
274 }
275 mFilterAnimator = ValueAnimator.ofObject(new FloatArrayEvaluator(mCurrentFilter),
276 oldFilter, targetFilter);
277 mFilterAnimator.setDuration(COLOR_CHANGE_DURATION);
278 mFilterAnimator.addUpdateListener(new AnimatorUpdateListener() {
279
280 @Override
281 public void onAnimationUpdate(ValueAnimator animation) {
282 mPaint.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter));
283 invalidate();
284 }
285 });
286 mFilterAnimator.start();
Joe Onorato00acb122009-08-04 16:04:30 -0400287 }
288
Adam Cohenfc53cd22011-07-20 15:45:11 -0700289 public boolean hasDrawn() {
290 return mHasDrawn;
291 }
292
Adam Cohen3e8f8112011-07-02 18:03:00 -0700293 @Override
294 public void setAlpha(float alpha) {
295 super.setAlpha(alpha);
Adam Cohen3e8f8112011-07-02 18:03:00 -0700296 mPaint.setAlpha((int) (255 * alpha));
297 invalidate();
298 }
299
Joe Onorato00acb122009-08-04 16:04:30 -0400300 /**
301 * Create a window containing this view and show it.
302 *
303 * @param windowToken obtained from v.getWindowToken() from one of your views
Adam Cohen8dfcba42011-07-07 16:38:18 -0700304 * @param touchX the x coordinate the user touched in DragLayer coordinates
305 * @param touchY the y coordinate the user touched in DragLayer coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400306 */
Adam Cohen8dfcba42011-07-07 16:38:18 -0700307 public void show(int touchX, int touchY) {
308 mDragLayer.addView(this);
Winson Chung043f2af2012-03-01 16:09:54 -0800309
Winson Chung043f2af2012-03-01 16:09:54 -0800310 // Start the pick-up animation
Adam Cohen8dfcba42011-07-07 16:38:18 -0700311 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(0, 0);
312 lp.width = mBitmap.getWidth();
313 lp.height = mBitmap.getHeight();
Adam Cohen8dfcba42011-07-07 16:38:18 -0700314 lp.customPosition = true;
315 setLayoutParams(lp);
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800316 setTranslationX(touchX - mRegistrationX);
317 setTranslationY(touchY - mRegistrationY);
Michael Jurkaca993832012-06-29 15:17:04 -0700318 // Post the animation to skip other expensive work happening on the first frame
319 post(new Runnable() {
320 public void run() {
321 mAnim.start();
322 }
323 });
Joe Onorato00acb122009-08-04 16:04:30 -0400324 }
Adam Cohen716b51e2011-06-30 12:09:54 -0700325
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800326 public void cancelAnimation() {
327 if (mAnim != null && mAnim.isRunning()) {
328 mAnim.cancel();
329 }
330 }
331
332 public void resetLayoutParams() {
333 mOffsetX = mOffsetY = 0;
334 requestLayout();
335 }
336
Joe Onorato00acb122009-08-04 16:04:30 -0400337 /**
338 * Move the window containing this view.
339 *
Adam Cohen8dfcba42011-07-07 16:38:18 -0700340 * @param touchX the x coordinate the user touched in DragLayer coordinates
341 * @param touchY the y coordinate the user touched in DragLayer coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400342 */
343 void move(int touchX, int touchY) {
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800344 setTranslationX(touchX - mRegistrationX + (int) mOffsetX);
345 setTranslationY(touchY - mRegistrationY + (int) mOffsetY);
Joe Onorato00acb122009-08-04 16:04:30 -0400346 }
347
348 void remove() {
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800349 if (getParent() != null) {
350 mDragLayer.removeView(DragView.this);
351 }
Patrick Dubroy6f133422011-02-24 12:16:12 -0800352 }
Joe Onorato00acb122009-08-04 16:04:30 -0400353}