博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Using Touch Gestures 》Handling Multi-Touch Gestures
阅读量:4207 次
发布时间:2019-05-26

本文共 4253 字,大约阅读时间需要 14 分钟。

A multi-touch gesture is when multiple pointers (fingers) touch the screen at the same time. This lesson describes how to detect gestures that involve multiple pointers.

Track Multiple Pointers


When multiple pointers touch the screen at the same time, the system generates the following touch events:

  • —For the first pointer that touches the screen. This starts the gesture. The pointer data for this pointer is always at index 0 in the .
  • —For extra pointers that enter the screen beyond the first. The pointer data for this pointer is at the index returned by .
  • —A change has happened during a press gesture.
  • —Sent when a non-primary pointer goes up.
  • —Sent when the last pointer leaves the screen.

You keep track of individual pointers within a  via each pointer's index and ID:

  • Index: A  effectively stores information about each pointer in an array. The index of a pointer is its position within this array. Most of the  methods you use to interact with pointers take the pointer index as a parameter, not the pointer ID.
  • ID: Each pointer also has an ID mapping that stays persistent across touch events to allow tracking an individual pointer across the entire gesture.

The order in which individual pointers appear within a motion event is undefined. Thus the index of a pointer can change from one event to the next, but the pointer ID of a pointer is guaranteed to remain constant as long as the pointer remains active. Use the  method to obtain a pointer's ID to track the pointer across all subsequent motion events in a gesture. Then for successive motion events, use the method to obtain the pointer index for a given pointer ID in that motion event. For example:

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);}

Get a MotionEvent's Action


You should always use the method  (or better yet, the compatability version) to retrieve the action of a . Unlike the older method,  is designed to work with multiple pointers. It returns the masked action being performed, without including the pointer index bits. You can then use  to return the index of the pointer associated with the action. This is illustrated in the snippet below.

Note: This example uses the  class. This class is in the . You should use to provide the best support for a wide range of platforms. Note that is not a replacement for the  class. Rather, it provides static utility methods to which you pass your  object in order to receive the desired action associated with that event.

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 descriptionpublic 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 "";}

For more discussion of multi-touch and some examples, see the lesson .

转载地址:http://axlli.baihongyu.com/

你可能感兴趣的文章
openstack官方docker介绍
查看>>
[转]在ASP.NET 2.0中操作数据::创建一个数据访问层
查看>>
Linux命令之chmod详解
查看>>
【java小程序实战】小程序注销功能实现
查看>>
Java中子类能否继承父类的私有属性和方法
查看>>
JVM内存模型详解
查看>>
(六) Git--标签管理
查看>>
建造者模式(Builder)-设计模式(三)
查看>>
Linux-网络运维基础
查看>>
Verilog编程网站学习——门电路、组合电路、时序电路
查看>>
android——学生信息显示和添加
查看>>
Android——ImageSwitcher轮流显示动画
查看>>
Android——利用手机端的文件存储和SQLite实现一个拍照图片管理系统
查看>>
图像调优1:清晰度相关参数MTF,SFR,MTF50,MTF50P 以及TVL的概念以及换算说明
查看>>
罗永浩欲直播带货,京东说可以帮忙联系
查看>>
B站,正在变成下一个“公众号”?
查看>>
小米启动安心服务月 手机家电产品可免费清洁保养
查看>>
刘作虎:一加新品将全系支持 5G
查看>>
滴滴顺风车上线新功能,特殊时期便捷出行
查看>>
不会延期!iPhone 12S预计如期在9月发售:升级三星LTPO屏幕
查看>>