blob: c3c274eca892e5ffbe8348cb60ffc9e6606919cf [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);
Michael Jurka6cfafb92012-02-23 18:25:43 -0800113 mPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
Patrick Dubroy62bbb3c2011-01-17 15:29:27 -0800114 }
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 }
Patrick Dubroya669d792010-11-23 14:40:33 -0800166
Adam Cohenfc53cd22011-07-20 15:45:11 -0700167 mHasDrawn = true;
Adam Cohened66b2b2012-01-23 17:28:51 -0800168 boolean crossFade = mCrossFadeProgress > 0 && mCrossFadeBitmap != null;
169 if (crossFade) {
170 int alpha = crossFade ? (int) (255 * (1 - mCrossFadeProgress)) : 255;
171 mPaint.setAlpha(alpha);
172 }
Joe Onorato00acb122009-08-04 16:04:30 -0400173 canvas.drawBitmap(mBitmap, 0.0f, 0.0f, mPaint);
Adam Cohened66b2b2012-01-23 17:28:51 -0800174 if (crossFade) {
175 mPaint.setAlpha((int) (255 * mCrossFadeProgress));
176 canvas.save();
177 float sX = (mBitmap.getWidth() * 1.0f) / mCrossFadeBitmap.getWidth();
178 float sY = (mBitmap.getHeight() * 1.0f) / mCrossFadeBitmap.getHeight();
179 canvas.scale(sX, sY);
180 canvas.drawBitmap(mCrossFadeBitmap, 0.0f, 0.0f, mPaint);
181 canvas.restore();
182 }
183 }
184
185 public void setCrossFadeBitmap(Bitmap crossFadeBitmap) {
186 mCrossFadeBitmap = crossFadeBitmap;
187 }
188
189 public void crossFade(int duration) {
190 ValueAnimator va = ValueAnimator.ofFloat(0f, 1f);
191 va.setDuration(duration);
192 va.setInterpolator(new DecelerateInterpolator(1.5f));
193 va.addUpdateListener(new AnimatorUpdateListener() {
194 @Override
195 public void onAnimationUpdate(ValueAnimator animation) {
196 mCrossFadeProgress = animation.getAnimatedFraction();
197 }
198 });
199 va.start();
Joe Onorato00acb122009-08-04 16:04:30 -0400200 }
201
Joe Onorato00acb122009-08-04 16:04:30 -0400202 public void setPaint(Paint paint) {
203 mPaint = paint;
Michael Jurka6cfafb92012-02-23 18:25:43 -0800204 if (mPaint == null) {
205 mPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
206 }
Joe Onorato00acb122009-08-04 16:04:30 -0400207 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);
Adam Cohen3e8f8112011-07-02 18:03:00 -0700217 mPaint.setAlpha((int) (255 * alpha));
218 invalidate();
219 }
220
Joe Onorato00acb122009-08-04 16:04:30 -0400221 /**
222 * Create a window containing this view and show it.
223 *
224 * @param windowToken obtained from v.getWindowToken() from one of your views
Adam Cohen8dfcba42011-07-07 16:38:18 -0700225 * @param touchX the x coordinate the user touched in DragLayer coordinates
226 * @param touchY the y coordinate the user touched in DragLayer coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400227 */
Adam Cohen8dfcba42011-07-07 16:38:18 -0700228 public void show(int touchX, int touchY) {
229 mDragLayer.addView(this);
230 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(0, 0);
231 lp.width = mBitmap.getWidth();
232 lp.height = mBitmap.getHeight();
Adam Cohen8dfcba42011-07-07 16:38:18 -0700233 lp.customPosition = true;
234 setLayoutParams(lp);
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800235 setTranslationX(touchX - mRegistrationX);
236 setTranslationY(touchY - mRegistrationY);
Patrick Dubroya669d792010-11-23 14:40:33 -0800237 mAnim.start();
Joe Onorato00acb122009-08-04 16:04:30 -0400238 }
Adam Cohen716b51e2011-06-30 12:09:54 -0700239
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800240 public void cancelAnimation() {
241 if (mAnim != null && mAnim.isRunning()) {
242 mAnim.cancel();
243 }
244 }
245
246 public void resetLayoutParams() {
247 mOffsetX = mOffsetY = 0;
248 requestLayout();
249 }
250
Joe Onorato00acb122009-08-04 16:04:30 -0400251 /**
252 * Move the window containing this view.
253 *
Adam Cohen8dfcba42011-07-07 16:38:18 -0700254 * @param touchX the x coordinate the user touched in DragLayer coordinates
255 * @param touchY the y coordinate the user touched in DragLayer coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400256 */
257 void move(int touchX, int touchY) {
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800258 setTranslationX(touchX - mRegistrationX + (int) mOffsetX);
259 setTranslationY(touchY - mRegistrationY + (int) mOffsetY);
Joe Onorato00acb122009-08-04 16:04:30 -0400260 }
261
262 void remove() {
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800263 if (getParent() != null) {
264 mDragLayer.removeView(DragView.this);
265 }
Patrick Dubroy6f133422011-02-24 12:16:12 -0800266 }
Joe Onorato00acb122009-08-04 16:04:30 -0400267}
268