blob: d14f5f756626cd1543ca656673ea4af7fa615914 [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
20import android.content.Context;
Joe Onorato00acb122009-08-04 16:04:30 -040021import android.graphics.Bitmap;
22import android.graphics.Canvas;
23import android.graphics.Matrix;
24import android.graphics.Paint;
25import android.graphics.PixelFormat;
Joe Onorato00acb122009-08-04 16:04:30 -040026import android.os.IBinder;
Joe Onorato00acb122009-08-04 16:04:30 -040027import android.view.Gravity;
28import android.view.View;
29import android.view.ViewGroup;
Joe Onorato00acb122009-08-04 16:04:30 -040030import android.view.WindowManager;
31import android.view.WindowManagerImpl;
32
33public class DragView extends View implements TweenCallback {
34 // Number of pixels to add to the dragged item for scaling. Should be even for pixel alignment.
Joe Onoratoe538b112010-02-01 19:10:25 -050035 private static final int DRAG_SCALE = 40;
Joe Onorato00acb122009-08-04 16:04:30 -040036
37 private Bitmap mBitmap;
38 private Paint mPaint;
39 private int mRegistrationX;
40 private int mRegistrationY;
41
Michael Jurkaa63c4522010-08-19 13:52:27 -070042 private int mDragRegionLeft = 0;
43 private int mDragRegionTop = 0;
44 private int mDragRegionWidth;
45 private int mDragRegionHeight;
46
Joe Onorato00acb122009-08-04 16:04:30 -040047 SymmetricalLinearTween mTween;
48 private float mScale;
49 private float mAnimationScale = 1.0f;
50
51 private WindowManager.LayoutParams mLayoutParams;
52 private WindowManager mWindowManager;
53
54 /**
55 * Construct the drag view.
56 * <p>
57 * The registration point is the point inside our view that the touch events should
58 * be centered upon.
59 *
60 * @param context A context
61 * @param bitmap The view that we're dragging around. We scale it up when we draw it.
62 * @param registrationX The x coordinate of the registration point.
63 * @param registrationY The y coordinate of the registration point.
64 */
Joe Onorato5162ea92009-09-03 09:39:42 -070065 public DragView(Context context, Bitmap bitmap, int registrationX, int registrationY,
66 int left, int top, int width, int height) {
Joe Onorato00acb122009-08-04 16:04:30 -040067 super(context);
68
69 mWindowManager = WindowManagerImpl.getDefault();
70
71 mTween = new SymmetricalLinearTween(false, 110 /*ms duration*/, this);
72
Joe Onorato00acb122009-08-04 16:04:30 -040073 Matrix scale = new Matrix();
74 float scaleFactor = width;
75 scaleFactor = mScale = (scaleFactor + DRAG_SCALE) / scaleFactor;
76 scale.setScale(scaleFactor, scaleFactor);
77
Joe Onorato5162ea92009-09-03 09:39:42 -070078 mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height, scale, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -070079 setDragRegion(0, 0, width, height);
Joe Onorato00acb122009-08-04 16:04:30 -040080
81 // The point in our scaled bitmap that the touch events are located
82 mRegistrationX = registrationX + (DRAG_SCALE / 2);
83 mRegistrationY = registrationY + (DRAG_SCALE / 2);
84 }
85
Michael Jurkaa63c4522010-08-19 13:52:27 -070086 public void setDragRegion(int left, int top, int width, int height) {
87 mDragRegionLeft = left;
88 mDragRegionTop = top;
89 mDragRegionWidth = width;
90 mDragRegionHeight = height;
91 }
92
Michael Jurka0280c3b2010-09-17 15:00:07 -070093 public int getScaledDragRegionXOffset() {
94 return -(int)((mScale - 1.0f) * mDragRegionWidth / 2);
95 }
96
97 public int getScaledDragRegionWidth() {
98 return (int)(mScale * mDragRegionWidth);
99 }
100
101 public int getScaledDragRegionYOffset() {
102 return -(int)((mScale - 1.0f) * mDragRegionHeight / 2);
103 }
104
105 public int getScaledDragRegionHeight() {
106 return (int)(mScale * mDragRegionWidth);
107 }
108
Michael Jurkaa63c4522010-08-19 13:52:27 -0700109 public int getDragRegionLeft() {
110 return mDragRegionLeft;
111 }
112
113 public int getDragRegionTop() {
114 return mDragRegionTop;
115 }
116
117 public int getDragRegionWidth() {
118 return mDragRegionWidth;
119 }
120
121 public int getDragRegionHeight() {
122 return mDragRegionHeight;
123 }
124
Joe Onorato00acb122009-08-04 16:04:30 -0400125 @Override
126 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Joe Onoratoeebd9242009-11-04 13:48:32 -0500127 setMeasuredDimension(mBitmap.getWidth(), mBitmap.getHeight());
Joe Onorato00acb122009-08-04 16:04:30 -0400128 }
129
130 @Override
131 protected void onDraw(Canvas canvas) {
132 if (false) {
133 // for debugging
134 Paint p = new Paint();
135 p.setStyle(Paint.Style.FILL);
136 p.setColor(0xaaffffff);
137 canvas.drawRect(0, 0, getWidth(), getHeight(), p);
138 }
139 float scale = mAnimationScale;
140 if (scale < 0.999f) { // allow for some float error
141 float width = mBitmap.getWidth();
142 float offset = (width-(width*scale))/2;
143 canvas.translate(offset, offset);
144 canvas.scale(scale, scale);
145 }
146 canvas.drawBitmap(mBitmap, 0.0f, 0.0f, mPaint);
147 }
148
149 @Override
150 protected void onDetachedFromWindow() {
151 super.onDetachedFromWindow();
152 mBitmap.recycle();
153 }
154
155 public void onTweenValueChanged(float value, float oldValue) {
156 mAnimationScale = (1.0f+((mScale-1.0f)*value))/mScale;
157 invalidate();
158 }
159
160 public void onTweenStarted() {
161 }
162
163 public void onTweenFinished() {
164 }
165
166 public void setPaint(Paint paint) {
167 mPaint = paint;
168 invalidate();
169 }
170
171 /**
172 * Create a window containing this view and show it.
173 *
174 * @param windowToken obtained from v.getWindowToken() from one of your views
175 * @param touchX the x coordinate the user touched in screen coordinates
176 * @param touchY the y coordinate the user touched in screen coordinates
177 */
178 public void show(IBinder windowToken, int touchX, int touchY) {
179 WindowManager.LayoutParams lp;
180 int pixelFormat;
181
182 pixelFormat = PixelFormat.TRANSLUCENT;
183
184 lp = new WindowManager.LayoutParams(
185 ViewGroup.LayoutParams.WRAP_CONTENT,
186 ViewGroup.LayoutParams.WRAP_CONTENT,
187 touchX-mRegistrationX, touchY-mRegistrationY,
188 WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL,
189 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
190 | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
191 /*| WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM*/,
192 pixelFormat);
193// lp.token = mStatusBarView.getWindowToken();
194 lp.gravity = Gravity.LEFT | Gravity.TOP;
195 lp.token = windowToken;
196 lp.setTitle("DragView");
197 mLayoutParams = lp;
198
199 mWindowManager.addView(this, lp);
200
201 mAnimationScale = 1.0f/mScale;
202 mTween.start(true);
203 }
204
205 /**
206 * Move the window containing this view.
207 *
208 * @param touchX the x coordinate the user touched in screen coordinates
209 * @param touchY the y coordinate the user touched in screen coordinates
210 */
211 void move(int touchX, int touchY) {
212 WindowManager.LayoutParams lp = mLayoutParams;
213 lp.x = touchX - mRegistrationX;
214 lp.y = touchY - mRegistrationY;
215 mWindowManager.updateViewLayout(this, lp);
216 }
217
218 void remove() {
219 mWindowManager.removeView(this);
220 }
221}
222