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
| private final static String TAG = "TAG";
private boolean isZoomed = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView imageView = findViewById(R.id.imageView);
GestureDetector.SimpleOnGestureListener simpleOnGestureListener = new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDown(MotionEvent e) {
//不返回true的话不会处理其他事件
return true;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
//没有放大,则放大图片
if (!isZoomed) {
Log.d(TAG, "onDoubleTap: ");
//放大到原来的1.5倍
imageView.getImageMatrix()
.postScale(5f, 5f, e.getX(), e.getY());
imageView.invalidate();
isZoomed = true;
return false;
}
return true;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
Log.d(TAG, "onSingleTapConfirmed: ");
if (isZoomed) {
//进行过放大之后,单机还原图片
imageView.setImageMatrix(new Matrix());
imageView.invalidate();
isZoomed = false;
}
return true; }
//
@Override
public boolean onSingleTapUp(MotionEvent e) {
Log.d(TAG, "onSingleTapUp: ");
return true;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
Log.d(TAG, "onScroll: " + distanceX + " " + distanceY);
if (isZoomed) {
//根据滑动的距离给matrix设置矩阵平移变换
imageView.getImageMatrix()
.postTranslate(-distanceX, -distanceY);
imageView.invalidate();
}
return true;
}
};
//设置手势监听器
final GestureDetector gestureDetector = new GestureDetector(this, simpleOnGestureListener);
imageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
});
}
|