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 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 20 | import com.android.launcher.R; |
| 21 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 22 | import android.content.Context; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 23 | import android.content.res.Resources; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 24 | import android.graphics.Bitmap; |
| 25 | import android.graphics.Canvas; |
| 26 | import android.graphics.Matrix; |
| 27 | import android.graphics.Paint; |
| 28 | import android.graphics.PixelFormat; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 29 | import android.os.IBinder; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 30 | import android.view.Gravity; |
| 31 | import android.view.View; |
| 32 | import android.view.ViewGroup; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 33 | import android.view.WindowManager; |
| 34 | import android.view.WindowManagerImpl; |
| 35 | |
| 36 | public class DragView extends View implements TweenCallback { |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 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 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 69 | final Resources res = getResources(); |
| 70 | final int dragScale = res.getInteger(R.integer.config_dragViewExtraPixels); |
| 71 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 72 | mWindowManager = WindowManagerImpl.getDefault(); |
| 73 | |
| 74 | mTween = new SymmetricalLinearTween(false, 110 /*ms duration*/, this); |
| 75 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 76 | Matrix scale = new Matrix(); |
| 77 | float scaleFactor = width; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 78 | scaleFactor = mScale = (scaleFactor + dragScale) / scaleFactor; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 79 | scale.setScale(scaleFactor, scaleFactor); |
| 80 | |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 81 | mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height, scale, true); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 82 | setDragRegion(0, 0, width, height); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 83 | |
| 84 | // The point in our scaled bitmap that the touch events are located |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 85 | mRegistrationX = registrationX + res.getInteger(R.integer.config_dragViewOffsetX); |
| 86 | mRegistrationY = registrationY + res.getInteger(R.integer.config_dragViewOffsetY); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 87 | } |
| 88 | |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 89 | 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 Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 96 | 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 Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 112 | 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 Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 128 | @Override |
| 129 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Joe Onorato | eebd924 | 2009-11-04 13:48:32 -0500 | [diff] [blame] | 130 | setMeasuredDimension(mBitmap.getWidth(), mBitmap.getHeight()); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 131 | } |
| 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 | |