blob: f6c16b36e7d50e8d63300b4acc28f4e5b6e602d8 [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;
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();
Joe Onorato00acb122009-08-04 16:04:30 -040070
Patrick Dubroyde7658b2010-09-27 11:15:43 -070071 final Resources res = getResources();
Winson Chung7bd1bbb2012-02-13 18:29:29 -080072 final float offsetX = res.getDimensionPixelSize(R.dimen.dragViewOffsetX);
73 final float offsetY = res.getDimensionPixelSize(R.dimen.dragViewOffsetY);
74 final float scaleDps = res.getDimensionPixelSize(R.dimen.dragViewScale);
75 final float scale = (width + scaleDps) / width;
Patrick Dubroya669d792010-11-23 14:40:33 -080076
77 // Animate the view into the correct position
78 mAnim = ValueAnimator.ofFloat(0.0f, 1.0f);
Winson Chung7bd1bbb2012-02-13 18:29:29 -080079 mAnim.setDuration(150);
Patrick Dubroya669d792010-11-23 14:40:33 -080080 mAnim.addUpdateListener(new AnimatorUpdateListener() {
81 @Override
82 public void onAnimationUpdate(ValueAnimator animation) {
83 final float value = (Float) animation.getAnimatedValue();
84
85 final int deltaX = (int) ((value * offsetX) - mOffsetX);
86 final int deltaY = (int) ((value * offsetY) - mOffsetY);
87
88 mOffsetX += deltaX;
89 mOffsetY += deltaY;
Winson Chung72d59842012-02-22 13:51:36 -080090 setScaleX(initialScale + (value * (scale - initialScale)));
91 setScaleY(initialScale + (value * (scale - initialScale)));
Winson Chung867ca622012-02-21 15:48:35 -080092 if (sDragAlpha != 1f) {
93 setAlpha(sDragAlpha * value + (1f - value));
94 }
Patrick Dubroya669d792010-11-23 14:40:33 -080095
96 if (getParent() == null) {
97 animation.cancel();
98 } else {
Winson Chung7bd1bbb2012-02-13 18:29:29 -080099 setTranslationX(getTranslationX() + deltaX);
100 setTranslationY(getTranslationY() + deltaY);
Patrick Dubroya669d792010-11-23 14:40:33 -0800101 }
102 }
103 });
Joe Onorato00acb122009-08-04 16:04:30 -0400104
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800105 mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height);
Adam Cohene3e27a82011-04-15 12:07:39 -0700106 setDragRegion(new Rect(0, 0, width, height));
Joe Onorato00acb122009-08-04 16:04:30 -0400107
108 // The point in our scaled bitmap that the touch events are located
Patrick Dubroya669d792010-11-23 14:40:33 -0800109 mRegistrationX = registrationX;
110 mRegistrationY = registrationY;
Patrick Dubroy62bbb3c2011-01-17 15:29:27 -0800111
112 // Force a measure, because Workspace uses getMeasuredHeight() before the layout pass
113 int ms = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
114 measure(ms, ms);
Michael Jurka6cfafb92012-02-23 18:25:43 -0800115 mPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
Patrick Dubroy62bbb3c2011-01-17 15:29:27 -0800116 }
117
118 public float getOffsetY() {
119 return mOffsetY;
Joe Onorato00acb122009-08-04 16:04:30 -0400120 }
121
Michael Jurkaa63c4522010-08-19 13:52:27 -0700122 public int getDragRegionLeft() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700123 return mDragRegion.left;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700124 }
125
126 public int getDragRegionTop() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700127 return mDragRegion.top;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700128 }
129
130 public int getDragRegionWidth() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700131 return mDragRegion.width();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700132 }
133
134 public int getDragRegionHeight() {
Adam Cohene3e27a82011-04-15 12:07:39 -0700135 return mDragRegion.height();
136 }
137
Winson Chungb8c69f32011-10-19 21:36:08 -0700138 public void setDragVisualizeOffset(Point p) {
139 mDragVisualizeOffset = p;
140 }
141
142 public Point getDragVisualizeOffset() {
143 return mDragVisualizeOffset;
144 }
145
Adam Cohene3e27a82011-04-15 12:07:39 -0700146 public void setDragRegion(Rect r) {
147 mDragRegion = r;
148 }
149
150 public Rect getDragRegion() {
151 return mDragRegion;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700152 }
153
Joe Onorato00acb122009-08-04 16:04:30 -0400154 @Override
155 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Joe Onoratoeebd9242009-11-04 13:48:32 -0500156 setMeasuredDimension(mBitmap.getWidth(), mBitmap.getHeight());
Joe Onorato00acb122009-08-04 16:04:30 -0400157 }
158
159 @Override
160 protected void onDraw(Canvas canvas) {
161 if (false) {
162 // for debugging
163 Paint p = new Paint();
164 p.setStyle(Paint.Style.FILL);
165 p.setColor(0xaaffffff);
166 canvas.drawRect(0, 0, getWidth(), getHeight(), p);
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
Winson Chung61967cb2012-02-28 18:11:33 -0800204 public void setColor(int color) {
Michael Jurka6cfafb92012-02-23 18:25:43 -0800205 if (mPaint == null) {
206 mPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
207 }
Winson Chung61967cb2012-02-28 18:11:33 -0800208 if (color != 0) {
209 mPaint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP));
210 } else {
211 mPaint.setColorFilter(null);
212 }
Joe Onorato00acb122009-08-04 16:04:30 -0400213 invalidate();
214 }
215
Adam Cohenfc53cd22011-07-20 15:45:11 -0700216 public boolean hasDrawn() {
217 return mHasDrawn;
218 }
219
Adam Cohen3e8f8112011-07-02 18:03:00 -0700220 @Override
221 public void setAlpha(float alpha) {
222 super.setAlpha(alpha);
Adam Cohen3e8f8112011-07-02 18:03:00 -0700223 mPaint.setAlpha((int) (255 * alpha));
224 invalidate();
225 }
226
Joe Onorato00acb122009-08-04 16:04:30 -0400227 /**
228 * Create a window containing this view and show it.
229 *
230 * @param windowToken obtained from v.getWindowToken() from one of your views
Adam Cohen8dfcba42011-07-07 16:38:18 -0700231 * @param touchX the x coordinate the user touched in DragLayer coordinates
232 * @param touchY the y coordinate the user touched in DragLayer coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400233 */
Adam Cohen8dfcba42011-07-07 16:38:18 -0700234 public void show(int touchX, int touchY) {
235 mDragLayer.addView(this);
236 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(0, 0);
237 lp.width = mBitmap.getWidth();
238 lp.height = mBitmap.getHeight();
Adam Cohen8dfcba42011-07-07 16:38:18 -0700239 lp.customPosition = true;
240 setLayoutParams(lp);
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800241 setTranslationX(touchX - mRegistrationX);
242 setTranslationY(touchY - mRegistrationY);
Patrick Dubroya669d792010-11-23 14:40:33 -0800243 mAnim.start();
Joe Onorato00acb122009-08-04 16:04:30 -0400244 }
Adam Cohen716b51e2011-06-30 12:09:54 -0700245
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800246 public void cancelAnimation() {
247 if (mAnim != null && mAnim.isRunning()) {
248 mAnim.cancel();
249 }
250 }
251
252 public void resetLayoutParams() {
253 mOffsetX = mOffsetY = 0;
254 requestLayout();
255 }
256
Joe Onorato00acb122009-08-04 16:04:30 -0400257 /**
258 * Move the window containing this view.
259 *
Adam Cohen8dfcba42011-07-07 16:38:18 -0700260 * @param touchX the x coordinate the user touched in DragLayer coordinates
261 * @param touchY the y coordinate the user touched in DragLayer coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400262 */
263 void move(int touchX, int touchY) {
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800264 setTranslationX(touchX - mRegistrationX + (int) mOffsetX);
265 setTranslationY(touchY - mRegistrationY + (int) mOffsetY);
Joe Onorato00acb122009-08-04 16:04:30 -0400266 }
267
268 void remove() {
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800269 if (getParent() != null) {
270 mDragLayer.removeView(DragView.this);
271 }
Patrick Dubroy6f133422011-02-24 12:16:12 -0800272 }
Joe Onorato00acb122009-08-04 16:04:30 -0400273}
274