“双指滑动”手势?

时间:2014-10-06 11:58:47

标签: 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 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;
}

}

我在向上或向下滑动中执行相同的操作。它只执行Action_up任务。任何人都可以帮我实现从底部滑动到底部向上滑动。

1 个答案:

答案 0 :(得分:5)

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

我希望这会有所帮助。

相关问题