本文实例为大家分享了android手势控制实现缩放、移动图片的方法,供大家参考,具体内容如下
新建一个触摸监听器类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
package com.liyong.btprinter; import android.graphics.matrix; import android.graphics.pointf; import android.util.floatmath; import android.view.motionevent; import android.view.view; import android.view.view.ontouchlistener; import android.widget.imageview; public class mulitpointtouchlistener implements ontouchlistener { private static final string tag = "touch" ; // these matrices will be used to move and zoom image matrix matrix = new matrix(); matrix savedmatrix = new matrix(); // we can be in one of these 3 states static final int none = 0 ; static final int drag = 1 ; static final int zoom = 2 ; int mode = none; // remember some things for zooming pointf start = new pointf(); pointf mid = new pointf(); float olddist = 1f; @override public boolean ontouch(view v, motionevent event) { imageview view = (imageview) v; // log.e("view_width", // view.getimagematrix()..tostring()+"*"+v.getwidth()); // dump touch event to log dumpevent(event); // handle touch events here... switch (event.getaction() & motionevent.action_mask) { case motionevent.action_down: matrix.set(view.getimagematrix()); savedmatrix.set(matrix); start.set(event.getx(), event.gety()); //log.d(tag, "mode=drag"); mode = drag; //log.d(tag, "mode=none"); break ; case motionevent.action_pointer_down: olddist = spacing(event); //log.d(tag, "olddist=" + olddist); if (olddist > 10f) { savedmatrix.set(matrix); midpoint(mid, event); mode = zoom; //log.d(tag, "mode=zoom"); } break ; case motionevent.action_up: case motionevent.action_pointer_up: mode = none; //log.e("view.getwidth", view.getwidth() + ""); //log.e("view.getheight", view.getheight() + ""); break ; case motionevent.action_move: if (mode == drag) { // ... matrix.set(savedmatrix); matrix.posttranslate(event.getx() - start.x, event.gety() - start.y); } else if (mode == zoom) { float newdist = spacing(event); //log.d(tag, "newdist=" + newdist); if (newdist > 10f) { matrix.set(savedmatrix); float scale = newdist / olddist; matrix.postscale(scale, scale, mid.x, mid.y); } } break ; } view.setimagematrix(matrix); return true ; // indicate event was handled } private void dumpevent(motionevent event) { string names[] = { "down" , "up" , "move" , "cancel" , "outside" , "pointer_down" , "pointer_up" , "7?" , "8?" , "9?" }; stringbuilder sb = new stringbuilder(); int action = event.getaction(); int actioncode = action & motionevent.action_mask; sb.append( "event action_" ).append(names[actioncode]); if (actioncode == motionevent.action_pointer_down || actioncode == motionevent.action_pointer_up) { sb.append( "(pid " ).append( action >> motionevent.action_pointer_id_shift); sb.append( ")" ); } sb.append( "[" ); for ( int i = 0 ; i < event.getpointercount(); i++) { sb.append( "#" ).append(i); sb.append( "(pid " ).append(event.getpointerid(i)); sb.append( ")=" ).append(( int ) event.getx(i)); sb.append( "," ).append(( int ) event.gety(i)); if (i + 1 < event.getpointercount()) sb.append( ";" ); } sb.append( "]" ); //log.d(tag, sb.tostring()); } private float spacing(motionevent event) { float x = event.getx( 0 ) - event.getx( 1 ); float y = event.gety( 0 ) - event.gety( 1 ); return floatmath.sqrt(x * x + y * y); } private void midpoint(pointf point, motionevent event) { float x = event.getx( 0 ) + event.getx( 1 ); float y = event.gety( 0 ) + event.gety( 1 ); point.set(x / 2 , y / 2 ); } } |
编写一个xml文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?xml version= "1.0" encoding= "utf-8" ?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "vertical" android:layout_height= "fill_parent" android:layout_width= "fill_parent" android:id= "@+id/linearlayout1" android:weightsum= "1" > <imageview android:layout_width= "fill_parent" android:id= "@+id/priviewimage" android:layout_height= "fill_parent" android:layout_gravity= "center" android:scaletype= "matrix" > </imageview> </linearlayout> |
注意其中的: android:scaletype="matrix"
1
2
3
|
priviewimage.setimagebitmap(activitypreviewimage.click_bitmap); priviewimage=(imageview)findviewbyid(r.id.priviewimage); priviewimage.setontouchlistener( new mulitpointtouchlistener()); |
以上就是android手势控制缩放移动图片的全部代码,希望对大家的学习有所帮助。