21、多点触摸技术

什么是多点触摸技术

       多点触摸是一项很早就使用的技术,从IPhone第一代就支持多点触摸。要想了解什么叫多点触摸,首先应了解什么是单点触摸。早期的触摸屏无论有多少个手指接触到屏幕,系统只会认为第1个接触到屏幕的手指是有效的,后来接触到屏幕的手指将被忽略。这就是所谓单点触摸,单点就是指第1个接触到屏幕的手指。

      了解了单点触摸,多点触摸就很容易理解了。所谓多点触摸就是系统同时接受多个手指触摸屏幕的动作。这些手指的按下、移动等操作所生成的数据都可以通过程序获取。根据这些数据的变化可以做出很多有趣的应用,例如,图像的放大和缩小就是典型的多点触摸应用,当两个或多个手指向内收缩时,图像按着一定比例缩小,当两个或多个手指向外扩张时,图像就会按一定比例放大。

Android对多点触摸的支持

       Android SDK从2.0开始支持多点触摸,不过最初只支持两点触摸(屏幕最多能检测到两个手指的触摸动作),而最新的Android版本支持五点触摸。当然,大多数应用只需要两个手指就够了。

如何使用多点触摸

onTouchEvent事件方法

MotionEvent.getX(int pointerIndex)

MotionEvent.getY(int pointerIndex)

MotionEvent.getHistoricalX(int pointerIndex, int pos)

MotionEvent.getHistoricalY(int pointerIndex, int pos)

Demo
 1 import android.app.Activity;

 2 import android.os.Bundle;

 3 import android.util.Log;

 4 import android.view.MotionEvent;

 5 

 6 public class MultiTouchActivity extends Activity {

 7 

 8     @Override

 9     protected void onCreate(Bundle savedInstanceState) {

10         super.onCreate(savedInstanceState);

11         setContentView(R.layout.activity_multi_touch);

12     }

13 

14     public boolean onTouchEvent(MotionEvent event) {

15         // 只有是亮点触摸才执行

16         if (event.getPointerCount() == 2) {

17             if (event.getAction() == MotionEvent.ACTION_MOVE) {

18                 int historySize = event.getHistorySize();

19                 if (historySize == 0)

20                     return true;

21 

22                 // 获取第一个手指当前的纵坐标

23                 float currentY1 = event.getY(0);

24                 // 获取第一个手指最新的历史纵坐标

25                 float historyY1 = event.getHistoricalY(0, historySize - 1);

26 

27                 // 获取第二个手指当前的纵坐标

28                 float currentY2 = event.getY(1);

29                 // 获取第二个手指最新的历史纵坐标

30                 float historyY2 = event.getHistoricalY(1, historySize - 1);

31 

32                 // 两手指当前纵坐标的距离

33                 float distance = Math.abs(currentY1 - currentY2);

34                 // 两手指最新的历史纵坐标的距离

35                 float historyDistance = Math.abs(historyY1 - historyY2);

36 

37                 if (distance > historyDistance) {

38                     Log.d("status", "放大");

39 

40                 } else if (distance < historyDistance) {

41                     Log.d("status", "缩小");

42                 } else {

43                     Log.d("status", "移动");

44                 }

45 

46             }

47         }

48         return true;

49     }

50 

51 }

 

 
 

你可能感兴趣的:(技术)