Added Android code
[wl-app.git] / Android / webViewMarker / src / main / java / com / blahti / drag / DragView.java
1 /*
2  * This is a modified version of a class from the Android Open Source Project. 
3  * The original copyright and license information follows.
4  * 
5  * Copyright (C) 2008 The Android Open Source Project
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 package com.blahti.drag;
21
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;
33
34 /**
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
37  * wants to move.
38  *
39  */
40
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;
50
51     public DragView(Context context) throws Exception {
52         super(context);
53         mRegistrationX = 0;
54         mRegistrationY = 0;
55         throw new Exception("DragView constructor permits only programatical calling");
56     }
57
58     /**
59      * Construct the drag view.
60      * <p>
61      * The registration point is the point inside our view that the touch events should
62      * be centered upon.
63      *
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.
68      */
69     public DragView(Context context, Bitmap bitmap, int registrationX, int registrationY, int left, int top, int width, int height) {
70         super(context);
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);
78     }
79
80     @Override
81     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
82         setMeasuredDimension(mBitmap.getWidth(), mBitmap.getHeight());
83     }
84     @Override
85     protected void onDraw(Canvas canvas) {
86         if (DEBUG) {
87             mDebugPaint.setStyle(Paint.Style.FILL);
88             mDebugPaint.setColor(0x88dd0011);
89             canvas.drawRect(0, 0, getWidth(), getHeight(), mDebugPaint);
90         }
91         canvas.drawBitmap(mBitmap, 0.0f, 0.0f, null);
92     }
93     @Override
94     protected void onDetachedFromWindow() {
95         super.onDetachedFromWindow();
96         mBitmap.recycle();
97     }
98
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
107         );
108         lp.gravity = Gravity.LEFT | Gravity.TOP;
109         lp.token = windowToken;
110         lp.setTitle("DragView");
111         mLayoutParams = lp;
112         mWindowManager.addView(this, lp);
113     }
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);
119     }
120     void remove() {
121         mWindowManager.removeView(this);
122     }
123 }