-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
//apply plugin: 'java' | ||
apply plugin: 'com.android.library' | ||
|
||
ext { | ||
|
@@ -26,7 +27,7 @@ ext { | |
|
||
android { | ||
compileSdkVersion 25 | ||
buildToolsVersion '25.0.3' | ||
//buildToolsVersion '25.0.3' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove comment |
||
|
||
defaultConfig { | ||
minSdkVersion 11 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add braces to |
||
pdfView.loadPages(); | ||
hideHandle(); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add braces to |
||
pdfView.loadPages(); | ||
hideHandle(); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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 */ | ||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add braces to |
||
if (swipeVertical) { | ||
if (withAnimation) { | ||
animationManager.startYAnimation(currentYOffset, offset); | ||
|
@@ -706,6 +712,9 @@ void loadComplete(PdfFile pdfFile) { | |
|
||
callbacks.callOnLoadComplete(pdfFile.getPagesCount()); | ||
|
||
if (getPageViewType() == PageViewType.SINGLE) | ||
spacingPx = pdfFile.getSpacingPx(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add braces to |
||
|
||
jumpTo(defaultPage, false); | ||
} | ||
|
||
|
@@ -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; | ||
} | ||
|
@@ -1183,6 +1200,8 @@ public class Configurator { | |
|
||
private FitPolicy pageFitPolicy = FitPolicy.WIDTH; | ||
|
||
private PageViewType pageViewType = PageViewType.CONTINUOUS; | ||
|
||
private Configurator(DocumentSource documentSource) { | ||
this.documentSource = documentSource; | ||
} | ||
|
@@ -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; | ||
|
@@ -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); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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); | ||
} | ||
|
||
|
@@ -113,7 +116,7 @@ public void recalculatePageSizes(Size viewSize) { | |
} | ||
|
||
prepareDocLen(); | ||
preparePagesOffset(); | ||
preparePagesOffset(viewSize); | ||
} | ||
|
||
public int getPagesCount() { | ||
|
@@ -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) | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add braces to |
||
else { | ||
SizeF pageSize = getPageSize(0); | ||
int offset = (int)(viewSize.getWidth() - pageSize.getWidth()) / 2; | ||
spacingPx = offset+1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please format this line |
||
prepareDocLen(); | ||
documentLength += spacingPx*2; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add braces to |
||
pageOffsets.add(offset + spacing); | ||
SizeF size = pageSizes.get(i); | ||
offset += isVertical ? size.getHeight() : size.getWidth(); | ||
|
@@ -207,6 +226,11 @@ public int getPageAtOffset(float offset, float zoom) { | |
return --currentPage >= 0 ? currentPage : 0; | ||
} | ||
|
||
public int getSpacingPx() | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,11 +13,11 @@ repositories { | |
} | ||
|
||
apply plugin: 'com.android.application' | ||
apply plugin: 'android-apt' | ||
//apply plugin: 'android-apt' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove comment |
||
|
||
defaultConfig { | ||
minSdkVersion 11 | ||
|
@@ -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 |
---|---|---|
|
@@ -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; | ||
|
@@ -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, | ||
|
@@ -102,7 +105,7 @@ void launchPicker() { | |
|
||
@AfterViews | ||
void afterViews() { | ||
pdfView.setBackgroundColor(Color.LTGRAY); | ||
pdfView.setBackgroundColor(Color.BLACK); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
@@ -114,7 +117,7 @@ void afterViews() { | |
private void displayFromAsset(String assetFileName) { | ||
pdfFileName = assetFileName; | ||
|
||
pdfView.fromAsset(SAMPLE_FILE) | ||
/*pdfView.fromAsset(SAMPLE_FILE) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove comment |
||
.defaultPage(pageNumber) | ||
.onPageChange(this) | ||
.enableAnnotationRendering(true) | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove comment