2 * This is a modified version of a class from the Android Open Source Project.
3 * The original copyright and license information follows.
5 * Copyright (C) 2008 The Android Open Source Project
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 package com.blahti.drag;
22 import android.content.Context;
23 import android.graphics.Bitmap;
24 import android.graphics.Canvas;
25 import android.graphics.Matrix;
26 import android.graphics.Paint;
27 import android.graphics.PixelFormat;
28 import android.os.IBinder;
29 import android.view.Gravity;
30 import android.view.View;
31 import android.view.ViewGroup;
32 import android.view.WindowManager;
35 * A DragView is a special view used by a DragController. During a drag operation, what is actually moving
36 * on the screen is a DragView. A DragView is constructed using a bitmap of the view the user really
41 public class DragView extends View {
42 private static final boolean DEBUG = false;
43 private static final int PADDING_TO_SCALE = 0;
44 private final int mRegistrationX;
45 private final int mRegistrationY;
46 private Bitmap mBitmap;
47 private Paint mDebugPaint = new Paint();
48 private WindowManager.LayoutParams mLayoutParams;
49 private WindowManager mWindowManager;
51 public DragView(Context context) throws Exception {
55 throw new Exception("DragView constructor permits only programatical calling");
59 * Construct the drag view.
61 * The registration point is the point inside our view that the touch events should
64 * @param context A context
65 * @param bitmap The view that we're dragging around. We scale it up when we draw it.
66 * @param registrationX The x coordinate of the registration point.
67 * @param registrationY The y coordinate of the registration point.
69 public DragView(Context context, Bitmap bitmap, int registrationX, int registrationY, int left, int top, int width, int height) {
71 mWindowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
72 mRegistrationX = registrationX + (PADDING_TO_SCALE / 2);
73 mRegistrationY = registrationY + (PADDING_TO_SCALE / 2);
74 final float scaleFactor = ((float)width + PADDING_TO_SCALE) / (float)width;
75 final Matrix scale = new Matrix();
76 scale.setScale(scaleFactor, scaleFactor);
77 mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height, scale, true);
81 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
82 setMeasuredDimension(mBitmap.getWidth(), mBitmap.getHeight());
85 protected void onDraw(Canvas canvas) {
87 mDebugPaint.setStyle(Paint.Style.FILL);
88 mDebugPaint.setColor(0x88dd0011);
89 canvas.drawRect(0, 0, getWidth(), getHeight(), mDebugPaint);
91 canvas.drawBitmap(mBitmap, 0.0f, 0.0f, null);
94 protected void onDetachedFromWindow() {
95 super.onDetachedFromWindow();
99 void show(IBinder windowToken, int touchX, int touchY) {
100 final WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
101 ViewGroup.LayoutParams.WRAP_CONTENT,
102 ViewGroup.LayoutParams.WRAP_CONTENT,
103 touchX - mRegistrationX, touchY - mRegistrationY,
104 WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL,
105 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
106 PixelFormat.TRANSLUCENT
108 lp.gravity = Gravity.LEFT | Gravity.TOP;
109 lp.token = windowToken;
110 lp.setTitle("DragView");
112 mWindowManager.addView(this, lp);
114 void move(int touchX, int touchY) {
115 WindowManager.LayoutParams lp = mLayoutParams;
116 lp.x = touchX - mRegistrationX;
117 lp.y = touchY - mRegistrationY;
118 mWindowManager.updateViewLayout(this, lp);
121 mWindowManager.removeView(this);