OSDN Git Service

Deprecate fill_parent and introduce match_parent.
[android-x86/packages-apps-Browser.git] / src / com / android / browser / MeshTracker.java
1
2
3 package com.android.browser;
4
5 import android.graphics.Bitmap;
6 import android.graphics.utils.BoundaryPatch;
7 import android.graphics.Canvas;
8 import android.graphics.Paint;
9 import android.webkit.WebView;
10
11 /*package*/ class MeshTracker extends WebView.DragTracker {
12
13     private static class Mesh {
14         private int mWhich;
15         private int mRows;
16         private int mCols;
17         private BoundaryPatch mPatch = new BoundaryPatch();
18         private float[] mCubics = new float[24];
19         private float[] mOrig = new float[24];
20         private float mStretchX, mStretchY;
21
22         Mesh(int which, int rows, int cols) {
23             mWhich = which;
24             mRows = rows;
25             mCols = cols;
26         }
27
28         private void rebuildPatch() {
29             mPatch.setCubicBoundary(mCubics, 0, mRows, mCols);
30         }
31
32         private void setSize(float w, float h) {
33             float[] pts = mCubics;
34             float x1 = w*0.3333f;
35             float y1 = h*0.3333f;
36             float x2 = w*0.6667f;
37             float y2 = h*0.6667f;
38             pts[0*2+0] = 0;  pts[0*2+1] = 0;
39             pts[1*2+0] = x1; pts[1*2+1] = 0;
40             pts[2*2+0] = x2; pts[2*2+1] = 0;
41
42             pts[3*2+0] = w; pts[3*2+1] = 0;
43             pts[4*2+0] = w; pts[4*2+1] = y1;
44             pts[5*2+0] = w; pts[5*2+1] = y2;
45
46             pts[6*2+0] = w; pts[6*2+1] = h;
47             pts[7*2+0] = x2; pts[7*2+1] = h;
48             pts[8*2+0] = x1; pts[8*2+1] = h;
49
50             pts[9*2+0] = 0;  pts[9*2+1] = h;
51             pts[10*2+0] = 0; pts[10*2+1] = y2;
52             pts[11*2+0] = 0; pts[11*2+1] = y1;
53
54             System.arraycopy(pts, 0, mOrig, 0, 24);
55
56             // recall our stretcher
57             setStretch(mStretchX, mStretchY);
58         }
59
60         public void setBitmap(Bitmap bm) {
61             mPatch.setTexture(bm);
62             setSize(bm.getWidth(), bm.getHeight());
63         }
64
65         // first experimental behavior
66         private void doit1(float dx, float dy) {
67             int index;
68
69             if (dx < 0) {
70                 index = 10;
71             } else {
72                 index = 4;
73             }
74             mCubics[index*2 + 0] = mOrig[index*2 + 0] + dx;
75             mCubics[index*2 + 2] = mOrig[index*2 + 2] + dx;
76
77             if (dy < 0) {
78                 index = 1;
79             } else {
80                 index = 7;
81             }
82             mCubics[index*2 + 1] = mOrig[index*2 + 1] + dy;
83             mCubics[index*2 + 3] = mOrig[index*2 + 3] + dy;
84         }
85
86         private void doit2(float dx, float dy) {
87             int index;
88
89             if (dx < 0) {
90                 index = 4;
91             } else {
92                 index = 10;
93             }
94             mCubics[index*2 + 0] = mOrig[index*2 + 0] + dx;
95             mCubics[index*2 + 2] = mOrig[index*2 + 2] + dx;
96
97             if (dy < 0) {
98                 index = 7;
99             } else {
100                 index = 1;
101             }
102             mCubics[index*2 + 1] = mOrig[index*2 + 1] + dy;
103             mCubics[index*2 + 3] = mOrig[index*2 + 3] + dy;
104         }
105
106         public void setStretch(float dx, float dy) {
107             mStretchX = dx;
108             mStretchY = dy;
109             switch (mWhich) {
110                 case 1:
111                     doit1(dx, dy);
112                     break;
113                 case 2:
114                     doit2(dx, dy);
115                     break;
116             }
117             rebuildPatch();
118         }
119
120         public void draw(Canvas canvas) {
121             mPatch.draw(canvas);
122         }
123     }
124
125     private Mesh mMesh;
126     private Bitmap mBitmap;
127     private int mWhich;
128
129     public MeshTracker(int which) {
130         mWhich = which;
131     }
132
133     @Override public void onStartDrag(float x, float y) {
134         mMesh = new Mesh(mWhich, 16, 16);
135     }
136
137     @Override public void onBitmapChange(Bitmap bm) {
138         mBitmap = bm;
139         mMesh.setBitmap(bm);
140     }
141
142     @Override public boolean onStretchChange(float sx, float sy) {
143         mMesh.setStretch(-sx, -sy);
144         return true;
145     }
146
147     @Override public void onStopDrag() {
148         mMesh = null;
149     }
150
151     @Override public void onDraw(Canvas canvas) {
152         canvas.drawColor(0xFF000000);
153         Paint paint = new Paint();
154         paint.setAlpha(0x80);
155         canvas.drawBitmap(mBitmap, 0, 0, paint);
156         mMesh.draw(canvas);
157     }
158 }
159