blob: e6b15b8ee2048b3862422d5ed38a54a65baab0f1 [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
Adam Cohen307fe232012-08-16 17:55:58 -070078 // Set the initial scale to avoid any jumps
79 setScaleX(initialScale);
80 setScaleY(initialScale);
81
Patrick Dubroya669d792010-11-23 14:40:33 -080082 // Animate the view into the correct position
Michael Jurka2ecf9952012-06-18 12:52:28 -070083 mAnim = LauncherAnimUtils.ofFloat(0.0f, 1.0f);
Winson Chung7bd1bbb2012-02-13 18:29:29 -080084 mAnim.setDuration(150);
Patrick Dubroya669d792010-11-23 14:40:33 -080085 mAnim.addUpdateListener(new AnimatorUpdateListener() {
86 @Override
87 public void onAnimationUpdate(ValueAnimator animation) {
88 final float value = (Float) animation.getAnimatedValue();
89
90 final int deltaX = (int) ((value * offsetX) - mOffsetX);
91 final int deltaY = (int) ((value * offsetY) - mOffsetY);
92
93 mOffsetX += deltaX;
94 mOffsetY += deltaY;
Winson Chung72d59842012-02-22 13:51:36 -080095 setScaleX(initialScale + (value * (scale - initialScale)));
96 setScaleY(initialScale + (value * (scale - initialScale)));
Winson Chung867ca622012-02-21 15:48:35 -080097 if (sDragAlpha != 1f) {
98 setAlpha(sDragAlpha * value + (1f - value));
99 }
Patrick Dubroya669d792010-11-23 14:40:33 -0800100
101 if (getParent() == null) {
102 animation.cancel();
103 } else {
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800104 setTranslationX(getTranslationX() + deltaX);
105 setTranslationY(getTranslationY() + deltaY);
Patrick Dubroya669d792010-11-23 14:40:33 -0800106 }
107 }
108 });
Joe Onorato00acb122009-08-04 16:04:30 -0400109
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800110 mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height);
Adam Cohene3e27a82011-04-15 12:07:39 -0700111 setDragRegion(new Rect(0, 0, width, height));
Joe Onorato00acb122009-08-04 16:04:30 -0400112
113 // The point in our scaled bitmap that the touch events are located
Patrick Dubroya669d792010-11-23 14:40:33 -0800114 mRegistrationX = registrationX;
115 mRegistrationY = registrationY;
Patrick Dubroy62bbb3c2011-01-17 15:29:27 -0800116
117 // Force a measure, because Workspace uses getMeasuredHeight() before the layout pass
118 int ms = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
119 measure(ms, ms);
Michael Jurka6cfafb92012-02-23 18:25:43 -0800120 mPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
Patrick Dubroy62bbb3c2011-01-17 15:29:27 -0800121 }
122
123 public float getOffsetY() {
124 return mOffsetY;
Joe Onorato00acb122009-08-04 16:04:30 -0400125 }
126
Michael Jurkaa63c4522010-08-19 13:52:27 -0700127 public int getDragRegionLeft() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700128 return mDragRegion.left;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700129 }
130
131 public int getDragRegionTop() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700132 return mDragRegion.top;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700133 }
134
135 public int getDragRegionWidth() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700136 return mDragRegion.width();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700137 }
138
139 public int getDragRegionHeight() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700140 return mDragRegion.height();
141 }
142
Winson Chungb8c69f32011-10-19 21:36:08 -0700143 public void setDragVisualizeOffset(Point p) {
144 mDragVisualizeOffset = p;
145 }
146
147 public Point getDragVisualizeOffset() {
148 return mDragVisualizeOffset;
149 }
150
Adam Cohene3e27a82011-04-15 12:07:39 -0700151 public void setDragRegion(Rect r) {
152 mDragRegion = r;
153 }
154
155 public Rect getDragRegion() {
156 return mDragRegion;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700157 }
158
Winson Chung043f2af2012-03-01 16:09:54 -0800159 public float getInitialScale() {
160 return mInitialScale;
161 }
162
163 public void updateInitialScaleToCurrentScale() {
164 mInitialScale = getScaleX();
165 }
166
Joe Onorato00acb122009-08-04 16:04:30 -0400167 @Override
168 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Joe Onoratoeebd9242009-11-04 13:48:32 -0500169 setMeasuredDimension(mBitmap.getWidth(), mBitmap.getHeight());
Joe Onorato00acb122009-08-04 16:04:30 -0400170 }
171
172 @Override
173 protected void onDraw(Canvas canvas) {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700174 @SuppressWarnings("all") // suppress dead code warning
175 final boolean debug = false;
176 if (debug) {
Joe Onorato00acb122009-08-04 16:04:30 -0400177 Paint p = new Paint();
178 p.setStyle(Paint.Style.FILL);
Winson Chungeecf02d2012-03-02 17:14:58 -0800179 p.setColor(0x66ffffff);
Joe Onorato00acb122009-08-04 16:04:30 -0400180 canvas.drawRect(0, 0, getWidth(), getHeight(), p);
181 }
Patrick Dubroya669d792010-11-23 14:40:33 -0800182
Adam Cohenfc53cd22011-07-20 15:45:11 -0700183 mHasDrawn = true;
Adam Cohened66b2b2012-01-23 17:28:51 -0800184 boolean crossFade = mCrossFadeProgress > 0 && mCrossFadeBitmap != null;
185 if (crossFade) {
186 int alpha = crossFade ? (int) (255 * (1 - mCrossFadeProgress)) : 255;
187 mPaint.setAlpha(alpha);
188 }
Joe Onorato00acb122009-08-04 16:04:30 -0400189 canvas.drawBitmap(mBitmap, 0.0f, 0.0f, mPaint);
Adam Cohened66b2b2012-01-23 17:28:51 -0800190 if (crossFade) {
191 mPaint.setAlpha((int) (255 * mCrossFadeProgress));
192 canvas.save();
193 float sX = (mBitmap.getWidth() * 1.0f) / mCrossFadeBitmap.getWidth();
194 float sY = (mBitmap.getHeight() * 1.0f) / mCrossFadeBitmap.getHeight();
195 canvas.scale(sX, sY);
196 canvas.drawBitmap(mCrossFadeBitmap, 0.0f, 0.0f, mPaint);
197 canvas.restore();
198 }
199 }
200
201 public void setCrossFadeBitmap(Bitmap crossFadeBitmap) {
202 mCrossFadeBitmap = crossFadeBitmap;
203 }
204
205 public void crossFade(int duration) {
Michael Jurka2ecf9952012-06-18 12:52:28 -0700206 ValueAnimator va = LauncherAnimUtils.ofFloat(0f, 1f);
Adam Cohened66b2b2012-01-23 17:28:51 -0800207 va.setDuration(duration);
208 va.setInterpolator(new DecelerateInterpolator(1.5f));
209 va.addUpdateListener(new AnimatorUpdateListener() {
210 @Override
211 public void onAnimationUpdate(ValueAnimator animation) {
212 mCrossFadeProgress = animation.getAnimatedFraction();
213 }
214 });
215 va.start();
Joe Onorato00acb122009-08-04 16:04:30 -0400216 }
217
Winson Chung61967cb2012-02-28 18:11:33 -0800218 public void setColor(int color) {
Michael Jurka6cfafb92012-02-23 18:25:43 -0800219 if (mPaint == null) {
220 mPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
221 }
Winson Chung61967cb2012-02-28 18:11:33 -0800222 if (color != 0) {
223 mPaint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP));
224 } else {
225 mPaint.setColorFilter(null);
226 }
Joe Onorato00acb122009-08-04 16:04:30 -0400227 invalidate();
228 }
229
Adam Cohenfc53cd22011-07-20 15:45:11 -0700230 public boolean hasDrawn() {
231 return mHasDrawn;
232 }
233
Adam Cohen3e8f8112011-07-02 18:03:00 -0700234 @Override
235 public void setAlpha(float alpha) {
236 super.setAlpha(alpha);
Adam Cohen3e8f8112011-07-02 18:03:00 -0700237 mPaint.setAlpha((int) (255 * alpha));
238 invalidate();
239 }
240
Joe Onorato00acb122009-08-04 16:04:30 -0400241 /**
242 * Create a window containing this view and show it.
243 *
244 * @param windowToken obtained from v.getWindowToken() from one of your views
Adam Cohen8dfcba42011-07-07 16:38:18 -0700245 * @param touchX the x coordinate the user touched in DragLayer coordinates
246 * @param touchY the y coordinate the user touched in DragLayer coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400247 */
Adam Cohen8dfcba42011-07-07 16:38:18 -0700248 public void show(int touchX, int touchY) {
249 mDragLayer.addView(this);
Winson Chung043f2af2012-03-01 16:09:54 -0800250
Winson Chung043f2af2012-03-01 16:09:54 -0800251 // Start the pick-up animation
Adam Cohen8dfcba42011-07-07 16:38:18 -0700252 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(0, 0);
253 lp.width = mBitmap.getWidth();
254 lp.height = mBitmap.getHeight();
Adam Cohen8dfcba42011-07-07 16:38:18 -0700255 lp.customPosition = true;
256 setLayoutParams(lp);
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800257 setTranslationX(touchX - mRegistrationX);
258 setTranslationY(touchY - mRegistrationY);
Michael Jurkaca993832012-06-29 15:17:04 -0700259 // Post the animation to skip other expensive work happening on the first frame
260 post(new Runnable() {
261 public void run() {
262 mAnim.start();
263 }
264 });
Joe Onorato00acb122009-08-04 16:04:30 -0400265 }
Adam Cohen716b51e2011-06-30 12:09:54 -0700266
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800267 public void cancelAnimation() {
268 if (mAnim != null && mAnim.isRunning()) {
269 mAnim.cancel();
270 }
271 }
272
273 public void resetLayoutParams() {
274 mOffsetX = mOffsetY = 0;
275 requestLayout();
276 }
277
Joe Onorato00acb122009-08-04 16:04:30 -0400278 /**
279 * Move the window containing this view.
280 *
Adam Cohen8dfcba42011-07-07 16:38:18 -0700281 * @param touchX the x coordinate the user touched in DragLayer coordinates
282 * @param touchY the y coordinate the user touched in DragLayer coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400283 */
284 void move(int touchX, int touchY) {
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800285 setTranslationX(touchX - mRegistrationX + (int) mOffsetX);
286 setTranslationY(touchY - mRegistrationY + (int) mOffsetY);
Joe Onorato00acb122009-08-04 16:04:30 -0400287 }
288
289 void remove() {
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800290 if (getParent() != null) {
291 mDragLayer.removeView(DragView.this);
292 }
Patrick Dubroy6f133422011-02-24 12:16:12 -0800293 }
Joe Onorato00acb122009-08-04 16:04:30 -0400294}
295