How to implement a “Two Finger Drag” gesture on Android?

China☆狼群 提交于 2019-11-28 06:03:42

Okay So thanks to Gabe here and numerous blogs on this, I have found a solution to my question!

First I initialized my variables in the "Activity" class

int GLOBAL_TOUCH_POSITION_X = 0;
int GLOBAL_TOUCH_CURRENT_POSITION_X = 0;

Next, Inside the onCreate():

//Two-Finger Drag Gesture detection
    RelativeLayout TextLoggerLayout = (RelativeLayout)findViewById(R.id.ActivityrView);
    TextLoggerLayout.setOnTouchListener(
            new RelativeLayout.OnTouchListener(){

                @Override
                public boolean onTouch(View v, MotionEvent m) {
                    handleTouch(m); 
                    return true;
                }

    });

Now define the function handleTouch(m) as follows, it outputs the current position of the "Two-finger-touch" along with the initial position of the touch:

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

There you have it! The "Two-finger-drag" gesture finally implemented. Looks, like I can wrte a tiny blog post on it as well!! :)

I don't know how relevant adding an answer now would be to such an old question but I have been developing a library to support basic 1 and 2 finger gestures on Android with a very very simple drop in functionality.

Most gestures can be set as easily as

view.setOn2FingerGestureListener ( new On2FingerGestureListener () {
@Override 
onSwipeup() {
}
onSwipedown() {
}
});

It is open source as well as available to be downloaded as a .jar

Touch and drag is a series of events. First you have a down, then you have 1 or more moves, then you have an up. You detect a starting point on the down, then the drag occurs on the moves. THe drag stops on an up.

To do a 2 finger drag, only pay attention to drags if MotionEvent.getPointerCount() ==2.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!