blob: 5636f995d1587f0ba9baeb79a20b4e8d30d25567 [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;
Winson Chung61967cb2012-02-28 18:11:33 -080028import android.graphics.PorterDuff;
29import android.graphics.PorterDuffColorFilter;
Adam Cohene3e27a82011-04-15 12:07:39 -070030import android.graphics.Rect;
Joe Onorato00acb122009-08-04 16:04:30 -040031import android.view.View;
Patrick Dubroya669d792010-11-23 14:40:33 -080032import android.view.animation.DecelerateInterpolator;
Joe Onorato00acb122009-08-04 16:04:30 -040033
Adam Cohen120980b2010-12-08 11:05:37 -080034import com.android.launcher.R;
35
Patrick Dubroya669d792010-11-23 14:40:33 -080036public class DragView extends View {
Winson Chung867ca622012-02-21 15:48:35 -080037 private static float sDragAlpha = 1f;
Winson Chung7bd1bbb2012-02-13 18:29:29 -080038
Joe Onorato00acb122009-08-04 16:04:30 -040039 private Bitmap mBitmap;
Adam Cohened66b2b2012-01-23 17:28:51 -080040 private Bitmap mCrossFadeBitmap;
Joe Onorato00acb122009-08-04 16:04:30 -040041 private Paint mPaint;
42 private int mRegistrationX;
43 private int mRegistrationY;
44
Winson Chungb8c69f32011-10-19 21:36:08 -070045 private Point mDragVisualizeOffset = null;
Adam Cohene3e27a82011-04-15 12:07:39 -070046 private Rect mDragRegion = null;
Adam Cohen8dfcba42011-07-07 16:38:18 -070047 private DragLayer mDragLayer = null;
Adam Cohenfc53cd22011-07-20 15:45:11 -070048 private boolean mHasDrawn = false;
Adam Cohened66b2b2012-01-23 17:28:51 -080049 private float mCrossFadeProgress = 0f;
Michael Jurkaa63c4522010-08-19 13:52:27 -070050
Patrick Dubroya669d792010-11-23 14:40:33 -080051 ValueAnimator mAnim;
Patrick Dubroya669d792010-11-23 14:40:33 -080052 private float mOffsetX = 0.0f;
53 private float mOffsetY = 0.0f;
Winson Chung043f2af2012-03-01 16:09:54 -080054 private float mInitialScale = 1f;
Joe Onorato00acb122009-08-04 16:04:30 -040055
Joe Onorato00acb122009-08-04 16:04:30 -040056 /**
57 * Construct the drag view.
58 * <p>
59 * The registration point is the point inside our view that the touch events should
60 * be centered upon.
61 *
Adam Cohen8dfcba42011-07-07 16:38:18 -070062 * @param launcher The Launcher instance
Joe Onorato00acb122009-08-04 16:04:30 -040063 * @param bitmap The view that we're dragging around. We scale it up when we draw it.
64 * @param registrationX The x coordinate of the registration point.
65 * @param registrationY The y coordinate of the registration point.
66 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070067 public DragView(Launcher launcher, Bitmap bitmap, int registrationX, int registrationY,
Winson Chung72d59842012-02-22 13:51:36 -080068 int left, int top, int width, int height, final float initialScale) {
Adam Cohen8dfcba42011-07-07 16:38:18 -070069 super(launcher);
70 mDragLayer = launcher.getDragLayer();
Winson Chung043f2af2012-03-01 16:09:54 -080071 mInitialScale = initialScale;
Joe Onorato00acb122009-08-04 16:04:30 -040072
Patrick Dubroyde7658b2010-09-27 11:15:43 -070073 final Resources res = getResources();
Winson Chung7bd1bbb2012-02-13 18:29:29 -080074 final float offsetX = res.getDimensionPixelSize(R.dimen.dragViewOffsetX);
75 final float offsetY = res.getDimensionPixelSize(R.dimen.dragViewOffsetY);
76 final float scaleDps = res.getDimensionPixelSize(R.dimen.dragViewScale);
77 final float scale = (width + scaleDps) / width;
Patrick Dubroya669d792010-11-23 14:40:33 -080078
79 // Animate the view into the correct position
80 mAnim = ValueAnimator.ofFloat(0.0f, 1.0f);
Winson Chung7bd1bbb2012-02-13 18:29:29 -080081 mAnim.setDuration(150);
Patrick Dubroya669d792010-11-23 14:40:33 -080082 mAnim.addUpdateListener(new AnimatorUpdateListener() {
83 @Override
84 public void onAnimationUpdate(ValueAnimator animation) {
85 final float value = (Float) animation.getAnimatedValue();
86
87 final int deltaX = (int) ((value * offsetX) - mOffsetX);
88 final int deltaY = (int) ((value * offsetY) - mOffsetY);
89
90 mOffsetX += deltaX;
91 mOffsetY += deltaY;
Winson Chung72d59842012-02-22 13:51:36 -080092 setScaleX(initialScale + (value * (scale - initialScale)));
93 setScaleY(initialScale + (value * (scale - initialScale)));
Winson Chung867ca622012-02-21 15:48:35 -080094 if (sDragAlpha != 1f) {
95 setAlpha(sDragAlpha * value + (1f - value));
96 }
Patrick Dubroya669d792010-11-23 14:40:33 -080097
98 if (getParent() == null) {
99 animation.cancel();
100 } else {
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800101 setTranslationX(getTranslationX() + deltaX);
102 setTranslationY(getTranslationY() + deltaY);
Patrick Dubroya669d792010-11-23 14:40:33 -0800103 }
104 }
105 });
Joe Onorato00acb122009-08-04 16:04:30 -0400106
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800107 mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height);
Adam Cohene3e27a82011-04-15 12:07:39 -0700108 setDragRegion(new Rect(0, 0, width, height));
Joe Onorato00acb122009-08-04 16:04:30 -0400109
110 // The point in our scaled bitmap that the touch events are located
Patrick Dubroya669d792010-11-23 14:40:33 -0800111 mRegistrationX = registrationX;
112 mRegistrationY = registrationY;
Patrick Dubroy62bbb3c2011-01-17 15:29:27 -0800113
114 // Force a measure, because Workspace uses getMeasuredHeight() before the layout pass
115 int ms = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
116 measure(ms, ms);
Michael Jurka6cfafb92012-02-23 18:25:43 -0800117 mPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
Patrick Dubroy62bbb3c2011-01-17 15:29:27 -0800118 }
119
120 public float getOffsetY() {
121 return mOffsetY;
Joe Onorato00acb122009-08-04 16:04:30 -0400122 }
123
Michael Jurkaa63c4522010-08-19 13:52:27 -0700124 public int getDragRegionLeft() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700125 return mDragRegion.left;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700126 }
127
128 public int getDragRegionTop() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700129 return mDragRegion.top;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700130 }
131
132 public int getDragRegionWidth() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700133 return mDragRegion.width();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700134 }
135
136 public int getDragRegionHeight() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700137 return mDragRegion.height();
138 }
139
Winson Chungb8c69f32011-10-19 21:36:08 -0700140 public void setDragVisualizeOffset(Point p) {
141 mDragVisualizeOffset = p;
142 }
143
144 public Point getDragVisualizeOffset() {
145 return mDragVisualizeOffset;
146 }
147
Adam Cohene3e27a82011-04-15 12:07:39 -0700148 public void setDragRegion(Rect r) {
149 mDragRegion = r;
150 }
151
152 public Rect getDragRegion() {
153 return mDragRegion;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700154 }
155
Winson Chung043f2af2012-03-01 16:09:54 -0800156 public float getInitialScale() {
157 return mInitialScale;
158 }
159
160 public void updateInitialScaleToCurrentScale() {
161 mInitialScale = getScaleX();
162 }
163
Joe Onorato00acb122009-08-04 16:04:30 -0400164 @Override
165 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Joe Onoratoeebd9242009-11-04 13:48:32 -0500166 setMeasuredDimension(mBitmap.getWidth(), mBitmap.getHeight());
Joe Onorato00acb122009-08-04 16:04:30 -0400167 }
168
169 @Override
170 protected void onDraw(Canvas canvas) {
171 if (false) {
172 // for debugging
173 Paint p = new Paint();
174 p.setStyle(Paint.Style.FILL);
Winson Chungeecf02d2012-03-02 17:14:58 -0800175 p.setColor(0x66ffffff);
Joe Onorato00acb122009-08-04 16:04:30 -0400176 canvas.drawRect(0, 0, getWidth(), getHeight(), p);
177 }
Patrick Dubroya669d792010-11-23 14:40:33 -0800178
Adam Cohenfc53cd22011-07-20 15:45:11 -0700179 mHasDrawn = true;
Adam Cohened66b2b2012-01-23 17:28:51 -0800180 boolean crossFade = mCrossFadeProgress > 0 && mCrossFadeBitmap != null;
181 if (crossFade) {
182 int alpha = crossFade ? (int) (255 * (1 - mCrossFadeProgress)) : 255;
183 mPaint.setAlpha(alpha);
184 }
Joe Onorato00acb122009-08-04 16:04:30 -0400185 canvas.drawBitmap(mBitmap, 0.0f, 0.0f, mPaint);
Adam Cohened66b2b2012-01-23 17:28:51 -0800186 if (crossFade) {
187 mPaint.setAlpha((int) (255 * mCrossFadeProgress));
188 canvas.save();
189 float sX = (mBitmap.getWidth() * 1.0f) / mCrossFadeBitmap.getWidth();
190 float sY = (mBitmap.getHeight() * 1.0f) / mCrossFadeBitmap.getHeight();
191 canvas.scale(sX, sY);
192 canvas.drawBitmap(mCrossFadeBitmap, 0.0f, 0.0f, mPaint);
193 canvas.restore();
194 }
195 }
196
197 public void setCrossFadeBitmap(Bitmap crossFadeBitmap) {
198 mCrossFadeBitmap = crossFadeBitmap;
199 }
200
201 public void crossFade(int duration) {
202 ValueAnimator va = ValueAnimator.ofFloat(0f, 1f);
203 va.setDuration(duration);
204 va.setInterpolator(new DecelerateInterpolator(1.5f));
205 va.addUpdateListener(new AnimatorUpdateListener() {
206 @Override
207 public void onAnimationUpdate(ValueAnimator animation) {
208 mCrossFadeProgress = animation.getAnimatedFraction();
209 }
210 });
211 va.start();
Joe Onorato00acb122009-08-04 16:04:30 -0400212 }
213
Winson Chung61967cb2012-02-28 18:11:33 -0800214 public void setColor(int color) {
Michael Jurka6cfafb92012-02-23 18:25:43 -0800215 if (mPaint == null) {
216 mPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
217 }
Winson Chung61967cb2012-02-28 18:11:33 -0800218 if (color != 0) {
219 mPaint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP));
220 } else {
221 mPaint.setColorFilter(null);
222 }
Joe Onorato00acb122009-08-04 16:04:30 -0400223 invalidate();
224 }
225
Adam Cohenfc53cd22011-07-20 15:45:11 -0700226 public boolean hasDrawn() {
227 return mHasDrawn;
228 }
229
Adam Cohen3e8f8112011-07-02 18:03:00 -0700230 @Override
231 public void setAlpha(float alpha) {
232 super.setAlpha(alpha);
Adam Cohen3e8f8112011-07-02 18:03:00 -0700233 mPaint.setAlpha((int) (255 * alpha));
234 invalidate();
235 }
236
Joe Onorato00acb122009-08-04 16:04:30 -0400237 /**
238 * Create a window containing this view and show it.
239 *
240 * @param windowToken obtained from v.getWindowToken() from one of your views
Adam Cohen8dfcba42011-07-07 16:38:18 -0700241 * @param touchX the x coordinate the user touched in DragLayer coordinates
242 * @param touchY the y coordinate the user touched in DragLayer coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400243 */
Adam Cohen8dfcba42011-07-07 16:38:18 -0700244 public void show(int touchX, int touchY) {
245 mDragLayer.addView(this);
Winson Chung043f2af2012-03-01 16:09:54 -0800246
247 // Enable hw-layers on this view
248 setLayerType(View.LAYER_TYPE_HARDWARE, null);
249
250 // Start the pick-up animation
Adam Cohen8dfcba42011-07-07 16:38:18 -0700251 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(0, 0);
252 lp.width = mBitmap.getWidth();
253 lp.height = mBitmap.getHeight();
Adam Cohen8dfcba42011-07-07 16:38:18 -0700254 lp.customPosition = true;
255 setLayoutParams(lp);
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800256 setTranslationX(touchX - mRegistrationX);
257 setTranslationY(touchY - mRegistrationY);
Patrick Dubroya669d792010-11-23 14:40:33 -0800258 mAnim.start();
Joe Onorato00acb122009-08-04 16:04:30 -0400259 }
Adam Cohen716b51e2011-06-30 12:09:54 -0700260
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800261 public void cancelAnimation() {
262 if (mAnim != null && mAnim.isRunning()) {
263 mAnim.cancel();
264 }
265 }
266
267 public void resetLayoutParams() {
268 mOffsetX = mOffsetY = 0;
269 requestLayout();
270 }
271
Joe Onorato00acb122009-08-04 16:04:30 -0400272 /**
273 * Move the window containing this view.
274 *
Adam Cohen8dfcba42011-07-07 16:38:18 -0700275 * @param touchX the x coordinate the user touched in DragLayer coordinates
276 * @param touchY the y coordinate the user touched in DragLayer coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400277 */
278 void move(int touchX, int touchY) {
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800279 setTranslationX(touchX - mRegistrationX + (int) mOffsetX);
280 setTranslationY(touchY - mRegistrationY + (int) mOffsetY);
Joe Onorato00acb122009-08-04 16:04:30 -0400281 }
282
283 void remove() {
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800284 if (getParent() != null) {
Winson Chung043f2af2012-03-01 16:09:54 -0800285 // Disable hw-layers on this view
286 setLayerType(View.LAYER_TYPE_NONE, null);
287
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800288 mDragLayer.removeView(DragView.this);
289 }
Patrick Dubroy6f133422011-02-24 12:16:12 -0800290 }
Joe Onorato00acb122009-08-04 16:04:30 -0400291}
292