Skip to content

Commit

Permalink
Remove SnapPolicy from API and auto-detect edge to snap to.
Browse files Browse the repository at this point in the history
Now also properly works when zoomed in and with the different FitPolicies. Close DImuthuUpe#5
  • Loading branch information
Flamedek committed Mar 24, 2018
1 parent c08ac9b commit a72b7cc
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 73 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ pdfView.fromAsset(String)
.autoSpacing(false) // add dynamic spacing to fit each page on its own on the screen
.linkHandler(DefaultLinkHandler)
.pageFitPolicy(FitPolicy.WIDTH)
// choose of start, center or end. Pages are snapped to this edge after scroll.
.snapPolicy(SnapPolicy.NONE)
.pageSnap(true) // snap pages to screen boundaries
.pageFling(false) // make a fling change only a single page like ViewPager
.load();
```
Expand Down Expand Up @@ -211,10 +210,10 @@ Configurator.onRender(new OnRenderListener() {
```

### How can I scroll through single pages like a ViewPager?
You can use a combination of the following settings to get scroll and fling behaviour similiar to a ViewPager:
You can use a combination of the following settings to get scroll and fling behaviour similar to a ViewPager:
``` java
.swipeHorizontal(true)
.snapPolicy(SnapPolicy.CENTER)
.pageSnap(true)
.autoSpacing(true)
.pageFling(true)
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ void computeFling() {
flinging = false;
pdfView.loadPages();
hideHandle();
if (!pdfView.isZooming()) {
pdfView.doPageSnap();
}
pdfView.performPageSnap();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import com.github.barteksc.pdfviewer.model.LinkTapEvent;
import com.github.barteksc.pdfviewer.scroll.ScrollHandle;
import com.github.barteksc.pdfviewer.util.SnapEdge;
import com.shockwave.pdfium.PdfDocument;
import com.shockwave.pdfium.util.SizeF;

Expand Down Expand Up @@ -121,10 +122,11 @@ private void startPageFling(MotionEvent downEvent, MotionEvent ev, float velocit
float delta = pdfView.isSwipeVertical() ? ev.getY() - downEvent.getY() : ev.getX() - downEvent.getX();
float offsetX = pdfView.getCurrentXOffset() - delta * pdfView.getZoom();
float offsetY = pdfView.getCurrentYOffset() - delta * pdfView.getZoom();
int startingPage = pdfView.findPageToSnap(offsetX, offsetY);
int startingPage = pdfView.findCenterPage(offsetX, offsetY);
int targetPage = Math.max(0, Math.min(pdfView.getPageCount() - 1, startingPage + direction));

float offset = pdfView.snapOffsetForPage(targetPage);
SnapEdge edge = pdfView.findSnapEdge(targetPage);
float offset = pdfView.snapOffsetForPage(targetPage, edge);
animationManager.startPageFlingAnimation(-offset);
}

Expand Down Expand Up @@ -180,8 +182,8 @@ public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float d
private void onScrollEnd(MotionEvent event) {
pdfView.loadPages();
hideHandle();
if (!animationManager.isFlinging() && !pdfView.pageFillsScreen()) {
pdfView.doPageSnap();
if (!animationManager.isFlinging()) {
pdfView.performPageSnap();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
import com.github.barteksc.pdfviewer.util.Constants;
import com.github.barteksc.pdfviewer.util.FitPolicy;
import com.github.barteksc.pdfviewer.util.MathUtils;
import com.github.barteksc.pdfviewer.util.SnapPolicy;
import com.github.barteksc.pdfviewer.util.SnapEdge;
import com.github.barteksc.pdfviewer.util.Util;
import com.shockwave.pdfium.PdfDocument;
import com.shockwave.pdfium.PdfiumCore;
Expand Down Expand Up @@ -168,8 +168,6 @@ enum ScrollDir {
/** Policy for fitting pages to screen */
private FitPolicy pageFitPolicy = FitPolicy.WIDTH;

private SnapPolicy snapPolicy = SnapPolicy.NONE;

private int defaultPage = 0;

/** True if should scroll through pages vertically instead of horizontally */
Expand All @@ -179,6 +177,8 @@ enum ScrollDir {

private boolean doubletapEnabled = true;

private boolean pageSnap = true;

/** Pdfium core for loading and rendering PDFs */
private PdfiumCore pdfiumCore;

Expand Down Expand Up @@ -874,13 +874,17 @@ void loadPageByOffset() {
/**
* Animate to the nearest snapping position for the current SnapPolicy
*/
public void doPageSnap() {
if (snapPolicy == SnapPolicy.NONE || pdfFile == null || pdfFile.getPagesCount() == 0) {
public void performPageSnap() {
if (!pageSnap || pdfFile == null || pdfFile.getPagesCount() == 0) {
return;
}
int centerPage = findCenterPage(currentXOffset, currentYOffset);
SnapEdge edge = findSnapEdge(centerPage);
if (edge == SnapEdge.NONE) {
return;
}
int targetPage = findPageToSnap(currentXOffset, currentYOffset);
float offset = snapOffsetForPage(targetPage);

float offset = snapOffsetForPage(centerPage, edge);
if (swipeVertical) {
animationManager.startYAnimation(currentYOffset, -offset);
} else {
Expand All @@ -889,58 +893,48 @@ public void doPageSnap() {
}

/**
* Get the offset to move to in order to snap to the specified page
* Find the edge to snap to when showing the specified page
*/
float snapOffsetForPage(int pageIndex) {
SnapEdge findSnapEdge(int page) {
if (!pageSnap || page < 0) {
return SnapEdge.NONE;
}
float currentOffset = swipeVertical ? currentYOffset : currentXOffset;
float offset = -pdfFile.getPageOffset(page, zoom);
int length = swipeVertical ? getHeight() : getWidth();
float pageLength = pdfFile.getPageLength(page, zoom);

if (length >= pageLength) {
return SnapEdge.CENTER;
} else if (currentOffset >= offset) {
return SnapEdge.START;
} else if (offset - pageLength > currentOffset - length) {
return SnapEdge.END;
} else {
return SnapEdge.NONE;
}
}

/**
* Get the offset to move to in order to snap to the page
*/
float snapOffsetForPage(int pageIndex, SnapEdge edge) {
float offset = pdfFile.getPageOffset(pageIndex, zoom);

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

if (snapPolicy == SnapPolicy.CENTER) {
offset = offset - size / 2f + pageSize / 2f;
} else if (snapPolicy == SnapPolicy.END) {
offset = offset - size + pageSize;
if (edge == SnapEdge.CENTER) {
offset = offset - length / 2f + pageLength / 2f;
} else if (edge == SnapEdge.END) {
offset = offset - length + pageLength;
}
return offset;
}

int findPageToSnap(float xOffset, float yOffset) {
switch (snapPolicy) {
case CENTER:
// get page at center
float center = swipeVertical ? yOffset - getHeight() / 2f : xOffset - getWidth() / 2f;
return pdfFile.getPageAtOffset(-center, zoom);
case START:
// get page at starting edge
float start = swipeVertical ? yOffset : xOffset;
int startIndex = pdfFile.getPageAtOffset(-start, zoom);
if (startIndex < getPageCount() - 1) {
// check if next page start is actually closer
float pageStart = -pdfFile.getPageOffset(startIndex, zoom);
float nextPageStart = -pdfFile.getPageOffset(startIndex + 1, zoom);
if (start - pageStart < nextPageStart - start) {
return startIndex + 1;
}
}
return startIndex;
case END:
// get page at ending edge
float end = swipeVertical ? yOffset - getHeight() : xOffset - getWidth();
int endIndex = pdfFile.getPageAtOffset(-end, zoom);
if (endIndex > 0) {
// check if previous page is actually closer
float pageEnd = -pdfFile.getPageOffset(endIndex, zoom) - pdfFile.getPageLength(endIndex, zoom);
float prevPageEnd = -pdfFile.getPageOffset(endIndex - 1, zoom) -
pdfFile.getPageLength(endIndex - 1, zoom);
if (end - pageEnd > prevPageEnd - end) {
return endIndex - 1;
}
}
return endIndex;
default:
return -1;
}
int findCenterPage(float xOffset, float yOffset) {
float center = swipeVertical ? yOffset - getHeight() / 2f : xOffset - getWidth() / 2f;
return pdfFile.getPageAtOffset(-center, zoom);
}

/**
Expand Down Expand Up @@ -1187,12 +1181,12 @@ public FitPolicy getPageFitPolicy() {
return pageFitPolicy;
}

public void setSnapPolicy(SnapPolicy snapPolicy) {
this.snapPolicy = snapPolicy;
public boolean doPageSnap() {
return pageSnap;
}

public SnapPolicy getSnapPolicy() {
return snapPolicy;
public void setPageSnap(boolean pageSnap) {
this.pageSnap = pageSnap;
}

public boolean doRenderDuringScale() {
Expand Down Expand Up @@ -1303,10 +1297,10 @@ public class Configurator {

private FitPolicy pageFitPolicy = FitPolicy.WIDTH;

private SnapPolicy snapPolicy = SnapPolicy.NONE;

private boolean pageFling = false;

private boolean pageSnap = false;

private Configurator(DocumentSource documentSource) {
this.documentSource = documentSource;
}
Expand Down Expand Up @@ -1421,8 +1415,8 @@ public Configurator pageFitPolicy(FitPolicy pageFitPolicy) {
return this;
}

public Configurator snapPolicy(SnapPolicy snapPolicy) {
this.snapPolicy = snapPolicy;
public Configurator pageSnap(boolean pageSnap) {
this.pageSnap = pageSnap;
return this;
}

Expand Down Expand Up @@ -1457,7 +1451,7 @@ public void load() {
PDFView.this.setSpacing(spacing);
PDFView.this.setAutoSpacing(autoSpacing);
PDFView.this.setPageFitPolicy(pageFitPolicy);
PDFView.this.setSnapPolicy(snapPolicy);
PDFView.this.setPageSnap(pageSnap);
PDFView.this.setPageFling(pageFling);

if (pageNumbers != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public boolean onTouchEvent(MotionEvent event) {
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
hideDelayed();
pdfView.doPageSnap();
pdfView.performPageSnap();
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
*/
package com.github.barteksc.pdfviewer.util;

public enum SnapPolicy {
public enum SnapEdge {
START, CENTER, END, NONE
}

0 comments on commit a72b7cc

Please sign in to comment.