blob: 7128ded94c3a009a0a7b44cb47a8b150cb10f66d [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;
21import android.content.res.TypedArray;
22import android.graphics.Bitmap;
23import android.graphics.Canvas;
24import android.graphics.Matrix;
25import android.graphics.Paint;
26import android.graphics.PixelFormat;
27import android.graphics.Point;
28import android.os.IBinder;
29import android.util.AttributeSet;
30import android.util.Log;
31import android.view.Gravity;
32import android.view.View;
33import android.view.ViewGroup;
34import android.view.KeyEvent;
35import android.view.WindowManager;
36import android.view.WindowManagerImpl;
37
38public class DragView extends View implements TweenCallback {
39 // Number of pixels to add to the dragged item for scaling. Should be even for pixel alignment.
40 private static final int DRAG_SCALE = 24;
41
42 private Bitmap mBitmap;
43 private Paint mPaint;
44 private int mRegistrationX;
45 private int mRegistrationY;
46
47 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);
Joe Onorato00acb122009-08-04 16:04:30 -040079
80 // The point in our scaled bitmap that the touch events are located
81 mRegistrationX = registrationX + (DRAG_SCALE / 2);
82 mRegistrationY = registrationY + (DRAG_SCALE / 2);
83 }
84
85 @Override
86 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
87 int widthSize = resolveSize(mBitmap.getWidth(), widthMeasureSpec);
88 int heightSize = resolveSize(mBitmap.getHeight(), heightMeasureSpec);
89 setMeasuredDimension(widthSize, heightSize);
90 }
91
92 @Override
93 protected void onDraw(Canvas canvas) {
94 if (false) {
95 // for debugging
96 Paint p = new Paint();
97 p.setStyle(Paint.Style.FILL);
98 p.setColor(0xaaffffff);
99 canvas.drawRect(0, 0, getWidth(), getHeight(), p);
100 }
101 float scale = mAnimationScale;
102 if (scale < 0.999f) { // allow for some float error
103 float width = mBitmap.getWidth();
104 float offset = (width-(width*scale))/2;
105 canvas.translate(offset, offset);
106 canvas.scale(scale, scale);
107 }
108 canvas.drawBitmap(mBitmap, 0.0f, 0.0f, mPaint);
109 }
110
111 @Override
112 protected void onDetachedFromWindow() {
113 super.onDetachedFromWindow();
114 mBitmap.recycle();
115 }
116
117 public void onTweenValueChanged(float value, float oldValue) {
118 mAnimationScale = (1.0f+((mScale-1.0f)*value))/mScale;
119 invalidate();
120 }
121
122 public void onTweenStarted() {
123 }
124
125 public void onTweenFinished() {
126 }
127
128 public void setPaint(Paint paint) {
129 mPaint = paint;
130 invalidate();
131 }
132
133 /**
134 * Create a window containing this view and show it.
135 *
136 * @param windowToken obtained from v.getWindowToken() from one of your views
137 * @param touchX the x coordinate the user touched in screen coordinates
138 * @param touchY the y coordinate the user touched in screen coordinates
139 */
140 public void show(IBinder windowToken, int touchX, int touchY) {
141 WindowManager.LayoutParams lp;
142 int pixelFormat;
143
144 pixelFormat = PixelFormat.TRANSLUCENT;
145
146 lp = new WindowManager.LayoutParams(
147 ViewGroup.LayoutParams.WRAP_CONTENT,
148 ViewGroup.LayoutParams.WRAP_CONTENT,
149 touchX-mRegistrationX, touchY-mRegistrationY,
150 WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL,
151 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
152 | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
153 /*| WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM*/,
154 pixelFormat);
155// lp.token = mStatusBarView.getWindowToken();
156 lp.gravity = Gravity.LEFT | Gravity.TOP;
157 lp.token = windowToken;
158 lp.setTitle("DragView");
159 mLayoutParams = lp;
160
161 mWindowManager.addView(this, lp);
162
163 mAnimationScale = 1.0f/mScale;
164 mTween.start(true);
165 }
166
167 /**
168 * Move the window containing this view.
169 *
170 * @param touchX the x coordinate the user touched in screen coordinates
171 * @param touchY the y coordinate the user touched in screen coordinates
172 */
173 void move(int touchX, int touchY) {
174 WindowManager.LayoutParams lp = mLayoutParams;
175 lp.x = touchX - mRegistrationX;
176 lp.y = touchY - mRegistrationY;
177 mWindowManager.updateViewLayout(this, lp);
178 }
179
180 void remove() {
181 mWindowManager.removeView(this);
182 }
183}
184