Skip to content
This repository was archived by the owner on Nov 10, 2025. It is now read-only.

Commit c543eba

Browse files
committed
https://github.com/TalbotGooday/AndroidPdfViewer/issues/1
1 parent 0dd93cf commit c543eba

File tree

1 file changed

+115
-40
lines changed
  • android-pdf-viewer/src/main/java/com/github/barteksc/pdfviewer

1 file changed

+115
-40
lines changed

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

Lines changed: 115 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,26 @@ enum ScrollDir {
114114

115115
private ScrollDir scrollDir = ScrollDir.NONE;
116116

117-
/** Rendered parts go to the cache manager */
117+
/**
118+
* Rendered parts go to the cache manager
119+
*/
118120
CacheManager cacheManager;
119121

120-
/** Animation manager manage all offset and zoom animation */
122+
/**
123+
* Animation manager manage all offset and zoom animation
124+
*/
121125
private AnimationManager animationManager;
122126

123-
/** Drag manager manage all touch events */
127+
/**
128+
* Drag manager manage all touch events
129+
*/
124130
private DragPinchManager dragPinchManager;
125131

126132
PdfFile pdfFile;
127133

128-
/** The index of the current sequence */
134+
/**
135+
* The index of the current sequence
136+
*/
129137
private int currentPage;
130138

131139
/**
@@ -142,41 +150,61 @@ enum ScrollDir {
142150
*/
143151
private float currentYOffset = 0;
144152

145-
/** The zoom level, always >= 1 */
153+
/**
154+
* The zoom level, always >= 1
155+
*/
146156
private float zoom = 1f;
147157

148-
/** True if the PDFView has been recycled */
158+
/**
159+
* True if the PDFView has been recycled
160+
*/
149161
private boolean recycled = true;
150162

151-
/** Current state of the view */
163+
/**
164+
* Current state of the view
165+
*/
152166
private State state = State.DEFAULT;
153167

154-
/** Async task used during the loading phase to decode a PDF document */
168+
/**
169+
* Async task used during the loading phase to decode a PDF document
170+
*/
155171
private DecodingAsyncTask decodingAsyncTask;
156172

157-
/** The thread {@link #renderingHandler} will run on */
173+
/**
174+
* The thread {@link #renderingHandler} will run on
175+
*/
158176
private HandlerThread renderingHandlerThread;
159-
/** Handler always waiting in the background and rendering tasks */
177+
/**
178+
* Handler always waiting in the background and rendering tasks
179+
*/
160180
RenderingHandler renderingHandler;
161181

162182
private PagesLoader pagesLoader;
163183

164184
Callbacks callbacks = new Callbacks();
165185

166-
/** Paint object for drawing */
186+
/**
187+
* Paint object for drawing
188+
*/
167189
private Paint paint;
168190

169-
/** Paint object for drawing debug stuff */
191+
/**
192+
* Paint object for drawing debug stuff
193+
*/
170194
private Paint debugPaint;
171195

172-
/** Policy for fitting pages to screen */
196+
/**
197+
* Policy for fitting pages to screen
198+
*/
173199
private FitPolicy pageFitPolicy = FitPolicy.WIDTH;
174200

175201
private boolean fitEachPage = false;
176202

177203
private int defaultPage = 0;
178204

179-
/** True if should scroll through pages vertically instead of horizontally */
205+
/**
206+
* True if should scroll through pages vertically instead of horizontally
207+
*/
180208
private boolean swipeVertical = true;
181209

182210
private boolean enableSwipe = true;
@@ -187,7 +215,9 @@ enum ScrollDir {
187215

188216
private boolean pageSnap = true;
189217

190-
/** Pdfium core for loading and rendering PDFs */
218+
/**
219+
* Pdfium core for loading and rendering PDFs
220+
*/
191221
private PdfiumCore pdfiumCore;
192222

193223
private ScrollHandle scrollHandle;
@@ -218,30 +248,46 @@ ScrollHandle getScrollHandle() {
218248
*/
219249
private boolean renderDuringScale = false;
220250

221-
/** Antialiasing and bitmap filtering */
251+
/**
252+
* Antialiasing and bitmap filtering
253+
*/
222254
private boolean enableAntialiasing = true;
223255
private PaintFlagsDrawFilter antialiasFilter =
224256
new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
225257

226-
/** Spacing between pages, in px */
258+
/**
259+
* Spacing between pages, in px
260+
*/
227261
private int spacingPx = 0;
228262

229-
/** Add dynamic spacing to fit each page separately on the screen. */
263+
/**
264+
* Add dynamic spacing to fit each page separately on the screen.
265+
*/
230266
private boolean autoSpacing = false;
231267

232-
/** Fling a single page at a time */
268+
/**
269+
* Fling a single page at a time
270+
*/
233271
private boolean pageFling = true;
234272

235-
/** Pages numbers used when calling onDrawAllListener */
273+
/**
274+
* Pages numbers used when calling onDrawAllListener
275+
*/
236276
private List<Integer> onDrawPagesNums = new ArrayList<>(10);
237277

238-
/** Holds info whether view has been added to layout and has width and height */
278+
/**
279+
* Holds info whether view has been added to layout and has width and height
280+
*/
239281
private boolean hasSize = false;
240282

241-
/** Holds last used Configurator that should be loaded when view has size */
283+
/**
284+
* Holds last used Configurator that should be loaded when view has size
285+
*/
242286
private Configurator waitingDocumentConfigurator;
243287

244-
/** Construct the initial view */
288+
/**
289+
* Construct the initial view
290+
*/
245291
public PDFView(Context context, AttributeSet set) {
246292
super(context, set);
247293

@@ -452,7 +498,9 @@ public boolean isRecycled() {
452498
return recycled;
453499
}
454500

455-
/** Handle fling animation */
501+
/**
502+
* Handle fling animation
503+
*/
456504
@Override
457505
public void computeScroll() {
458506
super.computeScroll();
@@ -493,10 +541,10 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
493541
float relativeCenterPointInStripXOffset;
494542
float relativeCenterPointInStripYOffset;
495543

496-
if (swipeVertical){
544+
if (swipeVertical) {
497545
relativeCenterPointInStripXOffset = centerPointInStripXOffset / pdfFile.getMaxPageWidth();
498546
relativeCenterPointInStripYOffset = centerPointInStripYOffset / pdfFile.getDocLen(zoom);
499-
}else {
547+
} else {
500548
relativeCenterPointInStripXOffset = centerPointInStripXOffset / pdfFile.getDocLen(zoom);
501549
relativeCenterPointInStripYOffset = centerPointInStripYOffset / pdfFile.getMaxPageHeight();
502550
}
@@ -506,12 +554,12 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
506554

507555
if (swipeVertical) {
508556
currentXOffset = -relativeCenterPointInStripXOffset * pdfFile.getMaxPageWidth() + w * 0.5f;
509-
currentYOffset = -relativeCenterPointInStripYOffset * pdfFile.getDocLen(zoom) + h * 0.5f ;
510-
}else {
557+
currentYOffset = -relativeCenterPointInStripYOffset * pdfFile.getDocLen(zoom) + h * 0.5f;
558+
} else {
511559
currentXOffset = -relativeCenterPointInStripXOffset * pdfFile.getDocLen(zoom) + w * 0.5f;
512560
currentYOffset = -relativeCenterPointInStripYOffset * pdfFile.getMaxPageHeight() + h * 0.5f;
513561
}
514-
moveTo(currentXOffset,currentYOffset);
562+
moveTo(currentXOffset, currentYOffset);
515563
loadPageByOffset();
516564
}
517565

@@ -667,7 +715,9 @@ private void drawWithListener(Canvas canvas, int page, OnDrawListener listener)
667715
}
668716
}
669717

670-
/** Draw a given PagePart on the canvas */
718+
/**
719+
* Draw a given PagePart on the canvas
720+
*/
671721
private void drawPart(Canvas canvas, PagePart part) {
672722
// Can seem strange, but avoid lot of calls
673723
RectF pageRelativeBounds = part.getPageRelativeBounds();
@@ -747,15 +797,22 @@ public void loadPages() {
747797
redraw();
748798
}
749799

750-
/** Called when the PDF is loaded */
800+
/**
801+
* Called when the PDF is loaded
802+
*/
751803
void loadComplete(PdfFile pdfFile) {
752804
state = State.LOADED;
753805

754806
this.pdfFile = pdfFile;
755807

808+
if (renderingHandlerThread == null) {
809+
return;
810+
}
811+
756812
if (!renderingHandlerThread.isAlive()) {
757813
renderingHandlerThread.start();
758814
}
815+
759816
renderingHandler = new RenderingHandler(renderingHandlerThread.getLooper(), this);
760817
renderingHandler.start();
761818

@@ -1263,56 +1320,74 @@ public boolean doRenderDuringScale() {
12631320
return renderDuringScale;
12641321
}
12651322

1266-
/** Returns null if document is not loaded */
1323+
/**
1324+
* Returns null if document is not loaded
1325+
*/
12671326
public PdfDocument.Meta getDocumentMeta() {
12681327
if (pdfFile == null) {
12691328
return null;
12701329
}
12711330
return pdfFile.getMetaData();
12721331
}
12731332

1274-
/** Will be empty until document is loaded */
1333+
/**
1334+
* Will be empty until document is loaded
1335+
*/
12751336
public List<PdfDocument.Bookmark> getTableOfContents() {
12761337
if (pdfFile == null) {
12771338
return Collections.emptyList();
12781339
}
12791340
return pdfFile.getBookmarks();
12801341
}
12811342

1282-
/** Will be empty until document is loaded */
1343+
/**
1344+
* Will be empty until document is loaded
1345+
*/
12831346
public List<PdfDocument.Link> getLinks(int page) {
12841347
if (pdfFile == null) {
12851348
return Collections.emptyList();
12861349
}
12871350
return pdfFile.getPageLinks(page);
12881351
}
12891352

1290-
/** Use an asset file as the pdf source */
1353+
/**
1354+
* Use an asset file as the pdf source
1355+
*/
12911356
public Configurator fromAsset(String assetName) {
12921357
return new Configurator(new AssetSource(assetName));
12931358
}
12941359

1295-
/** Use a file as the pdf source */
1360+
/**
1361+
* Use a file as the pdf source
1362+
*/
12961363
public Configurator fromFile(File file) {
12971364
return new Configurator(new FileSource(file));
12981365
}
12991366

1300-
/** Use URI as the pdf source, for use with content providers */
1367+
/**
1368+
* Use URI as the pdf source, for use with content providers
1369+
*/
13011370
public Configurator fromUri(Uri uri) {
13021371
return new Configurator(new UriSource(uri));
13031372
}
13041373

1305-
/** Use bytearray as the pdf source, documents is not saved */
1374+
/**
1375+
* Use bytearray as the pdf source, documents is not saved
1376+
*/
13061377
public Configurator fromBytes(byte[] bytes) {
13071378
return new Configurator(new ByteArraySource(bytes));
13081379
}
13091380

1310-
/** Use stream as the pdf source. Stream will be written to bytearray, because native code does not support Java Streams */
1381+
/**
1382+
* Use stream as the pdf source. Stream will be written to bytearray, because native code does not support Java Streams
1383+
*/
13111384
public Configurator fromStream(InputStream stream) {
13121385
return new Configurator(new InputStreamSource(stream));
13131386
}
13141387

1315-
/** Use custom source as pdf source */
1388+
/**
1389+
* Use custom source as pdf source
1390+
*/
13161391
public Configurator fromSource(DocumentSource docSource) {
13171392
return new Configurator(docSource);
13181393
}

0 commit comments

Comments
 (0)