android多点触摸手势,安卓手势学习笔记(三) 多点触控

66b52468c121889b900d4956032f1009.png

8种机械键盘轴体对比

本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选?

跟踪多个触点

当多个手指同时触碰屏幕时,系统产生如下的事件:ACTION_DOWN –第一个触点。它启动了手势,在MotionEvent中该触点的数据索引是0.

ACTION_POINTER_DOWN –第一个触点之后触碰屏幕的其他触点。其索引通过getActionIndex()获取。

ACTION_MOVE –已经按下的手势发生变化。

ACTION_POINTER_UP –非最初手指抬起时发送。

ACTION_UP –最后一个手指离开屏幕时发送。

在MotionEvent中通过索引和ID可以跟踪各个触点。

索引:MotionEvent在一个数组中存储各个触点的信息。触点的索引就是触点在数组中的位置。大多数的MotionEvent方法使用索引来与触点交互。

ID:每个触点在触摸事件中维持一个ID映射,可以在整个手势过程中跟踪单独的触点。

各个触点在触摸事件中出现的顺序是不确定的。所以触点的索引可能在各个事件中不同,但其ID则会保持为一个常数。使用getPointerID()方法可以获取其ID。接下来的事件中,使用findPointerIndex()方法可以获取指定ID的触点的索引。例如: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的动作

使用getActionMasked()(或者用兼容版本MotionEventCompat.getActionMasked()更好)获取MotionEvent的动作。getActionMasked()用来处理多触点,而原来的getAction()方法不行。它返回正在执行的修饰过的动作,不包括触点的索引。可以用getActionIndex()来返回触点的索引。

注意:下面的例子使用MotionEventCompat类,可以为更多平台提供兼容性支持。但MotionEventCompat并非MotionEvent类的替代。它提供了一些静态方法,把MotionEvent作为参数传入,可以获取其相关的事件。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 "";

}

总结

看到这里发现有点混乱,所以和前面单触点的小节整合到一起看一看。首先,每一次触屏事件导致onTouchEvent()被调用,带来一个MotionEvent;

调用MotionEventCompat.getActionMasked(event)方法可以获取这个event的动作;

MotionEvent在一个数组从存储各个触点的数据,他们的索引并不固定;

每个触点同时有一个不变的ID;

调用event.getPointerID(index)可以获取指定索引的ID;

调用event.findPointerIndex(id)可以获取指定ID的索引;

调用MotionEventCompat.getActionIndex()可以获取动作触点的index; // 这里看上去很奇怪,一个event中存在多个触点,为什么要获取index?答案应该是,每个action只针对其中一个触点,所以这样就可以获取当前事件的动作的唯一触点,而其他触点的数据也存在这个event之中;

调用event.getPointerCount()可以区分当前是单触点操作还是多触点操作。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值