Skip to content

Commit

Permalink
Make fling bound to page when single page fills screen with pageFling…
Browse files Browse the repository at this point in the history
… enabled.

Close DImuthuUpe#2 and DImuthuUpe#3
  • Loading branch information
Flamedek committed Mar 20, 2018
1 parent ede8331 commit c08ac9b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.ViewConfiguration;

import com.github.barteksc.pdfviewer.model.LinkTapEvent;
import com.github.barteksc.pdfviewer.scroll.ScrollHandle;
Expand Down Expand Up @@ -181,7 +180,7 @@ public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float d
private void onScrollEnd(MotionEvent event) {
pdfView.loadPages();
hideHandle();
if (!pdfView.isZooming() && !animationManager.isFlinging()) {
if (!animationManager.isFlinging() && !pdfView.pageFillsScreen()) {
pdfView.doPageSnap();
}
}
Expand All @@ -196,10 +195,15 @@ public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float ve
if (!pdfView.isSwipeEnabled()) {
return false;
}
if (!pdfView.isZooming() && pdfView.doPageFling()) {
startPageFling(e1, e2, velocityX, velocityY);
if (pdfView.doPageFling()) {
if (pdfView.pageFillsScreen()) {
onBoundedFling(velocityX, velocityY);
} else {
startPageFling(e1, e2, velocityX, velocityY);
}
return true;
}

int xOffset = (int) pdfView.getCurrentXOffset();
int yOffset = (int) pdfView.getCurrentYOffset();

Expand All @@ -218,6 +222,31 @@ public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float ve
return true;
}

private void onBoundedFling(float velocityX, float velocityY) {
int xOffset = (int) pdfView.getCurrentXOffset();
int yOffset = (int) pdfView.getCurrentYOffset();

PdfFile pdfFile = pdfView.pdfFile;

float pageStart = -pdfFile.getPageOffset(pdfView.getCurrentPage(), pdfView.getZoom());
float pageEnd = pageStart - pdfFile.getPageLength(pdfView.getCurrentPage(), pdfView.getZoom());
float minX, minY, maxX, maxY;
if (pdfView.isSwipeVertical()) {
minX = -(pdfView.toCurrentScale(pdfFile.getMaxPageWidth()) - pdfView.getWidth());
minY = pageEnd + pdfView.getHeight();
maxX = 0;
maxY = pageStart;
} else {
minX = pageEnd + pdfView.getWidth();
minY = -(pdfView.toCurrentScale(pdfFile.getMaxPageHeight()) - pdfView.getHeight());
maxX = pageStart;
maxY = 0;
}

animationManager.startFlingAnimation(xOffset, yOffset, (int) (velocityX), (int) (velocityY),
(int) minX, (int) maxX, (int) minY, (int) maxY);
}

@Override
public boolean onScale(ScaleGestureDetector detector) {
float dr = detector.getScaleFactor();
Expand Down Expand Up @@ -270,11 +299,8 @@ private void hideHandle() {
}

private boolean checkDoPageFling(float velocityX, float velocityY) {
int minVelocity = ViewConfiguration.get(pdfView.getContext()).getScaledMinimumFlingVelocity();
if (pdfView.isSwipeVertical()) {
return Math.abs(velocityY) > minVelocity;
} else {
return Math.abs(velocityX) > minVelocity;
}
float absX = Math.abs(velocityX);
float absY = Math.abs(velocityY);
return pdfView.isSwipeVertical() ? absY > absX : absX > absY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ float snapOffsetForPage(int pageIndex) {
float offset = pdfFile.getPageOffset(pageIndex, zoom);

float size = swipeVertical ? getHeight() : getWidth();
float pageSize = getPageLength(pageIndex, zoom);
float pageSize = pdfFile.getPageLength(pageIndex, zoom);

if (snapPolicy == SnapPolicy.CENTER) {
offset = offset - size / 2f + pageSize / 2f;
Expand Down Expand Up @@ -930,9 +930,9 @@ int findPageToSnap(float xOffset, float yOffset) {
int endIndex = pdfFile.getPageAtOffset(-end, zoom);
if (endIndex > 0) {
// check if previous page is actually closer
float pageEnd = -pdfFile.getPageOffset(endIndex, zoom) - getPageLength(endIndex, zoom);
float pageEnd = -pdfFile.getPageOffset(endIndex, zoom) - pdfFile.getPageLength(endIndex, zoom);
float prevPageEnd = -pdfFile.getPageOffset(endIndex - 1, zoom) -
getPageLength(endIndex - 1, zoom);
pdfFile.getPageLength(endIndex - 1, zoom);
if (end - pageEnd > prevPageEnd - end) {
return endIndex - 1;
}
Expand All @@ -944,11 +944,16 @@ int findPageToSnap(float xOffset, float yOffset) {
}

/**
* Get the page's height if swiping vertical, or width if swiping horizontal.
* @return true if single page fills the entire screen in the scrolling direction
*/
private float getPageLength(int pageIndex, float zoom) {
SizeF size = pdfFile.getPageSize(pageIndex);
return (swipeVertical ? size.getHeight() : size.getWidth()) * zoom;
public boolean pageFillsScreen() {
float start = -pdfFile.getPageOffset(currentPage, zoom);
float end = start - pdfFile.getPageLength(currentPage, zoom);
if (isSwipeVertical()) {
return start > currentYOffset && end < currentYOffset - getHeight();
} else {
return start > currentXOffset && end < currentXOffset - getWidth();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@ public float getDocLen(float zoom) {
return documentLength * zoom;
}

/**
* Get the page's height if swiping vertical, or width if swiping horizontal.
*/
public float getPageLength(int pageIndex, float zoom) {
SizeF size = getPageSize(pageIndex);
return (isVertical ? size.getHeight() : size.getWidth()) * zoom;
}

public float getPageSpacing(int pageIndex, float zoom) {
float spacing = autoSpacing ? pageSpacing.get(pageIndex) : spacingPx;
return spacing * zoom;
Expand Down Expand Up @@ -356,5 +364,4 @@ public int documentPage(int userPage) {

return documentPage;
}

}

0 comments on commit c08ac9b

Please sign in to comment.