Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android上检测两个手指的滑动手势_Android_Swipe - Fatal编程技术网

如何在Android上检测两个手指的滑动手势

如何在Android上检测两个手指的滑动手势,android,swipe,Android,Swipe,我想在我的android应用程序中实现两个手指滑动手势。我使用了以下代码: void handleTouch(MotionEvent m){ //Number of touches int pointerCount = m.getPointerCount(); if(pointerCount == 2){ int action = m.getActionMasked(); int actionIndex = m.getActionIndex(); String actio

我想在我的android应用程序中实现两个手指滑动手势。我使用了以下代码:

void handleTouch(MotionEvent m){
//Number of touches
int pointerCount = m.getPointerCount();
if(pointerCount == 2){
    int action = m.getActionMasked();
    int actionIndex = m.getActionIndex();
    String actionString;
    TextView tv = (TextView) findViewById(R.id.testDiffText);
    switch (action)
    {
        case MotionEvent.ACTION_DOWN:                   
            GLOBAL_TOUCH_POSITION_X = (int) m.getX(1);
            actionString = "DOWN"+" current "+GLOBAL_TOUCH_CURRENT_POSITION_X+" prev "+GLOBAL_TOUCH_POSITION_X;
            tv.setText(actionString);
            break;
        case MotionEvent.ACTION_UP:                 
            GLOBAL_TOUCH_CURRENT_POSITION_X = 0;
            actionString = "UP"+" current "+GLOBAL_TOUCH_CURRENT_POSITION_X+" prev "+GLOBAL_TOUCH_POSITION_X;
            tv.setText(actionString);  
            break;  
        case MotionEvent.ACTION_MOVE:
            GLOBAL_TOUCH_CURRENT_POSITION_X = (int) m.getX(1);
            int diff = GLOBAL_TOUCH_POSITION_X-GLOBAL_TOUCH_CURRENT_POSITION_X;
            actionString = "Diff "+diff+" current "+GLOBAL_TOUCH_CURRENT_POSITION_X+" prev "+GLOBAL_TOUCH_POSITION_X;
            tv.setText(actionString);                   
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
            GLOBAL_TOUCH_POSITION_X = (int) m.getX(1);
            actionString = "DOWN"+" current "+GLOBAL_TOUCH_CURRENT_POSITION_X+" prev "+GLOBAL_TOUCH_POSITION_X;
            tv.setText(actionString);
            break;
        default:
            actionString = "";
    }

    pointerCount = 0;
}
else {
    GLOBAL_TOUCH_POSITION_X = 0;
    GLOBAL_TOUCH_CURRENT_POSITION_X = 0;
}
}

我在向上或向下滑动中执行相同的操作。它只执行操作任务。有人能帮我实现上下滑动和下上滑动吗

private static final int NONE = 0;
private static final int SWIPE = 1;
private int mode = NONE;
private float startY;
private float stopY;
// We will only detect a swipe if the difference is at least 100 pixels
// Change this value to your needs
private static final int TRESHOLD = 100;

public boolean onTouch(View v, MotionEvent event)
{
    switch (event.getAction() & MotionEvent.ACTION_MASK)
    {
        case MotionEvent.ACTION_POINTER_DOWN:
            // This happens when you touch the screen with two fingers
            mode = SWIPE;
            // You can also use event.getY(1) or the average of the two
            startY = event.getY(0);
            break;

        case MotionEvent.ACTION_POINTER_UP:
            // This happens when you release the second finger
            mode = NONE;
            if(Math.abs(startY - stopY) > TRESHOLD)
            {
                if(startY > stopY)
                {
                    // Swipe up
                }
                else
                {
                    //Swipe down
                }
            }
            this.mode = NONE;
            break;

        case MotionEvent.ACTION_MOVE:
            if(mode == SWIPE)
            {
                stopY = event.getY(0);
            }
            break;
    }

    return true;
}

我希望这有帮助。

用两个手指滑动是否有效?MotionEvent.ACTION\u DOWN表示一个手指。MotionEvent.ACTION\u POINTER\u DOWN是两个手指。非常感谢@Inneke De Clippel.:)请您帮助左右滑动两个手指。@Inneke De Clippel。对于左右滑动,您必须以与startY和stopY相同的方式保存startX和stopX。如果(stopX和startX之间的差值)大于(stopY和startY之间的差值),则将向左或向右滑动。因此,在那之后,如果startX>stopX,则扫向左侧。当我开始投票时,它给我带来的喜悦是一个封闭的问题,因为这正是我所寻找的,尽管它不是“专注的”-去算数。答案很简单,但也很有帮助,因此再次投票。我的竞选活动还在继续。。。