失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > android多点触摸手势 安卓手势学习笔记(三) 多点触控

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

时间:2019-07-05 17:23:32

相关推荐

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

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()可以区分当前是单触点操作还是多触点操作。

如果觉得《android多点触摸手势 安卓手势学习笔记(三) 多点触控》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。