首页 > 代码库 > 多点触摸技术

多点触摸技术

技术分享

技术分享

 1 //多点触摸  放大,缩小
 2 public class MainActivity extends ActionBarActivity {
 3 
 4     @Override
 5     protected void onCreate(Bundle savedInstanceState) {
 6         super.onCreate(savedInstanceState);
 7         setContentView(R.layout.fragment_main);
 8 
 9     }
10 
11     @Override
12     public boolean onTouchEvent(MotionEvent event) {
13         // TODO Auto-generated method stub
14         // 两点触摸
15         if (event.getPointerCount() == 2) {
16             if (event.getAction() == MotionEvent.ACTION_MOVE) {
17                 int historySize = event.getHistorySize();
18                 if (historySize == 0) {
19                     return true;
20                 }
21                 // 获取第一个手指当前的纵坐标
22                 float currentY1 = event.getY(0);
23                 // 获取第一个手指当前最新的历史的纵坐标
24                 float historyY1 = event.getHistoricalY(0, historySize - 1);
25                 // 获取第二个手指当前的纵坐标
26                 float currentY2 = event.getY(2);
27                 // 获取第二个手指当前最新的历史的纵坐标
28                 float historyY2 = event.getHistoricalY(1, historySize - 1);
29                 // 两手指当前纵坐标的距离
30                 float distance = Math.abs(currentY1 - currentY2);
31                 // 两手指最新的历史纵坐标的距离
32                 float historyDistance = Math.abs(historyY1 - historyY2);
33 
34                 if (distance > historyDistance) {
35                     Log.i("-====>", "放大");
36                 } else if (distance < historyDistance) {
37                     Log.i("-====>", "缩小");
38                 } else {
39                     Log.i("-====>", "移动");
40                 }
41             }
42         }
43         return super.onTouchEvent(event);
44     }
45 
46 }

 

多点触摸技术