blob: a6aa595300f3883e0cfe3eb7de3d282d3e772f74 [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
18package com.android.launcher2;
19
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
Adam Cohen120980b2010-12-08 11:05:37 -080033import com.android.launcher.R;
34
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;
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 offsetX = res.getDimensionPixelSize(R.dimen.dragViewOffsetX);
74 final float offsetY = res.getDimensionPixelSize(R.dimen.dragViewOffsetY);
75 final float scaleDps = res.getDimensionPixelSize(R.dimen.dragViewScale);
76 final float scale = (width + scaleDps) / width;
Patrick Dubroya669d792010-11-23 14:40:33 -080077
78 // Animate the view into the correct position
79 mAnim = ValueAnimator.ofFloat(0.0f, 1.0f);
Winson Chung7bd1bbb2012-02-13 18:29:29 -080080 mAnim.setDuration(150);
Patrick Dubroya669d792010-11-23 14:40:33 -080081 mAnim.addUpdateListener(new AnimatorUpdateListener() {
82 @Override
83 public void onAnimationUpdate(ValueAnimator animation) {
84 final float value = (Float) animation.getAnimatedValue();
85
86 final int deltaX = (int) ((value * offsetX) - mOffsetX);
87 final int deltaY = (int) ((value * offsetY) - mOffsetY);
88
89 mOffsetX += deltaX;
90 mOffsetY += deltaY;
Winson Chung72d59842012-02-22 13:51:36 -080091 setScaleX(initialScale + (value * (scale - initialScale)));
92 setScaleY(initialScale + (value * (scale - initialScale)));
Winson Chung867ca622012-02-21 15:48:35 -080093 if (sDragAlpha != 1f) {
94 setAlpha(sDragAlpha * value + (1f - value));
95 }
Patrick Dubroya669d792010-11-23 14:40:33 -080096
97 if (getParent() == null) {
98 animation.cancel();
99 } else {
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800100 setTranslationX(getTranslationX() + deltaX);
101 setTranslationY(getTranslationY() + deltaY);
Patrick Dubroya669d792010-11-23 14:40:33 -0800102 }
103 }
104 });
Joe Onorato00acb122009-08-04 16:04:30 -0400105
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800106 mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height);
Adam Cohene3e27a82011-04-15 12:07:39 -0700107 setDragRegion(new Rect(0, 0, width, height));
Joe Onorato00acb122009-08-04 16:04:30 -0400108
109 // The point in our scaled bitmap that the touch events are located
Patrick Dubroya669d792010-11-23 14:40:33 -0800110 mRegistrationX = registrationX;
111 mRegistrationY = registrationY;
Patrick Dubroy62bbb3c2011-01-17 15:29:27 -0800112
113 // Force a measure, because Workspace uses getMeasuredHeight() before the layout pass
114 int ms = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
115 measure(ms, ms);
Michael Jurka6cfafb92012-02-23 18:25:43 -0800116 mPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
Patrick Dubroy62bbb3c2011-01-17 15:29:27 -0800117 }
118
119 public float getOffsetY() {
120 return mOffsetY;
Joe Onorato00acb122009-08-04 16:04:30 -0400121 }
122
Michael Jurkaa63c4522010-08-19 13:52:27 -0700123 public int getDragRegionLeft() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700124 return mDragRegion.left;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700125 }
126
127 public int getDragRegionTop() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700128 return mDragRegion.top;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700129 }
130
131 public int getDragRegionWidth() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700132 return mDragRegion.width();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700133 }
134
135 public int getDragRegionHeight() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700136 return mDragRegion.height();
137 }
138
Winson Chungb8c69f32011-10-19 21:36:08 -0700139 public void setDragVisualizeOffset(Point p) {
140 mDragVisualizeOffset = p;
141 }
142
143 public Point getDragVisualizeOffset() {
144 return mDragVisualizeOffset;
145 }
146
Adam Cohene3e27a82011-04-15 12:07:39 -0700147 public void setDragRegion(Rect r) {
148 mDragRegion = r;
149 }
150
151 public Rect getDragRegion() {
152 return mDragRegion;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700153 }
154
Winson Chung043f2af2012-03-01 16:09:54 -0800155 public float getInitialScale() {
156 return mInitialScale;
157 }
158
159 public void updateInitialScaleToCurrentScale() {
160 mInitialScale = getScaleX();
161 }
162
Joe Onorato00acb122009-08-04 16:04:30 -0400163 @Override
164 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Joe Onoratoeebd9242009-11-04 13:48:32 -0500165 setMeasuredDimension(mBitmap.getWidth(), mBitmap.getHeight());
Joe Onorato00acb122009-08-04 16:04:30 -0400166 }
167
168 @Override
169 protected void onDraw(Canvas canvas) {
170 if (false) {
171 // for debugging
172 Paint p = new Paint();
173 p.setStyle(Paint.Style.FILL);
Winson Chungeecf02d2012-03-02 17:14:58 -0800174 p.setColor(0x66ffffff);
Joe Onorato00acb122009-08-04 16:04:30 -0400175 canvas.drawRect(0, 0, getWidth(), getHeight(), p);
176 }
Patrick Dubroya669d792010-11-23 14:40:33 -0800177
Adam Cohenfc53cd22011-07-20 15:45:11 -0700178 mHasDrawn = true;
Adam Cohened66b2b2012-01-23 17:28:51 -0800179 boolean crossFade = mCrossFadeProgress > 0 && mCrossFadeBitmap != null;
180 if (crossFade) {
181 int alpha = crossFade ? (int) (255 * (1 - mCrossFadeProgress)) : 255;
182 mPaint.setAlpha(alpha);
183 }
Joe Onorato00acb122009-08-04 16:04:30 -0400184 canvas.drawBitmap(mBitmap, 0.0f, 0.0f, mPaint);
Adam Cohened66b2b2012-01-23 17:28:51 -0800185 if (crossFade) {
186 mPaint.setAlpha((int) (255 * mCrossFadeProgress));
187 canvas.save();
188 float sX = (mBitmap.getWidth() * 1.0f) / mCrossFadeBitmap.getWidth();
189 float sY = (mBitmap.getHeight() * 1.0f) / mCrossFadeBitmap.getHeight();
190 canvas.scale(sX, sY);
191 canvas.drawBitmap(mCrossFadeBitmap, 0.0f, 0.0f, mPaint);
192 canvas.restore();
193 }
194 }
195
196 public void setCrossFadeBitmap(Bitmap crossFadeBitmap) {
197 mCrossFadeBitmap = crossFadeBitmap;
198 }
199
200 public void crossFade(int duration) {
201 ValueAnimator va = ValueAnimator.ofFloat(0f, 1f);
202 va.setDuration(duration);
203 va.setInterpolator(new DecelerateInterpolator(1.5f));
204 va.addUpdateListener(new AnimatorUpdateListener() {
205 @Override
206 public void onAnimationUpdate(ValueAnimator animation) {
207 mCrossFadeProgress = animation.getAnimatedFraction();
208 }
209 });
210 va.start();
Joe Onorato00acb122009-08-04 16:04:30 -0400211 }
212
Winson Chung61967cb2012-02-28 18:11:33 -0800213 public void setColor(int color) {
Michael Jurka6cfafb92012-02-23 18:25:43 -0800214 if (mPaint == null) {
215 mPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
216 }
Winson Chung61967cb2012-02-28 18:11:33 -0800217 if (color != 0) {
218 mPaint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP));
219 } else {
220 mPaint.setColorFilter(null);
221 }
Joe Onorato00acb122009-08-04 16:04:30 -0400222 invalidate();
223 }
224
Adam Cohenfc53cd22011-07-20 15:45:11 -0700225 public boolean hasDrawn() {
226 return mHasDrawn;
227 }
228
Adam Cohen3e8f8112011-07-02 18:03:00 -0700229 @Override
230 public void setAlpha(float alpha) {
231 super.setAlpha(alpha);
Adam Cohen3e8f8112011-07-02 18:03:00 -0700232 mPaint.setAlpha((int) (255 * alpha));
233 invalidate();
234 }
235
Joe Onorato00acb122009-08-04 16:04:30 -0400236 /**
237 * Create a window containing this view and show it.
238 *
239 * @param windowToken obtained from v.getWindowToken() from one of your views
Adam Cohen8dfcba42011-07-07 16:38:18 -0700240 * @param touchX the x coordinate the user touched in DragLayer coordinates
241 * @param touchY the y coordinate the user touched in DragLayer coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400242 */
Adam Cohen8dfcba42011-07-07 16:38:18 -0700243 public void show(int touchX, int touchY) {
244 mDragLayer.addView(this);
Winson Chung043f2af2012-03-01 16:09:54 -0800245
246 // Enable hw-layers on this view
247 setLayerType(View.LAYER_TYPE_HARDWARE, null);
248
249 // Start the pick-up animation
Adam Cohen8dfcba42011-07-07 16:38:18 -0700250 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(0, 0);
251 lp.width = mBitmap.getWidth();
252 lp.height = mBitmap.getHeight();
Adam Cohen8dfcba42011-07-07 16:38:18 -0700253 lp.customPosition = true;
254 setLayoutParams(lp);
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800255 setTranslationX(touchX - mRegistrationX);
256 setTranslationY(touchY - mRegistrationY);
Patrick Dubroya669d792010-11-23 14:40:33 -0800257 mAnim.start();
Joe Onorato00acb122009-08-04 16:04:30 -0400258 }
Adam Cohen716b51e2011-06-30 12:09:54 -0700259
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800260 public void cancelAnimation() {
261 if (mAnim != null && mAnim.isRunning()) {
262 mAnim.cancel();
263 }
264 }
265
266 public void resetLayoutParams() {
267 mOffsetX = mOffsetY = 0;
268 requestLayout();
269 }
270
Joe Onorato00acb122009-08-04 16:04:30 -0400271 /**
272 * Move the window containing this view.
273 *
Adam Cohen8dfcba42011-07-07 16:38:18 -0700274 * @param touchX the x coordinate the user touched in DragLayer coordinates
275 * @param touchY the y coordinate the user touched in DragLayer coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400276 */
277 void move(int touchX, int touchY) {
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800278 setTranslationX(touchX - mRegistrationX + (int) mOffsetX);
279 setTranslationY(touchY - mRegistrationY + (int) mOffsetY);
Joe Onorato00acb122009-08-04 16:04:30 -0400280 }
281
282 void remove() {
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800283 if (getParent() != null) {
Winson Chung043f2af2012-03-01 16:09:54 -0800284 // Disable hw-layers on this view
285 setLayerType(View.LAYER_TYPE_NONE, null);
286
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800287 mDragLayer.removeView(DragView.this);
288 }
Patrick Dubroy6f133422011-02-24 12:16:12 -0800289 }
Joe Onorato00acb122009-08-04 16:04:30 -0400290}
291