Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 18 | package com.android.launcher2; |
| 19 | |
| 20 | import android.content.Context; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 21 | import android.graphics.Bitmap; |
| 22 | import android.graphics.Canvas; |
| 23 | import android.graphics.Matrix; |
| 24 | import android.graphics.Paint; |
| 25 | import android.graphics.PixelFormat; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 26 | import android.os.IBinder; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 27 | import android.view.Gravity; |
| 28 | import android.view.View; |
| 29 | import android.view.ViewGroup; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 30 | import android.view.WindowManager; |
| 31 | import android.view.WindowManagerImpl; |
| 32 | |
| 33 | public 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 Onorato | e538b11 | 2010-02-01 19:10:25 -0500 | [diff] [blame] | 35 | private static final int DRAG_SCALE = 40; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 36 | |
| 37 | private Bitmap mBitmap; |
| 38 | private Paint mPaint; |
| 39 | private int mRegistrationX; |
| 40 | private int mRegistrationY; |
| 41 | |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 42 | private int mDragRegionLeft = 0; |
| 43 | private int mDragRegionTop = 0; |
| 44 | private int mDragRegionWidth; |
| 45 | private int mDragRegionHeight; |
| 46 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 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 Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 65 | public DragView(Context context, Bitmap bitmap, int registrationX, int registrationY, |
| 66 | int left, int top, int width, int height) { |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 67 | super(context); |
| 68 | |
| 69 | mWindowManager = WindowManagerImpl.getDefault(); |
| 70 | |
| 71 | mTween = new SymmetricalLinearTween(false, 110 /*ms duration*/, this); |
| 72 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 73 | Matrix scale = new Matrix(); |
| 74 | float scaleFactor = width; |
| 75 | scaleFactor = mScale = (scaleFactor + DRAG_SCALE) / scaleFactor; |
| 76 | scale.setScale(scaleFactor, scaleFactor); |
| 77 | |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 78 | mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height, scale, true); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame^] | 79 | setDragRegion(0, 0, width, height); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 80 | |
| 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 Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 86 | 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 Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame^] | 93 | 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 Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 109 | 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 Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 125 | @Override |
| 126 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Joe Onorato | eebd924 | 2009-11-04 13:48:32 -0500 | [diff] [blame] | 127 | setMeasuredDimension(mBitmap.getWidth(), mBitmap.getHeight()); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 128 | } |
| 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 | |