7
7
import android .view .ScaleGestureDetector ;
8
8
import android .view .View ;
9
9
10
+ import androidx .annotation .NonNull ;
11
+
10
12
/*
11
13
The GestureHelper present a simple gesture api for the PdfViewer
12
14
*/
13
15
14
16
class GestureHelper {
15
17
public interface GestureListener {
16
18
boolean onTapUp ();
19
+
20
+ void onSwipeLeft ();
21
+ void onSwipeRight ();
22
+
17
23
// Can be replaced with ratio when supported
18
24
void onZoomIn (float value );
19
25
void onZoomOut (float value );
@@ -29,6 +35,31 @@ static void attach(Context context, View gestureView, GestureListener listener)
29
35
public boolean onSingleTapUp (MotionEvent motionEvent ) {
30
36
return listener .onTapUp ();
31
37
}
38
+
39
+ // inspired by https://www.geeksforgeeks.org/how-to-detect-swipe-direction-in-android/
40
+ @ Override
41
+ public boolean onFling (@ NonNull MotionEvent e1 , @ NonNull MotionEvent e2 , float velocityX , float velocityY ) {
42
+ final int swipeThreshold = 100 ;
43
+ final int swipeVelocityThreshold = 100 ;
44
+
45
+ final float diffX = e2 .getX () - e1 .getX ();
46
+ final float absDiffX = Math .abs (diffX );
47
+ final float diffY = e2 .getY () - e1 .getY ();
48
+ final float absDiffY = Math .abs (diffY );
49
+
50
+ if (absDiffX > absDiffY // only handle horizontal swipe
51
+ && absDiffX > swipeThreshold
52
+ && Math .abs (velocityX ) > swipeVelocityThreshold ) {
53
+ if (diffX > 0 ) {
54
+ listener .onSwipeRight ();
55
+ } else {
56
+ listener .onSwipeLeft ();
57
+ }
58
+ return true ;
59
+ }
60
+
61
+ return false ;
62
+ }
32
63
});
33
64
34
65
final ScaleGestureDetector scaleDetector = new ScaleGestureDetector (context ,
0 commit comments