Created
January 15, 2015 11:15
-
-
Save omegasoft7/4dc0d817a9036e8243e8 to your computer and use it in GitHub Desktop.
SwipeDetectorIt should set as ontouch event of a View
This file contains hidden or 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.app.Activity; | |
import android.view.MotionEvent; | |
import android.view.View; | |
public abstract class ActivitySwipeDetector implements View.OnTouchListener { | |
private Activity activity; | |
static final int MIN_DISTANCE = 100; | |
private float downX, downY, upX, upY; | |
public ActivitySwipeDetector(final Activity activity) { | |
this.activity = activity; | |
} | |
public abstract void onRightToLeftSwipe(float distanceX); | |
public abstract void onLeftToRightSwipe(float distanceX); | |
public abstract void onTopToBottomSwipe(float distanceY); | |
public abstract void onBottomToTopSwipe(float distanceY); | |
public boolean onTouch(View v, MotionEvent event) { | |
switch(event.getAction()){ | |
case MotionEvent.ACTION_DOWN: { | |
downX = event.getX(); | |
downY = event.getY(); | |
// return true; | |
} | |
case MotionEvent.ACTION_UP: { | |
upX = event.getX(); | |
upY = event.getY(); | |
float deltaX = downX - upX; | |
float deltaY = downY - upY; | |
// swipe horizontal? | |
if(Math.abs(deltaX) > MIN_DISTANCE) { | |
// left or right | |
if(deltaX < 0) { this.onLeftToRightSwipe(deltaX); return true; } | |
if(deltaX > 0) { this.onRightToLeftSwipe(deltaX); return true; } | |
} | |
// else { Logger.logout( "Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE); } | |
// swipe vertical? | |
if(Math.abs(deltaY) > MIN_DISTANCE){ | |
// top or down | |
if(deltaY < 0) { this.onTopToBottomSwipe(deltaY); return true; } | |
if(deltaY > 0) { this.onBottomToTopSwipe(deltaY); return true; } | |
} | |
// else { Logger.logout("Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE); } | |
// return true; | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment