How to implement a "Two Finger Drag" gesture on Android?
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
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!! :)