blob: b6645e102c01004f768258aadacf6cb5d4100991 [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) {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700170 @SuppressWarnings("all") // suppress dead code warning
171 final boolean debug = false;
172 if (debug) {
Joe Onorato00acb122009-08-04 16:04:30 -0400173 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