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 | |
| 42 | SymmetricalLinearTween mTween; |
| 43 | private float mScale; |
| 44 | private float mAnimationScale = 1.0f; |
| 45 | |
| 46 | private WindowManager.LayoutParams mLayoutParams; |
| 47 | private WindowManager mWindowManager; |
| 48 | |
| 49 | /** |
| 50 | * Construct the drag view. |
| 51 | * <p> |
| 52 | * The registration point is the point inside our view that the touch events should |
| 53 | * be centered upon. |
| 54 | * |
| 55 | * @param context A context |
| 56 | * @param bitmap The view that we're dragging around. We scale it up when we draw it. |
| 57 | * @param registrationX The x coordinate of the registration point. |
| 58 | * @param registrationY The y coordinate of the registration point. |
| 59 | */ |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 60 | public DragView(Context context, Bitmap bitmap, int registrationX, int registrationY, |
| 61 | int left, int top, int width, int height) { |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 62 | super(context); |
| 63 | |
| 64 | mWindowManager = WindowManagerImpl.getDefault(); |
| 65 | |
| 66 | mTween = new SymmetricalLinearTween(false, 110 /*ms duration*/, this); |
| 67 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 68 | Matrix scale = new Matrix(); |
| 69 | float scaleFactor = width; |
| 70 | scaleFactor = mScale = (scaleFactor + DRAG_SCALE) / scaleFactor; |
| 71 | scale.setScale(scaleFactor, scaleFactor); |
| 72 | |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 73 | mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height, scale, true); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 74 | |
| 75 | // The point in our scaled bitmap that the touch events are located |
| 76 | mRegistrationX = registrationX + (DRAG_SCALE / 2); |
| 77 | mRegistrationY = registrationY + (DRAG_SCALE / 2); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Joe Onorato | eebd924 | 2009-11-04 13:48:32 -0500 | [diff] [blame] | 82 | setMeasuredDimension(mBitmap.getWidth(), mBitmap.getHeight()); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @Override |
| 86 | protected void onDraw(Canvas canvas) { |
| 87 | if (false) { |
| 88 | // for debugging |
| 89 | Paint p = new Paint(); |
| 90 | p.setStyle(Paint.Style.FILL); |
| 91 | p.setColor(0xaaffffff); |
| 92 | canvas.drawRect(0, 0, getWidth(), getHeight(), p); |
| 93 | } |
| 94 | float scale = mAnimationScale; |
| 95 | if (scale < 0.999f) { // allow for some float error |
| 96 | float width = mBitmap.getWidth(); |
| 97 | float offset = (width-(width*scale))/2; |
| 98 | canvas.translate(offset, offset); |
| 99 | canvas.scale(scale, scale); |
| 100 | } |
| 101 | canvas.drawBitmap(mBitmap, 0.0f, 0.0f, mPaint); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | protected void onDetachedFromWindow() { |
| 106 | super.onDetachedFromWindow(); |
| 107 | mBitmap.recycle(); |
| 108 | } |
| 109 | |
| 110 | public void onTweenValueChanged(float value, float oldValue) { |
| 111 | mAnimationScale = (1.0f+((mScale-1.0f)*value))/mScale; |
| 112 | invalidate(); |
| 113 | } |
| 114 | |
| 115 | public void onTweenStarted() { |
| 116 | } |
| 117 | |
| 118 | public void onTweenFinished() { |
| 119 | } |
| 120 | |
| 121 | public void setPaint(Paint paint) { |
| 122 | mPaint = paint; |
| 123 | invalidate(); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Create a window containing this view and show it. |
| 128 | * |
| 129 | * @param windowToken obtained from v.getWindowToken() from one of your views |
| 130 | * @param touchX the x coordinate the user touched in screen coordinates |
| 131 | * @param touchY the y coordinate the user touched in screen coordinates |
| 132 | */ |
| 133 | public void show(IBinder windowToken, int touchX, int touchY) { |
| 134 | WindowManager.LayoutParams lp; |
| 135 | int pixelFormat; |
| 136 | |
| 137 | pixelFormat = PixelFormat.TRANSLUCENT; |
| 138 | |
| 139 | lp = new WindowManager.LayoutParams( |
| 140 | ViewGroup.LayoutParams.WRAP_CONTENT, |
| 141 | ViewGroup.LayoutParams.WRAP_CONTENT, |
| 142 | touchX-mRegistrationX, touchY-mRegistrationY, |
| 143 | WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL, |
| 144 | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
| 145 | | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS |
| 146 | /*| WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM*/, |
| 147 | pixelFormat); |
| 148 | // lp.token = mStatusBarView.getWindowToken(); |
| 149 | lp.gravity = Gravity.LEFT | Gravity.TOP; |
| 150 | lp.token = windowToken; |
| 151 | lp.setTitle("DragView"); |
| 152 | mLayoutParams = lp; |
| 153 | |
| 154 | mWindowManager.addView(this, lp); |
| 155 | |
| 156 | mAnimationScale = 1.0f/mScale; |
| 157 | mTween.start(true); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Move the window containing this view. |
| 162 | * |
| 163 | * @param touchX the x coordinate the user touched in screen coordinates |
| 164 | * @param touchY the y coordinate the user touched in screen coordinates |
| 165 | */ |
| 166 | void move(int touchX, int touchY) { |
| 167 | WindowManager.LayoutParams lp = mLayoutParams; |
| 168 | lp.x = touchX - mRegistrationX; |
| 169 | lp.y = touchY - mRegistrationY; |
| 170 | mWindowManager.updateViewLayout(this, lp); |
| 171 | } |
| 172 | |
| 173 | void remove() { |
| 174 | mWindowManager.removeView(this); |
| 175 | } |
| 176 | } |
| 177 | |