barteksc/PdfiumAndroid 这个源码被,引用的次数是比较高的,flutter的几个pdf库也是引用它.它使用福昕的开源sdk.福昕阅读器我早期的时候用,交互一般,渲染也不如mupdf,有些pdf中文显示不了,体积小点.
barteksc/PdfiumAndroid已经是一个完善的sdk了,不去作什么修改,这里分析一下它的工作原理就行.与vudroid的模式略有差别.但大道是相通的.分块渲染.
入口是PDFView,它提供了build模式来构造.
PDFView.this.setSwipeEnabled(enableSwipe);
PDFView.this.setNightMode(nightMode);
PDFView.this.enableDoubletap(enableDoubletap);
PDFView.this.setDefaultPage(defaultPage);
PDFView.this.setSwipeVertical(!swipeHorizontal);
PDFView.this.enableAnnotationRendering(annotationRendering);
PDFView.this.setScrollHandle(scrollHandle);
PDFView.this.enableAntialiasing(antialiasing);
PDFView.this.setSpacing(spacing);
PDFView.this.setAutoSpacing(autoSpacing);
PDFView.this.setPageFitPolicy(pageFitPolicy);
PDFView.this.setFitEachPage(fitEachPage);
PDFView.this.setPageSnap(pageSnap);
PDFView.this.setPageFling(pageFling);
这要功能是这些,垂直与水平滚动,双击放大,页面间距,页面的适配屏幕
共1564行,它的特点是支持rgb565,与argb8888.而mupdf源码编译的只支持argb8888,如果想支持565要自己修改了.
- private void load(DocumentSource docSource, String password, int[] userPages) {
-
- if (!recycled) {
- throw new IllegalStateException("Don't call load on a PDF View without recycling it first.");
- }
-
- recycled = false;
- // Start decoding document
- decodingAsyncTask = new DecodingAsyncTask(docSource, password, userPages, this, pdfiumCore);
- decodingAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
- }
这是入口方法,加载文档.加载完进入loadComplete
- void loadComplete(PdfFile pdfFile) {
- state = State.LOADED;
-
- this.pdfFile = pdfFile;
-
- if (!renderingHandlerThread.isAlive()) {
- renderingHandlerThread.start();
- }
- renderingHandler = new RenderingHandler(renderingHandlerThread.getLooper(), this);
- renderingHandler.start();
-
- if (scrollHandle != null) {
- scrollHandle.setupLayout(this);
- isScrollHandleInit = true;
- }
-
- dragPinchM