2424
2525import com .github .barteksc .pdfviewer .model .LinkTapEvent ;
2626import com .github .barteksc .pdfviewer .scroll .ScrollHandle ;
27+ import com .github .barteksc .pdfviewer .util .SnapEdge ;
2728import com .shockwave .pdfium .PdfDocument ;
2829import com .shockwave .pdfium .util .SizeF ;
2930
@@ -97,7 +98,7 @@ private boolean checkLinkTapped(float x, float y) {
9798 for (PdfDocument .Link link : pdfFile .getPageLinks (page )) {
9899 RectF mapped = pdfFile .mapRectToDevice (page , pageX , pageY , (int ) pageSize .getWidth (),
99100 (int ) pageSize .getHeight (), link .getBounds ());
100- fixCoords ( mapped );
101+ mapped . sort ( );
101102 if (mapped .contains (mappedX , mappedY )) {
102103 pdfView .callbacks .callLinkHandler (new LinkTapEvent (x , y , mappedX , mappedY , mapped , link ));
103104 return true ;
@@ -106,26 +107,27 @@ private boolean checkLinkTapped(float x, float y) {
106107 return false ;
107108 }
108109
109- /** Fix different coordinate axis */
110- private void fixCoords (RectF rect ) {
111- if (rect .top > rect .bottom ) {
112- swapTopBottom (rect );
110+ private void startPageFling (MotionEvent downEvent , MotionEvent ev , float velocityX , float velocityY ) {
111+ if (!checkDoPageFling (velocityX , velocityY )) {
112+ return ;
113113 }
114- if (rect .left > rect .right ) {
115- swapLeftRight (rect );
116- }
117- }
118-
119- private void swapTopBottom (RectF rect ) {
120- float tmp = rect .top ;
121- rect .top = rect .bottom ;
122- rect .bottom = tmp ;
123- }
124114
125- private void swapLeftRight (RectF rect ) {
126- float tmp = rect .left ;
127- rect .left = rect .right ;
128- rect .right = tmp ;
115+ int direction ;
116+ if (pdfView .isSwipeVertical ()) {
117+ direction = velocityY > 0 ? -1 : 1 ;
118+ } else {
119+ direction = velocityX > 0 ? -1 : 1 ;
120+ }
121+ // get the focused page during the down event to ensure only a single page is changed
122+ float delta = pdfView .isSwipeVertical () ? ev .getY () - downEvent .getY () : ev .getX () - downEvent .getX ();
123+ float offsetX = pdfView .getCurrentXOffset () - delta * pdfView .getZoom ();
124+ float offsetY = pdfView .getCurrentYOffset () - delta * pdfView .getZoom ();
125+ int startingPage = pdfView .findFocusPage (offsetX , offsetY );
126+ int targetPage = Math .max (0 , Math .min (pdfView .getPageCount () - 1 , startingPage + direction ));
127+
128+ SnapEdge edge = pdfView .findSnapEdge (targetPage );
129+ float offset = pdfView .snapOffsetForPage (targetPage , edge );
130+ animationManager .startPageFlingAnimation (-offset );
129131 }
130132
131133 @ Override
@@ -180,6 +182,9 @@ public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float d
180182 private void onScrollEnd (MotionEvent event ) {
181183 pdfView .loadPages ();
182184 hideHandle ();
185+ if (!animationManager .isFlinging ()) {
186+ pdfView .performPageSnap ();
187+ }
183188 }
184189
185190 @ Override
@@ -192,6 +197,15 @@ public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float ve
192197 if (!pdfView .isSwipeEnabled ()) {
193198 return false ;
194199 }
200+ if (pdfView .doPageFling ()) {
201+ if (pdfView .pageFillsScreen ()) {
202+ onBoundedFling (velocityX , velocityY );
203+ } else {
204+ startPageFling (e1 , e2 , velocityX , velocityY );
205+ }
206+ return true ;
207+ }
208+
195209 int xOffset = (int ) pdfView .getCurrentXOffset ();
196210 int yOffset = (int ) pdfView .getCurrentYOffset ();
197211
@@ -207,10 +221,34 @@ public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float ve
207221
208222 animationManager .startFlingAnimation (xOffset , yOffset , (int ) (velocityX ), (int ) (velocityY ),
209223 (int ) minX , 0 , (int ) minY , 0 );
210-
211224 return true ;
212225 }
213226
227+ private void onBoundedFling (float velocityX , float velocityY ) {
228+ int xOffset = (int ) pdfView .getCurrentXOffset ();
229+ int yOffset = (int ) pdfView .getCurrentYOffset ();
230+
231+ PdfFile pdfFile = pdfView .pdfFile ;
232+
233+ float pageStart = -pdfFile .getPageOffset (pdfView .getCurrentPage (), pdfView .getZoom ());
234+ float pageEnd = pageStart - pdfFile .getPageLength (pdfView .getCurrentPage (), pdfView .getZoom ());
235+ float minX , minY , maxX , maxY ;
236+ if (pdfView .isSwipeVertical ()) {
237+ minX = -(pdfView .toCurrentScale (pdfFile .getMaxPageWidth ()) - pdfView .getWidth ());
238+ minY = pageEnd + pdfView .getHeight ();
239+ maxX = 0 ;
240+ maxY = pageStart ;
241+ } else {
242+ minX = pageEnd + pdfView .getWidth ();
243+ minY = -(pdfView .toCurrentScale (pdfFile .getMaxPageHeight ()) - pdfView .getHeight ());
244+ maxX = pageStart ;
245+ maxY = 0 ;
246+ }
247+
248+ animationManager .startFlingAnimation (xOffset , yOffset , (int ) (velocityX ), (int ) (velocityY ),
249+ (int ) minX , (int ) maxX , (int ) minY , (int ) maxY );
250+ }
251+
214252 @ Override
215253 public boolean onScale (ScaleGestureDetector detector ) {
216254 float dr = detector .getScaleFactor ();
@@ -261,4 +299,10 @@ private void hideHandle() {
261299 scrollHandle .hideDelayed ();
262300 }
263301 }
302+
303+ private boolean checkDoPageFling (float velocityX , float velocityY ) {
304+ float absX = Math .abs (velocityX );
305+ float absY = Math .abs (velocityY );
306+ return pdfView .isSwipeVertical () ? absY > absX : absX > absY ;
307+ }
264308}
0 commit comments