1 package com.example.qjm3662.android_touch_test;
2
3 import android.os.Bundle;
4 import android.support.design.widget.FloatingActionButton;
5 import android.support.design.widget.Snackbar;
6 import android.support.v7.app.AppCompatActivity;
7 import android.support.v7.widget.Toolbar;
8 import android.util.Log;
9 import android.view.MotionEvent;
10 import android.view.View;
11 import android.view.Menu;
12 import android.view.MenuItem;
13 import android.widget.ImageView;
14 import android.widget.LinearLayout;
15
16 public class MainActivity extends AppCompatActivity {
17
18 private String TAG = "MainActivity";
19 private LinearLayout root;
20 private ImageView iv;
21 @Override
22 protected void onCreate(Bundle savedInstanceState) {
23 super.onCreate(savedInstanceState);
24 setContentView(R.layout.activity_main);
25
26 root = (LinearLayout) findViewById(R.id.container);
27 iv = (ImageView) findViewById(R.id.iv);
28
29 root.setOnTouchListener(new View.OnTouchListener() {
30
31 float offsetX;
32 float offsetY;
33 float curruntDistance; //当前距离
34 float lastDistance = -1; //前一次的距离
35 @Override
36 public boolean onTouch(View v, MotionEvent event) {
37 switch(event.getAction()){
38 case MotionEvent.ACTION_DOWN:
39 Log.e(TAG,"down");
40 break;
41 case MotionEvent.ACTION_UP:
42 Log.e(TAG,"up");
43 break;
44 case MotionEvent.ACTION_MOVE:
45 if(event.getPointerCount() > 1){
46
47 //两个触摸点之间的距离
48 offsetX = event.getX(0) - event.getX(1);
49 offsetY = event.getY(0) - event.getY(1);
50
51 //当前距离
52 curruntDistance = (float) Math.sqrt(offsetX * offsetX + offsetY * offsetY);
53
54 //第一次给lastDistance赋值
55 if(lastDistance < 0){
56 lastDistance = curruntDistance;
57 }else{
58 //获取ImageView的layoutParms
59 LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) iv.getLayoutParams();
60
61 //与上一次相比距离增大(5px是为了考虑操作误差)
62 if(curruntDistance - lastDistance > 5){
63
64 //放大图片操作
65 layoutParams.width = (int) (1.1f*iv.getWidth());
66 layoutParams.height = (int) (1.1f*iv.getHeight());
67
68 //操作完务必给lastDistance重新赋值
69 lastDistance = curruntDistance;
70 iv.setLayoutParams(layoutParams);
71 Log.e(TAG,"放大");
72 }
73 //与上一次相比距离减小(5px是为了考虑操作误差)
74 else if(lastDistance - curruntDistance > 5){
75 layoutParams.width = (int) (0.9f*iv.getWidth());
76 layoutParams.height = (int) (0.9f*iv.getHeight());
77 lastDistance = curruntDistance;
78 iv.setLayoutParams(layoutParams);
79 Log.e(TAG,"缩小");
80 }
81 }
82 }
83
84 // Log.e(TAG, "move");
85 // //多点触控
86 // Log.e(TAG,"Point count : " + event.getPointerCount());
87 // if(event.getPointerCount() > 1){
88 // Log.e(TAG,String.format("x1:%f,y1:%f\nx2:%f,y2:%f",event.getX(0),event.getY(0),event.getX(1),event.getY(1)));
89 // }
90
91
92 // //获取单个触摸点
93 // LinearLayout.LayoutParams lp;
94 // lp = (LinearLayout.LayoutParams) iv.getLayoutParams();
95 // Log.e(TAG,String.format("x:%f,y:%f",event.getX(),event.getY()));
96 // lp.leftMargin = (int) event.getX();
97 // lp.topMargin = (int) event.getY();
98 // iv.setLayoutParams(lp);
99 break;
}
return true;
}
});
}
}