blob: ca0e7b4436bcae1fe99fb9bae48196ba4ff31631 [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 Dubroyde7658b2010-09-27 11:15:43 -070020import com.android.launcher.R;
21
Joe Onorato00acb122009-08-04 16:04:30 -040022import android.content.Context;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070023import android.content.res.Resources;
Joe Onorato00acb122009-08-04 16:04:30 -040024import android.graphics.Bitmap;
25import android.graphics.Canvas;
26import android.graphics.Matrix;
27import android.graphics.Paint;
28import android.graphics.PixelFormat;
Joe Onorato00acb122009-08-04 16:04:30 -040029import android.os.IBinder;
Joe Onorato00acb122009-08-04 16:04:30 -040030import android.view.Gravity;
31import android.view.View;
32import android.view.ViewGroup;
Joe Onorato00acb122009-08-04 16:04:30 -040033import android.view.WindowManager;
34import android.view.WindowManagerImpl;
35
36public class DragView extends View implements TweenCallback {
Joe Onorato00acb122009-08-04 16:04:30 -040037 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
Patrick Dubroyde7658b2010-09-27 11:15:43 -070069 final Resources res = getResources();
70 final int dragScale = res.getInteger(R.integer.config_dragViewExtraPixels);
71
Joe Onorato00acb122009-08-04 16:04:30 -040072 mWindowManager = WindowManagerImpl.getDefault();
73
74 mTween = new SymmetricalLinearTween(false, 110 /*ms duration*/, this);
75
Joe Onorato00acb122009-08-04 16:04:30 -040076 Matrix scale = new Matrix();
77 float scaleFactor = width;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070078 scaleFactor = mScale = (scaleFactor + dragScale) / scaleFactor;
Joe Onorato00acb122009-08-04 16:04:30 -040079 scale.setScale(scaleFactor, scaleFactor);
80
Joe Onorato5162ea92009-09-03 09:39:42 -070081 mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height, scale, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -070082 setDragRegion(0, 0, width, height);
Joe Onorato00acb122009-08-04 16:04:30 -040083
84 // The point in our scaled bitmap that the touch events are located
Patrick Dubroyde7658b2010-09-27 11:15:43 -070085 mRegistrationX = registrationX + res.getInteger(R.integer.config_dragViewOffsetX);
86 mRegistrationY = registrationY + res.getInteger(R.integer.config_dragViewOffsetY);
Joe Onorato00acb122009-08-04 16:04:30 -040087 }
88
Michael Jurkaa63c4522010-08-19 13:52:27 -070089 public void setDragRegion(int left, int top, int width, int height) {
90 mDragRegionLeft = left;
91 mDragRegionTop = top;
92 mDragRegionWidth = width;
93 mDragRegionHeight = height;
94 }
95
Michael Jurka0280c3b2010-09-17 15:00:07 -070096 public int getScaledDragRegionXOffset() {
97 return -(int)((mScale - 1.0f) * mDragRegionWidth / 2);
98 }
99
100 public int getScaledDragRegionWidth() {
101 return (int)(mScale * mDragRegionWidth);
102 }
103
104 public int getScaledDragRegionYOffset() {
105 return -(int)((mScale - 1.0f) * mDragRegionHeight / 2);
106 }
107
108 public int getScaledDragRegionHeight() {
109 return (int)(mScale * mDragRegionWidth);
110 }
111
Michael Jurkaa63c4522010-08-19 13:52:27 -0700112 public int getDragRegionLeft() {
113 return mDragRegionLeft;
114 }
115
116 public int getDragRegionTop() {
117 return mDragRegionTop;
118 }
119
120 public int getDragRegionWidth() {
121 return mDragRegionWidth;
122 }
123
124 public int getDragRegionHeight() {
125 return mDragRegionHeight;
126 }
127
Joe Onorato00acb122009-08-04 16:04:30 -0400128 @Override
129 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Joe Onoratoeebd9242009-11-04 13:48:32 -0500130 setMeasuredDimension(mBitmap.getWidth(), mBitmap.getHeight());
Joe Onorato00acb122009-08-04 16:04:30 -0400131 }
132
133 @Override
134 protected void onDraw(Canvas canvas) {
135 if (false) {
136 // for debugging
137 Paint p = new Paint();
138 p.setStyle(Paint.Style.FILL);
139 p.setColor(0xaaffffff);
140 canvas.drawRect(0, 0, getWidth(), getHeight(), p);
141 }
142 float scale = mAnimationScale;
143 if (scale < 0.999f) { // allow for some float error
144 float width = mBitmap.getWidth();
145 float offset = (width-(width*scale))/2;
146 canvas.translate(offset, offset);
147 canvas.scale(scale, scale);
148 }
149 canvas.drawBitmap(mBitmap, 0.0f, 0.0f, mPaint);
150 }
151
152 @Override
153 protected void onDetachedFromWindow() {
154 super.onDetachedFromWindow();
155 mBitmap.recycle();
156 }
157
158 public void onTweenValueChanged(float value, float oldValue) {
159 mAnimationScale = (1.0f+((mScale-1.0f)*value))/mScale;
160 invalidate();
161 }
162
163 public void onTweenStarted() {
164 }
165
166 public void onTweenFinished() {
167 }
168
169 public void setPaint(Paint paint) {
170 mPaint = paint;
171 invalidate();
172 }
173
174 /**
175 * Create a window containing this view and show it.
176 *
177 * @param windowToken obtained from v.getWindowToken() from one of your views
178 * @param touchX the x coordinate the user touched in screen coordinates
179 * @param touchY the y coordinate the user touched in screen coordinates
180 */
181 public void show(IBinder windowToken, int touchX, int touchY) {
182 WindowManager.LayoutParams lp;
183 int pixelFormat;
184
185 pixelFormat = PixelFormat.TRANSLUCENT;
186
187 lp = new WindowManager.LayoutParams(
188 ViewGroup.LayoutParams.WRAP_CONTENT,
189 ViewGroup.LayoutParams.WRAP_CONTENT,
190 touchX-mRegistrationX, touchY-mRegistrationY,
191 WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL,
192 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
193 | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
194 /*| WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM*/,
195 pixelFormat);
196// lp.token = mStatusBarView.getWindowToken();
197 lp.gravity = Gravity.LEFT | Gravity.TOP;
198 lp.token = windowToken;
199 lp.setTitle("DragView");
200 mLayoutParams = lp;
201
202 mWindowManager.addView(this, lp);
203
204 mAnimationScale = 1.0f/mScale;
205 mTween.start(true);
206 }
207
208 /**
209 * Move the window containing this view.
210 *
211 * @param touchX the x coordinate the user touched in screen coordinates
212 * @param touchY the y coordinate the user touched in screen coordinates
213 */
214 void move(int touchX, int touchY) {
215 WindowManager.LayoutParams lp = mLayoutParams;
216 lp.x = touchX - mRegistrationX;
217 lp.y = touchY - mRegistrationY;
218 mWindowManager.updateViewLayout(this, lp);
219 }
220
221 void remove() {
222 mWindowManager.removeView(this);
223 }
224}
225