I tried implementing 2 finger swipe using onFling
method of SimpleOnGestureListener
. However,I found that SimpleOnGestureListener
doesn't support two finger touch.
How can I work it out ?
I tried implementing 2 finger swipe using onFling
method of SimpleOnGestureListener
. However,I found that SimpleOnGestureListener
doesn't support two finger touch.
How can I work it out ?
My Solution works, but its quite ugly to use a public static int to count how many fingers there are. i had to set fingercount back to one after i flinged with two fingers because i couldnot get the action_pointer_down for one finger anymore... i dont know why... but this solution would also work for more fingers... hope someone can use it in the feature
public class RemoteFragment extends Fragment{
public static int fingercount = 1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final GestureDetector gdt = new GestureDetector(getActivity(),
new RemoteGestureListener());
View view = inflater.inflate(R.layout.gestures, container, false);
view.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(final View view, final MotionEvent event) {
int action = event.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_POINTER_DOWN:
Log.d(TAG,
"Pointer-Count ="
+ String.valueOf(fingerCount = event
.getPointerCount()));
break;
}
gdt.onTouchEvent(event);
return true;
}
});
return view;
}
}
public class RemoteGestureListener implements OnGestureListenerr {
private final static String TAG = "RemoteGestureListener";
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
public RemoteGestureListener() {
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
Log.d(TAG, "onFling");
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
if (RemoteFragment.fingerCount == 1) {
Log.d(TAG, "Left Swipe");
} else {
Log.d(TAG, "Left xFinger Swipe");
RemoteFragment.fingerCount = 1;
}
return true; // Right to left
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
if (RemoteFragment.fingerCount == 1) {
Log.d(TAG, "Right Swipe");
} else {
Log.d(TAG, "Right xFinger Swipe");
RemoteFragment.fingerCount = 1;
}
return true; // Left to right
}
if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
if (RemoteFragment.fingerCount == 1) {
Log.d(TAG, "Top Swipe");
} else {
Log.d(TAG, "Top xFinger Swipe");
RemoteFragment.fingerCount = 1;
}
return true; // Bottom to top
} else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
if (RemoteFragment.fingerCount == 1) {
Log.d(TAG, "Bottom Swipe");
} else {
Log.d(TAG, "Bottom xFinger Swipe");
RemoteFragment.fingerCount = 1;
}
return true;
}
return true;
}
You could try to use OnTouchListener with MotionEvent.ACTION_POINTER_DOWN like this:
button.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent motionEvent) {
int action = motionEvent.getAction();
switch(action & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_POINTER_DOWN:
Toast.makeText(MyActivity.this, " Two Fingers Tapped Once. Yeeeyy :)", 0).show();
// set the mTwoFingersTapped flag to TRUE when we tap with 2 fingers at once
mTwoFingersTapped = true;
break;
}
return false;
}
});
Here is a tutorial which explains how to do it if you need more details. I hope it will help.
I had made a library some time ago that makes implementing 1/multi finger swipes very easy.
You can find more detailed documentation of the API here A sample implementation can be found here
The part that you want to look at is
ImageView mv = (ImageView) findViewById(R.id.myview);
final TextView grtv = (TextView) findViewById(R.id.gestureResultTextView);
SimpleFingerGestures.DEBUG = true;
SimpleFingerGestures.CONSUME_TOUCH_EVENTS = true;
SimpleFingerGestures sfg = new SimpleFingerGestures();
sfg.setOnFingerGestureListener(new SimpleFingerGestures.OnFingerGestureListener() {
@Override
public boolean onSwipeUp(int fingers, long gestureDuration) {
grtv.setText("swiped " + fingers + " up");
return false;
}
@Override
public boolean onSwipeDown(int fingers, long gestureDuration) {
grtv.setText("swiped " + fingers + " down");
return false;
}
@Override
public boolean onSwipeLeft(int fingers, long gestureDuration) {
grtv.setText("swiped " + fingers + " left");
return false;
}
@Override
public boolean onSwipeRight(int fingers, long gestureDuration) {
grtv.setText("swiped " + fingers + " right");
return false;
}
@Override
public boolean onPinch(int fingers, long gestureDuration) {
grtv.setText("pinch");
return false;
}
@Override
public boolean onUnpinch(int fingers, long gestureDuration) {
grtv.setText("unpinch");
return false;
}
});
mv.setOnTouchListener(sfg);