blob: 1b3029334fc6af74153537a9d201a45f14fd74de [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;
25import android.graphics.Matrix;
26import android.graphics.Paint;
Winson Chungb8c69f32011-10-19 21:36:08 -070027import android.graphics.Point;
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 Cohen120980b2010-12-08 11:05:37 -080032import com.android.launcher.R;
33
Patrick Dubroya669d792010-11-23 14:40:33 -080034public class DragView extends View {
Winson Chung867ca622012-02-21 15:48:35 -080035 private 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 Cohened66b2b2012-01-23 17:28:51 -080047 private float mCrossFadeProgress = 0f;
Michael Jurkaa63c4522010-08-19 13:52:27 -070048
Patrick Dubroya669d792010-11-23 14:40:33 -080049 ValueAnimator mAnim;
Patrick Dubroya669d792010-11-23 14:40:33 -080050 private float mOffsetX = 0.0f;
51 private float mOffsetY = 0.0f;
Joe Onorato00acb122009-08-04 16:04:30 -040052
Joe Onorato00acb122009-08-04 16:04:30 -040053 /**
54 * Construct the drag view.
55 * <p>
56 * The registration point is the point inside our view that the touch events should
57 * be centered upon.
58 *
Adam Cohen8dfcba42011-07-07 16:38:18 -070059 * @param launcher The Launcher instance
Joe Onorato00acb122009-08-04 16:04:30 -040060 * @param bitmap The view that we're dragging around. We scale it up when we draw it.
61 * @param registrationX The x coordinate of the registration point.
62 * @param registrationY The y coordinate of the registration point.
63 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070064 public DragView(Launcher launcher, Bitmap bitmap, int registrationX, int registrationY,
Winson Chung72d59842012-02-22 13:51:36 -080065 int left, int top, int width, int height, final float initialScale) {
Adam Cohen8dfcba42011-07-07 16:38:18 -070066 super(launcher);
67 mDragLayer = launcher.getDragLayer();
Joe Onorato00acb122009-08-04 16:04:30 -040068
Patrick Dubroyde7658b2010-09-27 11:15:43 -070069 final Resources res = getResources();
Winson Chung7bd1bbb2012-02-13 18:29:29 -080070 final float offsetX = res.getDimensionPixelSize(R.dimen.dragViewOffsetX);
71 final float offsetY = res.getDimensionPixelSize(R.dimen.dragViewOffsetY);
72 final float scaleDps = res.getDimensionPixelSize(R.dimen.dragViewScale);
73 final float scale = (width + scaleDps) / width;
Patrick Dubroya669d792010-11-23 14:40:33 -080074
75 // Animate the view into the correct position
76 mAnim = ValueAnimator.ofFloat(0.0f, 1.0f);
Winson Chung7bd1bbb2012-02-13 18:29:29 -080077 mAnim.setDuration(150);
Winson Chung867ca622012-02-21 15:48:35 -080078 mAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Patrick Dubroya669d792010-11-23 14:40:33 -080079 mAnim.addUpdateListener(new AnimatorUpdateListener() {
80 @Override
81 public void onAnimationUpdate(ValueAnimator animation) {
82 final float value = (Float) animation.getAnimatedValue();
83
84 final int deltaX = (int) ((value * offsetX) - mOffsetX);
85 final int deltaY = (int) ((value * offsetY) - mOffsetY);
86
87 mOffsetX += deltaX;
88 mOffsetY += deltaY;
Winson Chung72d59842012-02-22 13:51:36 -080089 setScaleX(initialScale + (value * (scale - initialScale)));
90 setScaleY(initialScale + (value * (scale - initialScale)));
Winson Chung867ca622012-02-21 15:48:35 -080091 if (sDragAlpha != 1f) {
92 setAlpha(sDragAlpha * value + (1f - value));
93 }
Patrick Dubroya669d792010-11-23 14:40:33 -080094
95 if (getParent() == null) {
96 animation.cancel();
97 } else {
Winson Chung7bd1bbb2012-02-13 18:29:29 -080098 setTranslationX(getTranslationX() + deltaX);
99 setTranslationY(getTranslationY() + deltaY);
Patrick Dubroya669d792010-11-23 14:40:33 -0800100 }
101 }
102 });
Joe Onorato00acb122009-08-04 16:04:30 -0400103
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800104 mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height);
Adam Cohene3e27a82011-04-15 12:07:39 -0700105 setDragRegion(new Rect(0, 0, width, height));
Joe Onorato00acb122009-08-04 16:04:30 -0400106
107 // The point in our scaled bitmap that the touch events are located
Patrick Dubroya669d792010-11-23 14:40:33 -0800108 mRegistrationX = registrationX;
109 mRegistrationY = registrationY;
Patrick Dubroy62bbb3c2011-01-17 15:29:27 -0800110
111 // Force a measure, because Workspace uses getMeasuredHeight() before the layout pass
112 int ms = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
113 measure(ms, ms);
114 }
115
116 public float getOffsetY() {
117 return mOffsetY;
Joe Onorato00acb122009-08-04 16:04:30 -0400118 }
119
Michael Jurkaa63c4522010-08-19 13:52:27 -0700120 public int getDragRegionLeft() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700121 return mDragRegion.left;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700122 }
123
124 public int getDragRegionTop() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700125 return mDragRegion.top;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700126 }
127
128 public int getDragRegionWidth() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700129 return mDragRegion.width();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700130 }
131
132 public int getDragRegionHeight() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700133 return mDragRegion.height();
134 }
135
Winson Chungb8c69f32011-10-19 21:36:08 -0700136 public void setDragVisualizeOffset(Point p) {
137 mDragVisualizeOffset = p;
138 }
139
140 public Point getDragVisualizeOffset() {
141 return mDragVisualizeOffset;
142 }
143
Adam Cohene3e27a82011-04-15 12:07:39 -0700144 public void setDragRegion(Rect r) {
145 mDragRegion = r;
146 }
147
148 public Rect getDragRegion() {
149 return mDragRegion;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700150 }
151
Joe Onorato00acb122009-08-04 16:04:30 -0400152 @Override
153 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Joe Onoratoeebd9242009-11-04 13:48:32 -0500154 setMeasuredDimension(mBitmap.getWidth(), mBitmap.getHeight());
Joe Onorato00acb122009-08-04 16:04:30 -0400155 }
156
157 @Override
158 protected void onDraw(Canvas canvas) {
159 if (false) {
160 // for debugging
161 Paint p = new Paint();
162 p.setStyle(Paint.Style.FILL);
163 p.setColor(0xaaffffff);
164 canvas.drawRect(0, 0, getWidth(), getHeight(), p);
165 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800166 if (mPaint == null) {
167 mPaint = new Paint();
168 }
Patrick Dubroya669d792010-11-23 14:40:33 -0800169
Adam Cohenfc53cd22011-07-20 15:45:11 -0700170 mHasDrawn = true;
Adam Cohened66b2b2012-01-23 17:28:51 -0800171 boolean crossFade = mCrossFadeProgress > 0 && mCrossFadeBitmap != null;
172 if (crossFade) {
173 int alpha = crossFade ? (int) (255 * (1 - mCrossFadeProgress)) : 255;
174 mPaint.setAlpha(alpha);
175 }
Joe Onorato00acb122009-08-04 16:04:30 -0400176 canvas.drawBitmap(mBitmap, 0.0f, 0.0f, mPaint);
Adam Cohened66b2b2012-01-23 17:28:51 -0800177 if (crossFade) {
178 mPaint.setAlpha((int) (255 * mCrossFadeProgress));
179 canvas.save();
180 float sX = (mBitmap.getWidth() * 1.0f) / mCrossFadeBitmap.getWidth();
181 float sY = (mBitmap.getHeight() * 1.0f) / mCrossFadeBitmap.getHeight();
182 canvas.scale(sX, sY);
183 canvas.drawBitmap(mCrossFadeBitmap, 0.0f, 0.0f, mPaint);
184 canvas.restore();
185 }
186 }
187
188 public void setCrossFadeBitmap(Bitmap crossFadeBitmap) {
189 mCrossFadeBitmap = crossFadeBitmap;
190 }
191
192 public void crossFade(int duration) {
193 ValueAnimator va = ValueAnimator.ofFloat(0f, 1f);
194 va.setDuration(duration);
195 va.setInterpolator(new DecelerateInterpolator(1.5f));
196 va.addUpdateListener(new AnimatorUpdateListener() {
197 @Override
198 public void onAnimationUpdate(ValueAnimator animation) {
199 mCrossFadeProgress = animation.getAnimatedFraction();
200 }
201 });
202 va.start();
Joe Onorato00acb122009-08-04 16:04:30 -0400203 }
204
Joe Onorato00acb122009-08-04 16:04:30 -0400205 public void setPaint(Paint paint) {
206 mPaint = paint;
207 invalidate();
208 }
209
Adam Cohenfc53cd22011-07-20 15:45:11 -0700210 public boolean hasDrawn() {
211 return mHasDrawn;
212 }
213
Adam Cohen3e8f8112011-07-02 18:03:00 -0700214 @Override
215 public void setAlpha(float alpha) {
216 super.setAlpha(alpha);
217 if (mPaint == null) {
218 mPaint = new Paint();
219 }
220 mPaint.setAlpha((int) (255 * alpha));
221 invalidate();
222 }
223
Joe Onorato00acb122009-08-04 16:04:30 -0400224 /**
225 * Create a window containing this view and show it.
226 *
227 * @param windowToken obtained from v.getWindowToken() from one of your views
Adam Cohen8dfcba42011-07-07 16:38:18 -0700228 * @param touchX the x coordinate the user touched in DragLayer coordinates
229 * @param touchY the y coordinate the user touched in DragLayer coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400230 */
Adam Cohen8dfcba42011-07-07 16:38:18 -0700231 public void show(int touchX, int touchY) {
232 mDragLayer.addView(this);
233 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(0, 0);
234 lp.width = mBitmap.getWidth();
235 lp.height = mBitmap.getHeight();
Adam Cohen8dfcba42011-07-07 16:38:18 -0700236 lp.customPosition = true;
237 setLayoutParams(lp);
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800238 setTranslationX(touchX - mRegistrationX);
239 setTranslationY(touchY - mRegistrationY);
Patrick Dubroya669d792010-11-23 14:40:33 -0800240 mAnim.start();
Joe Onorato00acb122009-08-04 16:04:30 -0400241 }
Adam Cohen716b51e2011-06-30 12:09:54 -0700242
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800243 public void cancelAnimation() {
244 if (mAnim != null && mAnim.isRunning()) {
245 mAnim.cancel();
246 }
247 }
248
249 public void resetLayoutParams() {
250 mOffsetX = mOffsetY = 0;
251 requestLayout();
252 }
253
Joe Onorato00acb122009-08-04 16:04:30 -0400254 /**
255 * Move the window containing this view.
256 *
Adam Cohen8dfcba42011-07-07 16:38:18 -0700257 * @param touchX the x coordinate the user touched in DragLayer coordinates
258 * @param touchY the y coordinate the user touched in DragLayer coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400259 */
260 void move(int touchX, int touchY) {
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800261 setTranslationX(touchX - mRegistrationX + (int) mOffsetX);
262 setTranslationY(touchY - mRegistrationY + (int) mOffsetY);
Joe Onorato00acb122009-08-04 16:04:30 -0400263 }
264
265 void remove() {
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800266 if (getParent() != null) {
267 mDragLayer.removeView(DragView.this);
268 }
Patrick Dubroy6f133422011-02-24 12:16:12 -0800269 }
Joe Onorato00acb122009-08-04 16:04:30 -0400270}
271