[Android分享] 使用触摸手势(四)

原文链接:http://developer.android.com/training/gestures/multi.html

处理多点触摸手势

跟踪多点触摸

当多点同时触摸屏幕时,系统将会产生如下的触摸事件:

1.ACTION_DOWN:触摸屏幕的第一个点。此时手势开始。该点的数据通常在MotionEvent事件队列索引位置0处。
2.ACTION_POINTER_DOWN:除了第一个点的其他触摸点数据。该点的数据的索引位置由getActionIndex()方法返回。
3.ACTION_MOVE:在手势过程中发生的一次变化。
4.ACTION_POINTER_UP:当不是第一个点的其他点UP后触发。
5.ACTION_UP:当手势中的最后一个点离开屏幕。

通过每个触摸点的index和ID来跟踪MotionEvent中每个触摸点的信息:

Index:一个MotionEvent将每个触摸点的信息有效的存储在一个数组中。每个触摸点的Index就是它在该数组中的位置。你使用的MotionEvent中的大多数方法将index作为一个参数,而不是触摸点的ID。

ID:每一个触摸点在触摸事件中保持一个映射的ID,在整个多点手势中来追踪某一个触摸点的事件。

每一个触摸点在一个MotionEvent的数组中出现的序列是不确定的。因此一个触摸点的index有可能从一个event到另一个event发生变化,但一个触摸点的ID在这个点的存活期间是保持不变的。使用getPointerId()方法来获得一个触摸点的ID,并在整个手势中跟踪一个点。在连续的运动事件中,使用findPointerIndex()方法来根据特定触摸点的ID获取该点的index。

?
代码片段,双击复制
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
private int mActivePointerId;
  
public boolean onTouchEvent(MotionEvent event) {
    ....
    // Get the pointer ID
    mActivePointerId = event.getPointerId( 0 );
 
    // ... Many touch events later...
 
    // Use the pointer ID to find the index of the active pointer
    // and fetch its position
    int pointerIndex = event.findPointerIndex(mActivePointerId);
    // Get the pointer's current position
    float x = event.getX(pointerIndex);
    float y = event.getY(pointerIndex);
}


获取MotionEvent的Action

应该始终使用getActionMasked()来获取一个MotionEvent的action。与旧版的getAction方法不同,getActionMask方法被设计用来在多点触摸的情况下工作。它包括该次事件的action,但并不包括触摸点的index。能使用getActionIndex()返回与该action关联点的index。
?
代码片段,双击复制
01
02
03
04
05
06
07
08
09
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
int action = MotionEventCompat.getActionMasked(event);
// Get the index of the pointer associated with the action.
int index = MotionEventCompat.getActionIndex(event);
int xPos = - 1 ;
int yPos = - 1 ;
 
Log.d(DEBUG_TAG, "The action is " + actionToString(action));
             
if (event.getPointerCount() > 1 ) {
    Log.d(DEBUG_TAG, "Multitouch event" );
    // The coordinates of the current screen contact, relative to
    // the responding View or Activity.  
    xPos = ( int )MotionEventCompat.getX(event, index);
    yPos = ( int )MotionEventCompat.getY(event, index);
 
} else {
    // Single touch event
    Log.d(DEBUG_TAG, "Single touch event" );
    xPos = ( int )MotionEventCompat.getX(event, index);
    yPos = ( int )MotionEventCompat.getY(event, index);
}
...
 
// Given an action int, returns a string description
public static String actionToString( int action) {
    switch (action) {
                 
         case MotionEvent.ACTION_DOWN: return "Down" ;
         case MotionEvent.ACTION_MOVE: return "Move" ;
         case MotionEvent.ACTION_POINTER_DOWN: return "Pointer Down" ;
         case MotionEvent.ACTION_UP: return "Up" ;
         case MotionEvent.ACTION_POINTER_UP: return "Pointer Up" ;
         case MotionEvent.ACTION_OUTSIDE: return "Outside" ;
         case MotionEvent.ACTION_CANCEL: return "Cancel" ;
    }
    return "" ;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值