1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-14 14:09:28 +01:00
Gadgetbridge/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/SwipeEvents.java
vanous 4d42e169b9 Add GPS fragment to Sport Activity detail
add GPX parser
fix possible swiping issue after device rotation
use window background color for screenshots
2020-10-24 18:27:51 +02:00

58 lines
1.8 KiB
Java

package nodomain.freeyourgadget.gadgetbridge.util;
import android.content.Context;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
//simple swipe detector based on GestureDetector, inspired by https://stackoverflow.com/a/19506010
public class SwipeEvents implements View.OnTouchListener {
private final GestureDetector gestureDetector;
public SwipeEvents(Context context) {
gestureDetector = new GestureDetector(context, new GestureListener());
}
public void onSwipeLeft() {
}
public void onSwipeRight() {
}
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
private final class GestureListener extends GestureDetector.SimpleOnGestureListener {
private static final int SWIPE_DISTANCE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return false;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (e1 == null || e2 == null){
return false;
}
float distanceX = e2.getX() - e1.getX();
float distanceY = e2.getY() - e1.getY();
if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (distanceX > 0)
onSwipeRight();
else
onSwipeLeft();
return true;
}
return false;
}
}
}