Reputation: 320
I am working on swipe gesture. Actually I want to restrict swipe if touch coordinates > Tap on left and right edges of screen (100 px) like
If I swipe from left to right and touch coordinate > from screen left Top 100px I want to restrict swipe
If swipe from right to left and touch coordinate
And There is no Restriction for Top and Bottom swipe.
Here is my code what I tried :
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD
&& Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
}
} else {
if (Math.abs(diffY) > SWIPE_THRESHOLD
&& Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
onSwipeBottom();
} else {
onSwipeTop();
}
}
}
Upvotes: 1
Views: 2605
Reputation: 6924
there is a great post for getting the siplay metrics: Get screen dimensions in pixels. i'm not sure that i quite understance the meaning of swipe along the side s of the screen (1 finger at the right and the other at the left maybe?).. but i guuess that in your case it should be something like:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
PointF p1 = new PointF(e1.getX(), e1.getY());
PointF p2 = new PointF(e2.getX(), e2.getY());
if ((p1.x < 100 || p1.x > width - 100) && (p2.x < 100 || p2.x > width - 100))
{
float diffY = p2.y - p1.y;
float diffX = p2.x - p1.x;
int dirY = (diffY < 0) ? -1 : 1;
int dirX = (diffX < 0) ? -1 : 1;
diffY = Math.abs(diffY); diffX = Math.abs(diffX);
if (diffX > diffY) {
if (diffX > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (dirX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
}
} else {
if (diffY > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (dirY > 0) {
onSwipeBottom();
} else {
onSwipeTop();
}
}
}
}
Upvotes: 0
Reputation: 384
You can calculate this using the onFling method in SimpleOnGestureListener. Please check the following question:
Android: How to handle right to left swipe gestures
Upvotes: 0
Reputation: 706
Check this might help you
https://github.com/daimajia/AndroidSwipeLayout
Its library predefined swipe implementation
Upvotes: 3