简体   繁体   English

如何在Android touchlistener中同时检测到触摸屏幕上的次要手指?

[英]how to detect the secondary finger on touching the screen not at same time in android touchlistener?

How to detect an additional finger on the screen? 如何检测屏幕上的其他手指? Eg I touch the screen using one finger and after sometime I keep the first finger on screen and then touch the screen using other finger while keeping first finger as it is? 例如,我用一根手指触摸屏幕,一段时间后我将第一根手指放在屏幕上,然后用另一根手指触摸屏幕,同时保持第一根手指的原样? How to detect the second finger touch in Touch Listener? 如何在Touch Listener中检测秒针触摸?

The MotionEvent.ACTION_POINTER_DOWN and MotionEvent.ACTION_POINTER_UP are send starting with the second finger. 从第二个手指开始发送MotionEvent.ACTION_POINTER_DOWN and MotionEvent.ACTION_POINTER_UP For the first finger MotionEvent.ACTION_DOWN and MotionEvent.ACTION_UP are used. 对于第一个手指MotionEvent.ACTION_DOWN and MotionEvent.ACTION_UP使用MotionEvent.ACTION_DOWN and MotionEvent.ACTION_UP

The getPointerCount() method on MotionEvent allows you to determine the number of pointers on the device. getPointerCount()方法MotionEvent允许您确定在装置上的指针的数目。 All events and the position of the pointers are included in the instance of MotionEvent which you receive in the onTouch() method. 所有事件和指针的位置都包含在您在onTouch()方法中收到的onTouch()实例中。

To track the touch events from multiple pointers you have to use the MotionEvent.getActionIndex() and the MotionEvent.getActionMasked() methods to identify the index of the pointer and the touch event which happened for this pointer. 要跟踪来自多个指针的触摸事件,您必须使用MotionEvent.getActionIndex()MotionEvent.getActionMasked()方法来标识指针的索引和此指针发生的触摸事件。

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

For more details please visit Google Handling Multi-Touch Gestures . 有关详细信息,请访问Google 处理多点触控手势

If you need to process multi touch events you should check PointerCount. 如果您需要处理多点触控事件,请检查PointerCount。

    public boolean onTouchEvent(MotionEvent event) {    
        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);
        }
...

    }

You can find more information here: https://developer.android.com/training/gestures/multi.html 您可以在此处找到更多信息: https//developer.android.com/training/gestures/multi.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Android 上,当第一根手指从屏幕上松开时,如何判断第二根手指的位置,该手指没有移动但正在触摸屏幕 - On Android, how can I tell the location of a second finger that isn't moving but is touching the screen when the first finger releases from the screen Android:当另一个手指已经触摸屏幕时,是否可以处理点击? - Android: Is it possible to handle a click while another finger is already touching the screen? 让最后一根手指触摸屏幕 - Getting the last finger touching the screen 检测手指何时离开屏幕并更新指针 - detect when a finger leaves the screen and update pointers touchlistener的代码出错 - Android - Error on code for touchlistener - Android 用于customadapter listview的Android touchlistener - Android touchlistener for customadapter listview Android-触摸屏幕时执行某些操作 - Android - Do something while touching the screen 为什么在触摸Android屏幕时却什么也没做? - Why, when touching the Android screen it does nothing? 每当用户的手指不触摸屏幕时,我都想让游戏暂停 - I want to make my game pause whenever the users finger is not touching the screen 更新工作—如何通过在 android 的触摸屏上滑动手指来测试智能手机屏幕? - UPDATE WORKING— How to Test Smartphone Screen by swiping finger on the touch screen in android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM