Last active
May 28, 2018 18:45
-
-
Save vic797/bd4b51a5f45b9c4296b55e2c04dc090c to your computer and use it in GitHub Desktop.
This is a simple class for basic gestures detection on Android. The original class that I found on internet includes only the swipe gestures.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.util.Log; | |
import android.view.GestureDetector; | |
import android.view.MotionEvent; | |
import android.view.View; | |
@SuppressWarnings("unused") | |
public abstract class OnSwipeTouchListener implements View.OnTouchListener { | |
private final GestureDetector gestureDetector; | |
public OnSwipeTouchListener(Context context) { | |
gestureDetector = new GestureDetector(context, new GestureListener()); | |
} | |
@Override | |
public boolean onTouch(View v, MotionEvent event) { | |
return gestureDetector.onTouchEvent(event); | |
} | |
abstract void onSwipeRight(); | |
abstract void onSwipeLeft(); | |
abstract void onSwipeTop(); | |
abstract void onSwipeBottom(); | |
abstract void onDoubleTouch(); | |
abstract void onLongTouch(); | |
abstract void onSingleTouch(); | |
private class GestureListener extends GestureDetector.SimpleOnGestureListener{ | |
private static final int SWIPE_THRESHOLD = 100; | |
private static final int SWIPE_VELOCITY_THRESHOLD = 100; | |
private static final String TAG = "OnSwipeTouchListener"; | |
@Override | |
public boolean onDown(MotionEvent e) { | |
return true; | |
} | |
@Override | |
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { | |
boolean result = false; | |
try { | |
float diffY = e2.getY() - e1.getY(); | |
float diffX = e2.getX() - e1.getX(); | |
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) { | |
if (diffX > 0) { | |
Log.i(TAG, "onSwipeRight"); | |
onSwipeRight(); | |
} else { | |
Log.i(TAG, "onSwipeLeft"); | |
onSwipeLeft(); | |
} | |
result = true; | |
} else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) { | |
if (diffY > 0) { | |
Log.i(TAG, "onSwipeBottom"); | |
onSwipeBottom(); | |
} else { | |
Log.i(TAG, "onSwipeTop"); | |
onSwipeTop(); | |
} | |
result = true; | |
} | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
} | |
return result; | |
} | |
@Override | |
public boolean onDoubleTap(MotionEvent e) { | |
Log.i(TAG, "onDoubleTouch"); | |
onDoubleTouch(); | |
return true; | |
} | |
@Override | |
public void onLongPress(MotionEvent e) { | |
Log.i(TAG, "onLongTouch"); | |
onLongTouch(); | |
super.onLongPress(e); | |
} | |
@Override | |
public boolean onSingleTapConfirmed(MotionEvent e) { | |
Log.i(TAG, "onSingleTouch"); | |
onSingleTouch(); | |
return super.onSingleTapConfirmed(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment