Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single page scrolling #514

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion android-pdf-viewer/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//apply plugin: 'java'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove comment

apply plugin: 'com.android.library'

ext {
Expand Down Expand Up @@ -26,7 +27,7 @@ ext {

android {
compileSdkVersion 25
buildToolsVersion '25.0.3'
//buildToolsVersion '25.0.3'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove comment


defaultConfig {
minSdkVersion 11
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import android.view.animation.DecelerateInterpolator;
import android.widget.OverScroller;

import com.github.barteksc.pdfviewer.util.PageViewType;


/**
* This manager is used by the PDFView to launch animations.
Expand Down Expand Up @@ -171,6 +173,8 @@ public void onAnimationCancel(Animator animation) {

@Override
public void onAnimationEnd(Animator animation) {
if (pdfView.getPageViewType() == PageViewType.SINGLE && pdfView.getZoom() == pdfView.getMinZoom())
pdfView.jumpTo(pdfView.getCurrentPage(), true);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add braces to if block

pdfView.loadPages();
hideHandle();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected Throwable doInBackground(Void... params) {
try {
PdfDocument pdfDocument = docSource.createDocument(pdfView.getContext(), pdfiumCore, password);
pdfFile = new PdfFile(pdfiumCore, pdfDocument, pdfView.getPageFitPolicy(), getViewSize(),
userPages, pdfView.isSwipeVertical(), pdfView.getSpacingPx());
userPages, pdfView.isSwipeVertical(), pdfView.getSpacingPx(), pdfView.getPageViewType());
return null;
} catch (Throwable t) {
return t;
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.PageViewType;
import com.shockwave.pdfium.PdfDocument;
import com.shockwave.pdfium.util.SizeF;

Expand Down Expand Up @@ -178,6 +179,8 @@ public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float d
}

private void onScrollEnd(MotionEvent event) {
if (pdfView.getPageViewType() == PageViewType.SINGLE && !pdfView.isZooming())
pdfView.jumpTo(pdfView.getCurrentPage(), true);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add braces to if block

pdfView.loadPages();
hideHandle();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +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.PageViewType;
import com.github.barteksc.pdfviewer.util.Util;
import com.shockwave.pdfium.PdfDocument;
import com.shockwave.pdfium.PdfiumCore;
Expand Down Expand Up @@ -167,6 +168,9 @@ enum ScrollDir {
/** Policy for fitting pages to screen */
private FitPolicy pageFitPolicy = FitPolicy.WIDTH;

/** How the page should move when swiping. Continuously or one at a time */
private PageViewType pageViewType = PageViewType.CONTINUOUS;

private int defaultPage = 0;

/** True if should scroll through pages vertically instead of horizontally */
Expand Down Expand Up @@ -275,6 +279,8 @@ public void jumpTo(int page, boolean withAnimation) {

page = pdfFile.determineValidPageNumberFrom(page);
float offset = -pdfFile.getPageOffset(page, zoom);
if (getPageViewType() == PageViewType.SINGLE)
offset = page == 0 ? 0 : -pdfFile.getPageOffset(page, zoom) + spacingPx;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add braces to if block

if (swipeVertical) {
if (withAnimation) {
animationManager.startYAnimation(currentYOffset, offset);
Expand Down Expand Up @@ -706,6 +712,9 @@ void loadComplete(PdfFile pdfFile) {

callbacks.callOnLoadComplete(pdfFile.getPagesCount());

if (getPageViewType() == PageViewType.SINGLE)
spacingPx = pdfFile.getSpacingPx();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add braces to if block


jumpTo(defaultPage, false);
}

Expand Down Expand Up @@ -1077,6 +1086,14 @@ public FitPolicy getPageFitPolicy() {
return pageFitPolicy;
}

private void setPageViewType(PageViewType pageViewType) {
this.pageViewType = pageViewType;
}

public PageViewType getPageViewType() {
return pageViewType;
}

public boolean doRenderDuringScale() {
return renderDuringScale;
}
Expand Down Expand Up @@ -1183,6 +1200,8 @@ public class Configurator {

private FitPolicy pageFitPolicy = FitPolicy.WIDTH;

private PageViewType pageViewType = PageViewType.CONTINUOUS;

private Configurator(DocumentSource documentSource) {
this.documentSource = documentSource;
}
Expand Down Expand Up @@ -1292,6 +1311,11 @@ public Configurator pageFitPolicy(FitPolicy pageFitPolicy) {
return this;
}

public Configurator pageViewType(PageViewType pageViewType) {
this.pageViewType = pageViewType;
return this;
}

public void load() {
if (!hasSize) {
waitingDocumentConfigurator = this;
Expand All @@ -1317,6 +1341,7 @@ public void load() {
PDFView.this.enableAntialiasing(antialiasing);
PDFView.this.setSpacing(spacing);
PDFView.this.setPageFitPolicy(pageFitPolicy);
PDFView.this.setPageViewType(pageViewType);

if (pageNumbers != null) {
PDFView.this.load(documentSource, password, pageNumbers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.github.barteksc.pdfviewer.exception.PageRenderingException;
import com.github.barteksc.pdfviewer.util.FitPolicy;
import com.github.barteksc.pdfviewer.util.PageSizeCalculator;
import com.github.barteksc.pdfviewer.util.PageViewType;
import com.shockwave.pdfium.PdfDocument;
import com.shockwave.pdfium.PdfiumCore;
import com.shockwave.pdfium.util.Size;
Expand Down Expand Up @@ -58,20 +59,22 @@ class PdfFile {
/** Calculated document length (width or height, depending on swipe mode) */
private float documentLength = 0;
private final FitPolicy pageFitPolicy;
private final PageViewType pageViewType;
/**
* The pages the user want to display in order
* (ex: 0, 2, 2, 8, 8, 1, 1, 1)
*/
private int[] originalUserPages;

PdfFile(PdfiumCore pdfiumCore, PdfDocument pdfDocument, FitPolicy pageFitPolicy, Size viewSize, int[] originalUserPages,
boolean isVertical, int spacing) {
boolean isVertical, int spacing, PageViewType viewType) {
this.pdfiumCore = pdfiumCore;
this.pdfDocument = pdfDocument;
this.pageFitPolicy = pageFitPolicy;
this.originalUserPages = originalUserPages;
this.isVertical = isVertical;
this.spacingPx = spacing;
this.pageViewType = viewType;
setup(viewSize);
}

Expand Down Expand Up @@ -113,7 +116,7 @@ public void recalculatePageSizes(Size viewSize) {
}

prepareDocLen();
preparePagesOffset();
preparePagesOffset(viewSize);
}

public int getPagesCount() {
Expand Down Expand Up @@ -159,11 +162,27 @@ private void prepareDocLen() {
documentLength = length + spacing;
}

private void preparePagesOffset() {
private void preparePagesOffset(Size viewSize) {
pageOffsets.clear();

if (pageViewType == PageViewType.SINGLE && getPagesCount() > 0)
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move brace to previous line

if (pageFitPolicy == FitPolicy.WIDTH || pageFitPolicy == FitPolicy.BOTH)
spacingPx = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add braces to if block

else {
SizeF pageSize = getPageSize(0);
int offset = (int)(viewSize.getWidth() - pageSize.getWidth()) / 2;
spacingPx = offset+1;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please format this line

prepareDocLen();
documentLength += spacingPx*2;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please format this line

}
}

float offset = 0;
for (int i = 0; i < getPagesCount(); i++) {
float spacing = i * spacingPx;
if (pageViewType == PageViewType.SINGLE)
spacing = (i+1) * spacingPx;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add braces to if block

pageOffsets.add(offset + spacing);
SizeF size = pageSizes.get(i);
offset += isVertical ? size.getHeight() : size.getWidth();
Expand Down Expand Up @@ -207,6 +226,11 @@ public int getPageAtOffset(float offset, float zoom) {
return --currentPage >= 0 ? currentPage : 0;
}

public int getSpacingPx()
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move brace to previous line

return spacingPx;
}

public boolean openPage(int pageIndex) throws PageRenderingException {
int docPage = documentPage(pageIndex);
if (docPage < 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.github.barteksc.pdfviewer.util;

/**
* Created by starnaus on 12/22/2017.
*/

public enum PageViewType {
CONTINUOUS, SINGLE
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
}
Expand Down
7 changes: 4 additions & 3 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ repositories {
}

apply plugin: 'com.android.application'
apply plugin: 'android-apt'
//apply plugin: 'android-apt'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove comment


android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
//buildToolsVersion "25.0.3"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove comment


defaultConfig {
minSdkVersion 11
Expand All @@ -31,6 +31,7 @@ android {
dependencies {
compile project(':android-pdf-viewer')
compile 'com.android.support:appcompat-v7:25.3.1'
provided 'org.androidannotations:androidannotations:4.0.0'
annotationProcessor 'org.androidannotations:androidannotations:4.0.0'
//provided 'org.androidannotations:androidannotations:4.0.0'
compile 'org.androidannotations:androidannotations-api:4.0.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.github.barteksc.pdfviewer.listener.OnPageErrorListener;
import com.github.barteksc.pdfviewer.scroll.DefaultScrollHandle;
import com.github.barteksc.pdfviewer.util.FitPolicy;
import com.github.barteksc.pdfviewer.util.PageViewType;
import com.shockwave.pdfium.PdfDocument;

import org.androidannotations.annotations.AfterViews;
Expand All @@ -47,6 +48,8 @@

import java.util.List;

import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;

@EActivity(R.layout.activity_main)
@OptionsMenu(R.menu.options)
public class PDFViewActivity extends AppCompatActivity implements OnPageChangeListener, OnLoadCompleteListener,
Expand Down Expand Up @@ -102,7 +105,7 @@ void launchPicker() {

@AfterViews
void afterViews() {
pdfView.setBackgroundColor(Color.LTGRAY);
pdfView.setBackgroundColor(Color.BLACK);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not modify sample when it isn't needed

if (uri != null) {
displayFromUri(uri);
} else {
Expand All @@ -114,7 +117,7 @@ void afterViews() {
private void displayFromAsset(String assetFileName) {
pdfFileName = assetFileName;

pdfView.fromAsset(SAMPLE_FILE)
/*pdfView.fromAsset(SAMPLE_FILE)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove comment

.defaultPage(pageNumber)
.onPageChange(this)
.enableAnnotationRendering(true)
Expand All @@ -123,7 +126,23 @@ private void displayFromAsset(String assetFileName) {
.spacing(10) // in dp
.onPageError(this)
.pageFitPolicy(FitPolicy.BOTH)
.load();
.load();*/

PDFView.Configurator configurator = pdfView.fromAsset(SAMPLE_FILE);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert it to chained style

configurator.defaultPage(pageNumber);
configurator.onPageChange(this);
configurator.enableAnnotationRendering(true);
configurator.onLoad(this);
//configurator.scrollHandle(new DefaultScrollHandle(this));
//configurator.spacing(300);
configurator.onPageError(this);
configurator.swipeHorizontal(true);
configurator.pageViewType(PageViewType.SINGLE);
if (getResources().getConfiguration().orientation == ORIENTATION_LANDSCAPE)
configurator.pageFitPolicy(FitPolicy.HEIGHT);
else
configurator.pageFitPolicy(FitPolicy.WIDTH);
configurator.load();
}

private void displayFromUri(Uri uri) {
Expand Down