Last active
September 27, 2016 12:04
-
-
Save talhahasanzia/3b5987e8aff26bc0267b9c61015c7d80 to your computer and use it in GitHub Desktop.
Swipe motion.
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
// member declarations | |
private float x1, x2; | |
private float p1, p2 = 0; | |
static final int MIN_DISTANCE = 150; | |
static final int MIN_MOVEMENT = 3; | |
View.setOnTouchListener(new View.OnTouchListener() { | |
@Override | |
public boolean onTouch(View v, MotionEvent event) { | |
switch (event.getAction()) { | |
case MotionEvent.ACTION_MOVE: | |
p1 = event.getX(); | |
if (Math.abs(p2 - p1) > MIN_MOVEMENT) { | |
p2 = p1; | |
//moved | |
Log.d("Gesture", "Moved"); | |
} | |
Log.d("Gesture", "p1: " + p1 + " p2: " + p2); | |
break; | |
case MotionEvent.ACTION_DOWN: | |
x1 = event.getX(); | |
break; | |
case MotionEvent.ACTION_UP: | |
x2 = event.getX(); | |
float deltaX = x2 - x1; | |
if (Math.abs(deltaX) > MIN_DISTANCE) { | |
// Left to Right swipe action | |
if (x2 > x1) { | |
Log.d("Gesture", "Left to Right swipe [Next]"); | |
} | |
// Right to left swipe action | |
else { | |
Log.d("Gesture", "Right to Left swipe "); | |
} | |
} else { | |
// consider as something else - a screen tap for example | |
} | |
break; | |
} | |
return true; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment