android - OnTouch 事件的行为如何?

标签 android multi-touch

如果设备上有多个手指,onTouch-listener 是为每个手指运行一次,还是每个事件(手指按下、移动等)运行一次并包含每个 Activity 的触摸指针?

如果

(e.getAction() & MotionEvent.ACTION_MASK == MotionEvent.ACTION_MOVE)

我如何知道当前哪个指针正在移动?

final int pointerIndex = e.getActionIndex();
pointerID = e.getPointerId(pointerIndex);

出于某种原因总是返回 0(当 MotionEvent.ACTION_MOVE 时)..

最佳答案

首先,您获得这些事件:

  1. MotionEvent.ACTION_DOWN
    第一次触摸屏幕
  2. MotionEvent.ACTION_POINTER_DOWN
    对于所有非主要接触
  3. MotionEvent.ACTION_UP
    最后一次修饰
  4. MotionEvent.ACTION_POINTER_UP
    对于每一次非主要修饰
  5. MotionEvent.ACTION_MOVE
    每当被触摸的手指移动
    这里你必须检查所有数字并检查坐标是否已更改

这样做:

@Override
public boolean onTouchEvent(MotionEvent ev)
{
    final int action = ev.getAction();
    final int pointerIndex = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT; //index of this pointer

    switch (action & MotionEvent.ACTION_MASK)
    {
        case MotionEvent.ACTION_DOWN:
            //your first touch on the screen
            break;

        case MotionEvent.ACTION_MOVE:
            //one of the touches has moved
            for (unsigned int i = 0; i < ev.getPointerCount(); i++)
            {
                //The pointer id is ev.getPointerId(i);
                //for loop through all touches
            }
            break;

        case MotionEvent.ACTION_UP:
            //your last touch has gone up
            break;

        case MotionEvent.ACTION_POINTER_DOWN:
            //non-primary pointer has gone down
            break;

        case MotionEvent.ACTION_POINTER_UP:
            //non-primary pointer has gone up
            break;
        }
    }

    return true;
}

希望这对你有一点帮助:)

关于android - OnTouch 事件的行为如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14201130/

相关文章:

java - 找不到类 'com.google.android.chimera.Activity' ,从方法 mu.b 引用

android - 在android中通过索引指针检测移动事件

Android Multitouch Dynamic Imageview Drag 只是最近添加的Imageview

user-interface - Android布局设计: Tablet first or Mobile first

java - Gson 只用 false 反序列化 boolean JSON,不管输入

android - 从android执行.bat文件

c++ - (如何)我可以在 Windows 上模拟触摸事件吗?

java - 在RelativeLayout上检测多点触摸

c# - 同时按下多个按钮

android - 如何在加载内容后将 Visibility(View.VISIBLE) 设置为 webView?