blob: dfa8202a73f2647d8de812c34c906d2f05f28d53 [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 Goyal3a644ed2015-05-21 10:28:02 -070041 public static int COLOR_CHANGE_DURATION = 120;
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070042
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;
Sunny Goyal316490e2015-06-02 09:38:28 -070047 @Thunk Paint mPaint;
Joe Onorato00acb122009-08-04 16:04:30 -040048 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 Goyal316490e2015-06-02 09:38:28 -070065 @Thunk float[] mCurrentFilter;
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070066 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 */
Sunny Goyal70660032015-05-14 00:07:08 -070079 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen8dfcba42011-07-07 16:38:18 -070080 public DragView(Launcher launcher, Bitmap bitmap, int registrationX, int registrationY,
Winson Chung72d59842012-02-22 13:51:36 -080081 int left, int top, int width, int height, final float initialScale) {
Adam Cohen8dfcba42011-07-07 16:38:18 -070082 super(launcher);
83 mDragLayer = launcher.getDragLayer();
Winson Chung043f2af2012-03-01 16:09:54 -080084 mInitialScale = initialScale;
Joe Onorato00acb122009-08-04 16:04:30 -040085
Patrick Dubroyde7658b2010-09-27 11:15:43 -070086 final Resources res = getResources();
Winson Chung7bd1bbb2012-02-13 18:29:29 -080087 final float scaleDps = res.getDimensionPixelSize(R.dimen.dragViewScale);
88 final float scale = (width + scaleDps) / width;
Patrick Dubroya669d792010-11-23 14:40:33 -080089
Adam Cohen307fe232012-08-16 17:55:58 -070090 // Set the initial scale to avoid any jumps
91 setScaleX(initialScale);
92 setScaleY(initialScale);
93
Patrick Dubroya669d792010-11-23 14:40:33 -080094 // Animate the view into the correct position
Michael Jurkaf1ad6082013-03-13 12:55:46 +010095 mAnim = LauncherAnimUtils.ofFloat(this, 0f, 1f);
Winson Chung7bd1bbb2012-02-13 18:29:29 -080096 mAnim.setDuration(150);
Patrick Dubroya669d792010-11-23 14:40:33 -080097 mAnim.addUpdateListener(new AnimatorUpdateListener() {
98 @Override
99 public void onAnimationUpdate(ValueAnimator animation) {
100 final float value = (Float) animation.getAnimatedValue();
101
Sunny Goyal560616d2015-02-26 11:26:19 -0800102 final int deltaX = (int) (-mOffsetX);
103 final int deltaY = (int) (-mOffsetY);
Patrick Dubroya669d792010-11-23 14:40:33 -0800104
105 mOffsetX += deltaX;
106 mOffsetY += deltaY;
Winson Chung72d59842012-02-22 13:51:36 -0800107 setScaleX(initialScale + (value * (scale - initialScale)));
108 setScaleY(initialScale + (value * (scale - initialScale)));
Winson Chung867ca622012-02-21 15:48:35 -0800109 if (sDragAlpha != 1f) {
110 setAlpha(sDragAlpha * value + (1f - value));
111 }
Patrick Dubroya669d792010-11-23 14:40:33 -0800112
113 if (getParent() == null) {
114 animation.cancel();
115 } else {
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800116 setTranslationX(getTranslationX() + deltaX);
117 setTranslationY(getTranslationY() + deltaY);
Patrick Dubroya669d792010-11-23 14:40:33 -0800118 }
119 }
120 });
Joe Onorato00acb122009-08-04 16:04:30 -0400121
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800122 mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height);
Adam Cohene3e27a82011-04-15 12:07:39 -0700123 setDragRegion(new Rect(0, 0, width, height));
Joe Onorato00acb122009-08-04 16:04:30 -0400124
125 // The point in our scaled bitmap that the touch events are located
Patrick Dubroya669d792010-11-23 14:40:33 -0800126 mRegistrationX = registrationX;
127 mRegistrationY = registrationY;
Patrick Dubroy62bbb3c2011-01-17 15:29:27 -0800128
129 // Force a measure, because Workspace uses getMeasuredHeight() before the layout pass
130 int ms = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
131 measure(ms, ms);
Michael Jurka6cfafb92012-02-23 18:25:43 -0800132 mPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
Sunny Goyal75deaf32015-05-07 16:13:12 -0700133
134 if (Utilities.isLmpOrAbove()) {
135 setElevation(getResources().getDimension(R.dimen.drag_elevation));
136 }
Patrick Dubroy62bbb3c2011-01-17 15:29:27 -0800137 }
138
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800139 /** Sets the scale of the view over the normal workspace icon size. */
140 public void setIntrinsicIconScaleFactor(float scale) {
141 mIntrinsicIconScale = scale;
142 }
143
144 public float getIntrinsicIconScaleFactor() {
145 return mIntrinsicIconScale;
146 }
147
Patrick Dubroy62bbb3c2011-01-17 15:29:27 -0800148 public float getOffsetY() {
149 return mOffsetY;
Joe Onorato00acb122009-08-04 16:04:30 -0400150 }
151
Michael Jurkaa63c4522010-08-19 13:52:27 -0700152 public int getDragRegionLeft() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700153 return mDragRegion.left;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700154 }
155
156 public int getDragRegionTop() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700157 return mDragRegion.top;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700158 }
159
160 public int getDragRegionWidth() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700161 return mDragRegion.width();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700162 }
163
164 public int getDragRegionHeight() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700165 return mDragRegion.height();
166 }
167
Winson Chungb8c69f32011-10-19 21:36:08 -0700168 public void setDragVisualizeOffset(Point p) {
169 mDragVisualizeOffset = p;
170 }
171
172 public Point getDragVisualizeOffset() {
173 return mDragVisualizeOffset;
174 }
175
Adam Cohene3e27a82011-04-15 12:07:39 -0700176 public void setDragRegion(Rect r) {
177 mDragRegion = r;
178 }
179
180 public Rect getDragRegion() {
181 return mDragRegion;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700182 }
183
Winson Chung043f2af2012-03-01 16:09:54 -0800184 public float getInitialScale() {
185 return mInitialScale;
186 }
187
188 public void updateInitialScaleToCurrentScale() {
189 mInitialScale = getScaleX();
190 }
191
Joe Onorato00acb122009-08-04 16:04:30 -0400192 @Override
193 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Joe Onoratoeebd9242009-11-04 13:48:32 -0500194 setMeasuredDimension(mBitmap.getWidth(), mBitmap.getHeight());
Joe Onorato00acb122009-08-04 16:04:30 -0400195 }
196
197 @Override
198 protected void onDraw(Canvas canvas) {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700199 @SuppressWarnings("all") // suppress dead code warning
200 final boolean debug = false;
201 if (debug) {
Joe Onorato00acb122009-08-04 16:04:30 -0400202 Paint p = new Paint();
203 p.setStyle(Paint.Style.FILL);
Winson Chungeecf02d2012-03-02 17:14:58 -0800204 p.setColor(0x66ffffff);
Joe Onorato00acb122009-08-04 16:04:30 -0400205 canvas.drawRect(0, 0, getWidth(), getHeight(), p);
206 }
Patrick Dubroya669d792010-11-23 14:40:33 -0800207
Adam Cohenfc53cd22011-07-20 15:45:11 -0700208 mHasDrawn = true;
Adam Cohened66b2b2012-01-23 17:28:51 -0800209 boolean crossFade = mCrossFadeProgress > 0 && mCrossFadeBitmap != null;
210 if (crossFade) {
211 int alpha = crossFade ? (int) (255 * (1 - mCrossFadeProgress)) : 255;
212 mPaint.setAlpha(alpha);
213 }
Joe Onorato00acb122009-08-04 16:04:30 -0400214 canvas.drawBitmap(mBitmap, 0.0f, 0.0f, mPaint);
Adam Cohened66b2b2012-01-23 17:28:51 -0800215 if (crossFade) {
216 mPaint.setAlpha((int) (255 * mCrossFadeProgress));
217 canvas.save();
218 float sX = (mBitmap.getWidth() * 1.0f) / mCrossFadeBitmap.getWidth();
219 float sY = (mBitmap.getHeight() * 1.0f) / mCrossFadeBitmap.getHeight();
220 canvas.scale(sX, sY);
221 canvas.drawBitmap(mCrossFadeBitmap, 0.0f, 0.0f, mPaint);
222 canvas.restore();
223 }
224 }
225
226 public void setCrossFadeBitmap(Bitmap crossFadeBitmap) {
227 mCrossFadeBitmap = crossFadeBitmap;
228 }
229
230 public void crossFade(int duration) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100231 ValueAnimator va = LauncherAnimUtils.ofFloat(this, 0f, 1f);
Adam Cohened66b2b2012-01-23 17:28:51 -0800232 va.setDuration(duration);
233 va.setInterpolator(new DecelerateInterpolator(1.5f));
234 va.addUpdateListener(new AnimatorUpdateListener() {
235 @Override
236 public void onAnimationUpdate(ValueAnimator animation) {
237 mCrossFadeProgress = animation.getAnimatedFraction();
238 }
239 });
240 va.start();
Joe Onorato00acb122009-08-04 16:04:30 -0400241 }
242
Winson Chung61967cb2012-02-28 18:11:33 -0800243 public void setColor(int color) {
Michael Jurka6cfafb92012-02-23 18:25:43 -0800244 if (mPaint == null) {
245 mPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
246 }
Winson Chung61967cb2012-02-28 18:11:33 -0800247 if (color != 0) {
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700248 ColorMatrix m1 = new ColorMatrix();
249 m1.setSaturation(0);
250
251 ColorMatrix m2 = new ColorMatrix();
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700252 setColorScale(color, m2);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700253 m1.postConcat(m2);
254
255 if (Utilities.isLmpOrAbove()) {
256 animateFilterTo(m1.getArray());
257 } else {
258 mPaint.setColorFilter(new ColorMatrixColorFilter(m1));
259 invalidate();
260 }
Winson Chung61967cb2012-02-28 18:11:33 -0800261 } else {
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700262 if (!Utilities.isLmpOrAbove() || mCurrentFilter == null) {
263 mPaint.setColorFilter(null);
264 invalidate();
265 } else {
266 animateFilterTo(new ColorMatrix().getArray());
267 }
Winson Chung61967cb2012-02-28 18:11:33 -0800268 }
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700269 }
270
271 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
272 private void animateFilterTo(float[] targetFilter) {
273 float[] oldFilter = mCurrentFilter == null ? new ColorMatrix().getArray() : mCurrentFilter;
274 mCurrentFilter = Arrays.copyOf(oldFilter, oldFilter.length);
275
276 if (mFilterAnimator != null) {
277 mFilterAnimator.cancel();
278 }
279 mFilterAnimator = ValueAnimator.ofObject(new FloatArrayEvaluator(mCurrentFilter),
280 oldFilter, targetFilter);
281 mFilterAnimator.setDuration(COLOR_CHANGE_DURATION);
282 mFilterAnimator.addUpdateListener(new AnimatorUpdateListener() {
283
284 @Override
285 public void onAnimationUpdate(ValueAnimator animation) {
286 mPaint.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter));
287 invalidate();
288 }
289 });
290 mFilterAnimator.start();
Joe Onorato00acb122009-08-04 16:04:30 -0400291 }
292
Adam Cohenfc53cd22011-07-20 15:45:11 -0700293 public boolean hasDrawn() {
294 return mHasDrawn;
295 }
296
Adam Cohen3e8f8112011-07-02 18:03:00 -0700297 @Override
298 public void setAlpha(float alpha) {
299 super.setAlpha(alpha);
Adam Cohen3e8f8112011-07-02 18:03:00 -0700300 mPaint.setAlpha((int) (255 * alpha));
301 invalidate();
302 }
303
Joe Onorato00acb122009-08-04 16:04:30 -0400304 /**
305 * Create a window containing this view and show it.
306 *
307 * @param windowToken obtained from v.getWindowToken() from one of your views
Adam Cohen8dfcba42011-07-07 16:38:18 -0700308 * @param touchX the x coordinate the user touched in DragLayer coordinates
309 * @param touchY the y coordinate the user touched in DragLayer coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400310 */
Adam Cohen8dfcba42011-07-07 16:38:18 -0700311 public void show(int touchX, int touchY) {
312 mDragLayer.addView(this);
Winson Chung043f2af2012-03-01 16:09:54 -0800313
Winson Chung043f2af2012-03-01 16:09:54 -0800314 // Start the pick-up animation
Adam Cohen8dfcba42011-07-07 16:38:18 -0700315 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(0, 0);
316 lp.width = mBitmap.getWidth();
317 lp.height = mBitmap.getHeight();
Adam Cohen8dfcba42011-07-07 16:38:18 -0700318 lp.customPosition = true;
319 setLayoutParams(lp);
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800320 setTranslationX(touchX - mRegistrationX);
321 setTranslationY(touchY - mRegistrationY);
Michael Jurkaca993832012-06-29 15:17:04 -0700322 // Post the animation to skip other expensive work happening on the first frame
323 post(new Runnable() {
324 public void run() {
325 mAnim.start();
326 }
327 });
Joe Onorato00acb122009-08-04 16:04:30 -0400328 }
Adam Cohen716b51e2011-06-30 12:09:54 -0700329
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800330 public void cancelAnimation() {
331 if (mAnim != null && mAnim.isRunning()) {
332 mAnim.cancel();
333 }
334 }
335
336 public void resetLayoutParams() {
337 mOffsetX = mOffsetY = 0;
338 requestLayout();
339 }
340
Joe Onorato00acb122009-08-04 16:04:30 -0400341 /**
342 * Move the window containing this view.
343 *
Adam Cohen8dfcba42011-07-07 16:38:18 -0700344 * @param touchX the x coordinate the user touched in DragLayer coordinates
345 * @param touchY the y coordinate the user touched in DragLayer coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400346 */
347 void move(int touchX, int touchY) {
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800348 setTranslationX(touchX - mRegistrationX + (int) mOffsetX);
349 setTranslationY(touchY - mRegistrationY + (int) mOffsetY);
Joe Onorato00acb122009-08-04 16:04:30 -0400350 }
351
352 void remove() {
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800353 if (getParent() != null) {
354 mDragLayer.removeView(DragView.this);
355 }
Patrick Dubroy6f133422011-02-24 12:16:12 -0800356 }
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700357
358 public static void setColorScale(int color, ColorMatrix target) {
359 target.setScale(Color.red(color) / 255f, Color.green(color) / 255f,
360 Color.blue(color) / 255f, Color.alpha(color) / 255f);
361 }
Joe Onorato00acb122009-08-04 16:04:30 -0400362}