Skip to content

Commit f0ce905

Browse files
committed
Improve fling implementation and experience
Update README and CHANGELOG Update version
1 parent 7fd803a commit f0ce905

File tree

6 files changed

+54
-68
lines changed

6 files changed

+54
-68
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.6.0 (2017-06-04)
2+
* Fix fling on single-page documents
3+
* Greatly improve overall fling experience
4+
15
## 2.5.1 (2017-04-08)
26
* Temporarily downgrade PdfiumAndroid until #253 will be fixed
37

README.md

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,9 @@ Library for displaying PDF documents on Android, with `animations`, `gestures`,
1010
It is based on [PdfiumAndroid](https://github.com/barteksc/PdfiumAndroid) for decoding PDF files. Works on API 11 (Android 3.0) and higher.
1111
Licensed under Apache License 2.0.
1212

13-
## What's new in 2.5.0?
14-
* Update PdfiumAndroid to 1.6.0, which is based on newest Pdfium from Android 7.1.1. It should fix many rendering and fonts problems
15-
* Add method `pdfView.fitToWidth()`, which called in `OnRenderListener.onInitiallyRendered()` will fit document to width of the screen (inspired by [1stmetro](https://github.com/1stmetro))
16-
* Add change from pull request by [isanwenyu](https://github.com/isanwenyu) to get rid of rare IllegalArgumentException while rendering
17-
* Add `OnRenderListener`, that will be called once, right before document is drawn on the screen
18-
* Add `Configurator.enableAntialiasing()` to improve rendering on low-res screen a little bit (as suggested by [majkimester](majkimester))
19-
* Modify engine to not block UI when big documents are loaded
20-
* Change `Constants` interface and inner interfaces to static public classes, to allow modifying core config values
21-
22-
Version 2.5.1 temporarily downgrades PdfiumAndroid until #253 will be fixed
13+
## What's new in 2.6.0?
14+
* Fix fling on single-page documents
15+
* Greatly improve overall fling experience
2316

2417
## Changes in 2.0 API
2518
* `Configurator#defaultPage(int)` and `PDFView#jumpTo(int)` now require page index (i.e. starting from 0)
@@ -34,7 +27,7 @@ Version 2.5.1 temporarily downgrades PdfiumAndroid until #253 will be fixed
3427

3528
Add to _build.gradle_:
3629

37-
`compile 'com.github.barteksc:android-pdf-viewer:2.5.1'`
30+
`compile 'com.github.barteksc:android-pdf-viewer:2.6.0'`
3831

3932
Library is available in jcenter repository, probably it'll be in Maven Central soon.
4033

android-pdf-viewer/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ ext {
1313
siteUrl = 'https://github.com/barteksc/AndroidPdfViewer'
1414
gitUrl = 'https://github.com/barteksc/AndroidPdfViewer.git'
1515

16-
libraryVersion = '2.5.1'
16+
libraryVersion = '2.6.0'
1717

1818
developerId = 'barteksc'
1919
developerName = 'Bartosz Schiller'
@@ -32,7 +32,7 @@ android {
3232
minSdkVersion 11
3333
targetSdkVersion 25
3434
versionCode 1
35-
versionName "2.5.1"
35+
versionName "2.6.0"
3636
}
3737

3838
}

android-pdf-viewer/src/main/java/com/github/barteksc/pdfviewer/AnimationManager.java

Lines changed: 8 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import android.animation.ValueAnimator.AnimatorUpdateListener;
2222
import android.graphics.PointF;
2323
import android.view.animation.DecelerateInterpolator;
24-
import android.widget.Scroller;
24+
import android.widget.OverScroller;
2525

2626

2727
/**
@@ -36,13 +36,11 @@ class AnimationManager {
3636

3737
private ValueAnimator animation;
3838

39-
private Scroller scroller;
40-
41-
private ValueAnimator flingAnimation;
39+
private OverScroller scroller;
4240

4341
public AnimationManager(PDFView pdfView) {
4442
this.pdfView = pdfView;
45-
scroller = new Scroller(pdfView.getContext(), null, true);
43+
scroller = new OverScroller(pdfView.getContext());
4644
}
4745

4846
public void startXAnimation(float xFrom, float xTo) {
@@ -76,13 +74,7 @@ public void startZoomAnimation(float centerX, float centerY, float zoomFrom, flo
7674

7775
public void startFlingAnimation(int startX, int startY, int velocityX, int velocityY, int minX, int maxX, int minY, int maxY) {
7876
stopAll();
79-
flingAnimation = ValueAnimator.ofFloat(0, 1);
80-
FlingAnimation flingAnim = new FlingAnimation();
81-
flingAnimation.addUpdateListener(flingAnim);
82-
flingAnimation.addListener(flingAnim);
8377
scroller.fling(startX, startY, velocityX, velocityY, minX, maxX, minY, maxY);
84-
flingAnimation.setDuration(scroller.getDuration());
85-
flingAnimation.start();
8678
}
8779

8880
public void stopAll() {
@@ -94,11 +86,11 @@ public void stopAll() {
9486
}
9587

9688
public void stopFling() {
97-
if (flingAnimation != null) {
98-
scroller.forceFinished(true);
99-
flingAnimation.cancel();
100-
flingAnimation = null;
101-
}
89+
scroller.forceFinished(true);
90+
}
91+
92+
public OverScroller getScroller() {
93+
return scroller;
10294
}
10395

10496
class XAnimation implements AnimatorUpdateListener {
@@ -157,38 +149,6 @@ public void onAnimationStart(Animator animation) {
157149

158150
}
159151

160-
class FlingAnimation implements AnimatorUpdateListener, AnimatorListener {
161-
@Override
162-
public void onAnimationUpdate(ValueAnimator animation) {
163-
if (!scroller.isFinished()) {
164-
scroller.computeScrollOffset();
165-
pdfView.moveTo(scroller.getCurrX(), scroller.getCurrY());
166-
pdfView.loadPageByOffset();
167-
}
168-
}
169-
170-
@Override
171-
public void onAnimationStart(Animator animation) {
172-
173-
}
174-
175-
@Override
176-
public void onAnimationEnd(Animator animation) {
177-
pdfView.loadPages();
178-
hideHandle();
179-
}
180-
181-
@Override
182-
public void onAnimationCancel(Animator animation) {
183-
184-
}
185-
186-
@Override
187-
public void onAnimationRepeat(Animator animation) {
188-
189-
}
190-
}
191-
192152
private void hideHandle() {
193153
if (pdfView.getScrollHandle() != null) {
194154
pdfView.getScrollHandle().hideDelayed();

android-pdf-viewer/src/main/java/com/github/barteksc/pdfviewer/DragPinchManager.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float d
133133
pdfView.moveRelativeTo(-distanceX, -distanceY);
134134
}
135135
if (!scaling || pdfView.doRenderDuringScale()) {
136-
pdfView.loadPageByOffset();
136+
pdfView.loadPageByOffset();
137137
}
138138
return true;
139139
}
@@ -152,11 +152,19 @@ public void onLongPress(MotionEvent e) {
152152
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
153153
int xOffset = (int) pdfView.getCurrentXOffset();
154154
int yOffset = (int) pdfView.getCurrentYOffset();
155-
animationManager.startFlingAnimation(xOffset,
156-
yOffset, (int) (velocityX),
157-
(int) (velocityY),
158-
xOffset * (swipeVertical ? 2 : pdfView.getPageCount()), 0,
159-
yOffset * (swipeVertical ? pdfView.getPageCount() : 2), 0);
155+
156+
float minX = pdfView.toCurrentScale(pdfView.getOptimalPageWidth());
157+
float minY = pdfView.toCurrentScale(pdfView.getOptimalPageHeight());
158+
if (pdfView.isSwipeVertical()) {
159+
minX = -(minX - pdfView.getWidth());
160+
minY = -(minY * pdfView.getPageCount() - pdfView.getHeight());
161+
} else {
162+
minX = -(minX * pdfView.getPageCount() - pdfView.getWidth());
163+
minY = -(minY - pdfView.getHeight());
164+
}
165+
166+
animationManager.startFlingAnimation(xOffset, yOffset, (int) (velocityX), (int) (velocityY),
167+
(int) minX, 0, (int) minY, 0);
160168

161169
return true;
162170
}

android-pdf-viewer/src/main/java/com/github/barteksc/pdfviewer/PDFView.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import android.os.HandlerThread;
3232
import android.util.AttributeSet;
3333
import android.util.Log;
34+
import android.widget.OverScroller;
3435
import android.widget.RelativeLayout;
3536

3637
import com.github.barteksc.pdfviewer.listener.OnDrawListener;
@@ -286,7 +287,7 @@ ScrollHandle getScrollHandle() {
286287
*/
287288
private boolean enableAntialiasing = true;
288289
private PaintFlagsDrawFilter antialiasFilter =
289-
new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG);
290+
new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
290291

291292
/**
292293
* Construct the initial view
@@ -521,6 +522,25 @@ public boolean isRecycled() {
521522
return recycled;
522523
}
523524

525+
/**
526+
* Handle fling animation
527+
*/
528+
@Override
529+
public void computeScroll() {
530+
super.computeScroll();
531+
532+
OverScroller scroller = animationManager.getScroller();
533+
if (scroller.computeScrollOffset()) {
534+
moveTo(scroller.getCurrX(), scroller.getCurrY());
535+
loadPageByOffset();
536+
} else { // fling finished
537+
loadPages();
538+
if (getScrollHandle() != null) {
539+
getScrollHandle().hideDelayed();
540+
}
541+
}
542+
}
543+
524544
@Override
525545
protected void onDetachedFromWindow() {
526546
recycle();
@@ -1219,6 +1239,7 @@ public Configurator fromUri(Uri uri) {
12191239

12201240
/**
12211241
* Use bytearray as the pdf source, documents is not saved
1242+
*
12221243
* @param bytes
12231244
* @return
12241245
*/

0 commit comments

Comments
 (0)