blob: 1608d0816ee9943f7cd4582010b56adc9b878ee2 [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);
Patrick Dubroya669d792010-11-23 14:40:33 -080078 mAnim.addUpdateListener(new AnimatorUpdateListener() {
79 @Override
80 public void onAnimationUpdate(ValueAnimator animation) {
81 final float value = (Float) animation.getAnimatedValue();
82
83 final int deltaX = (int) ((value * offsetX) - mOffsetX);
84 final int deltaY = (int) ((value * offsetY) - mOffsetY);
85
86 mOffsetX += deltaX;
87 mOffsetY += deltaY;
Winson Chung72d59842012-02-22 13:51:36 -080088 setScaleX(initialScale + (value * (scale - initialScale)));
89 setScaleY(initialScale + (value * (scale - initialScale)));
Winson Chung867ca622012-02-21 15:48:35 -080090 if (sDragAlpha != 1f) {
91 setAlpha(sDragAlpha * value + (1f - value));
92 }
Patrick Dubroya669d792010-11-23 14:40:33 -080093
94 if (getParent() == null) {
95 animation.cancel();
96 } else {
Winson Chung7bd1bbb2012-02-13 18:29:29 -080097 setTranslationX(getTranslationX() + deltaX);
98 setTranslationY(getTranslationY() + deltaY);
Patrick Dubroya669d792010-11-23 14:40:33 -080099 }
100 }
101 });
Joe Onorato00acb122009-08-04 16:04:30 -0400102
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800103 mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height);
Adam Cohene3e27a82011-04-15 12:07:39 -0700104 setDragRegion(new Rect(0, 0, width, height));
Joe Onorato00acb122009-08-04 16:04:30 -0400105
106 // The point in our scaled bitmap that the touch events are located
Patrick Dubroya669d792010-11-23 14:40:33 -0800107 mRegistrationX = registrationX;
108 mRegistrationY = registrationY;
Patrick Dubroy62bbb3c2011-01-17 15:29:27 -0800109
110 // Force a measure, because Workspace uses getMeasuredHeight() before the layout pass
111 int ms = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
112 measure(ms, ms);
113 }
114
115 public float getOffsetY() {
116 return mOffsetY;
Joe Onorato00acb122009-08-04 16:04:30 -0400117 }
118
Michael Jurkaa63c4522010-08-19 13:52:27 -0700119 public int getDragRegionLeft() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700120 return mDragRegion.left;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700121 }
122
123 public int getDragRegionTop() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700124 return mDragRegion.top;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700125 }
126
127 public int getDragRegionWidth() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700128 return mDragRegion.width();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700129 }
130
131 public int getDragRegionHeight() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700132 return mDragRegion.height();
133 }
134
Winson Chungb8c69f32011-10-19 21:36:08 -0700135 public void setDragVisualizeOffset(Point p) {
136 mDragVisualizeOffset = p;
137 }
138
139 public Point getDragVisualizeOffset() {
140 return mDragVisualizeOffset;
141 }
142
Adam Cohene3e27a82011-04-15 12:07:39 -0700143 public void setDragRegion(Rect r) {
144 mDragRegion = r;
145 }
146
147 public Rect getDragRegion() {
148 return mDragRegion;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700149 }
150
Joe Onorato00acb122009-08-04 16:04:30 -0400151 @Override
152 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Joe Onoratoeebd9242009-11-04 13:48:32 -0500153 setMeasuredDimension(mBitmap.getWidth(), mBitmap.getHeight());
Joe Onorato00acb122009-08-04 16:04:30 -0400154 }
155
156 @Override
157 protected void onDraw(Canvas canvas) {
158 if (false) {
159 // for debugging
160 Paint p = new Paint();
161 p.setStyle(Paint.Style.FILL);
162 p.setColor(0xaaffffff);
163 canvas.drawRect(0, 0, getWidth(), getHeight(), p);
164 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800165 if (mPaint == null) {
166 mPaint = new Paint();
167 }
Patrick Dubroya669d792010-11-23 14:40:33 -0800168
Adam Cohenfc53cd22011-07-20 15:45:11 -0700169 mHasDrawn = true;
Adam Cohened66b2b2012-01-23 17:28:51 -0800170 boolean crossFade = mCrossFadeProgress > 0 && mCrossFadeBitmap != null;
171 if (crossFade) {
172 int alpha = crossFade ? (int) (255 * (1 - mCrossFadeProgress)) : 255;
173 mPaint.setAlpha(alpha);
174 }
Joe Onorato00acb122009-08-04 16:04:30 -0400175 canvas.drawBitmap(mBitmap, 0.0f, 0.0f, mPaint);
Adam Cohened66b2b2012-01-23 17:28:51 -0800176 if (crossFade) {
177 mPaint.setAlpha((int) (255 * mCrossFadeProgress));
178 canvas.save();
179 float sX = (mBitmap.getWidth() * 1.0f) / mCrossFadeBitmap.getWidth();
180 float sY = (mBitmap.getHeight() * 1.0f) / mCrossFadeBitmap.getHeight();
181 canvas.scale(sX, sY);
182 canvas.drawBitmap(mCrossFadeBitmap, 0.0f, 0.0f, mPaint);
183 canvas.restore();
184 }
185 }
186
187 public void setCrossFadeBitmap(Bitmap crossFadeBitmap) {
188 mCrossFadeBitmap = crossFadeBitmap;
189 }
190
191 public void crossFade(int duration) {
192 ValueAnimator va = ValueAnimator.ofFloat(0f, 1f);
193 va.setDuration(duration);
194 va.setInterpolator(new DecelerateInterpolator(1.5f));
195 va.addUpdateListener(new AnimatorUpdateListener() {
196 @Override
197 public void onAnimationUpdate(ValueAnimator animation) {
198 mCrossFadeProgress = animation.getAnimatedFraction();
199 }
200 });
201 va.start();
Joe Onorato00acb122009-08-04 16:04:30 -0400202 }
203
Joe Onorato00acb122009-08-04 16:04:30 -0400204 public void setPaint(Paint paint) {
205 mPaint = paint;
206 invalidate();
207 }
208
Adam Cohenfc53cd22011-07-20 15:45:11 -0700209 public boolean hasDrawn() {
210 return mHasDrawn;
211 }
212
Adam Cohen3e8f8112011-07-02 18:03:00 -0700213 @Override
214 public void setAlpha(float alpha) {
215 super.setAlpha(alpha);
216 if (mPaint == null) {
217 mPaint = new Paint();
218 }
219 mPaint.setAlpha((int) (255 * alpha));
220 invalidate();
221 }
222
Joe Onorato00acb122009-08-04 16:04:30 -0400223 /**
224 * Create a window containing this view and show it.
225 *
226 * @param windowToken obtained from v.getWindowToken() from one of your views
Adam Cohen8dfcba42011-07-07 16:38:18 -0700227 * @param touchX the x coordinate the user touched in DragLayer coordinates
228 * @param touchY the y coordinate the user touched in DragLayer coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400229 */
Adam Cohen8dfcba42011-07-07 16:38:18 -0700230 public void show(int touchX, int touchY) {
231 mDragLayer.addView(this);
232 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(0, 0);
233 lp.width = mBitmap.getWidth();
234 lp.height = mBitmap.getHeight();
Adam Cohen8dfcba42011-07-07 16:38:18 -0700235 lp.customPosition = true;
236 setLayoutParams(lp);
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800237 setTranslationX(touchX - mRegistrationX);
238 setTranslationY(touchY - mRegistrationY);
Patrick Dubroya669d792010-11-23 14:40:33 -0800239 mAnim.start();
Joe Onorato00acb122009-08-04 16:04:30 -0400240 }
Adam Cohen716b51e2011-06-30 12:09:54 -0700241
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800242 public void cancelAnimation() {
243 if (mAnim != null && mAnim.isRunning()) {
244 mAnim.cancel();
245 }
246 }
247
248 public void resetLayoutParams() {
249 mOffsetX = mOffsetY = 0;
250 requestLayout();
251 }
252
Joe Onorato00acb122009-08-04 16:04:30 -0400253 /**
254 * Move the window containing this view.
255 *
Adam Cohen8dfcba42011-07-07 16:38:18 -0700256 * @param touchX the x coordinate the user touched in DragLayer coordinates
257 * @param touchY the y coordinate the user touched in DragLayer coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400258 */
259 void move(int touchX, int touchY) {
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800260 setTranslationX(touchX - mRegistrationX + (int) mOffsetX);
261 setTranslationY(touchY - mRegistrationY + (int) mOffsetY);
Joe Onorato00acb122009-08-04 16:04:30 -0400262 }
263
264 void remove() {
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800265 if (getParent() != null) {
266 mDragLayer.removeView(DragView.this);
267 }
Patrick Dubroy6f133422011-02-24 12:16:12 -0800268 }
Joe Onorato00acb122009-08-04 16:04:30 -0400269}
270