实现效果:
引入项目(打包以后项目会增加16M):
发文时的最新版:
implementation 'com.github.barteksc:android-pdf-viewer:3.2.0-beta.1'
当然也有旧一点的版本:
implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'
//如果项目有混淆需要的话,需要添加规则,否则混淆以后不能运行
-keep class com.shockwave.**
//布局文件
<com.github.barteksc.pdfviewer.PDFView
android:id="@+id/pdfView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
//核心代码
//引入文件,path可以是Uri、File、byte[]、InputStream、Assets的文件、path路径
pdfView.fromUri(path)
pdfView.fromFile(File)
pdfView.fromBytes(byte[])
pdfView.fromStream(InputStream)
pdfView.fromAsset(String)
pdfView.fromSource(DocumentSource)
pdfView.pages(0, 2, 1, 3, 3, 3) // 不设置的话默认显示全部
pdfView.swipeHorizontal(false) //设置滚动方向
pdfView.enableDoubletap(true) //允许算计放大
pdfView.defaultPage(0) //默认页面
pdfView.scrollHandle(null) //滚动手柄,null表示不启用,pdfView自带一个默认的滚动手柄,如果scrollHandle里面的参数是new DefaultScrollHandle(this,true)则表示启用滚动手柄,true是左边,false是右边。
pdfView.nightMode(false) // 是否开启夜间模式
pdfView.load(); //导入
完整代码:
pdfView = findViewById(R.id.pdfView);
pdfView.fromAsset("python.pdf")
.enableSwipe(true)
.swipeHorizontal(false)
.enableDoubletap(true)
.defaultPage(0)
.enableAnnotationRendering(false)
.password(null)
.scrollHandle(new DefaultScrollHandle(this))
.enableAntialiasing(true)
.spacing(0)
.autoSpacing(false)
.pageFitPolicy(FitPolicy.WIDTH)
.fitEachPage(false)
.pageSnap(false)
.pageFling(false)
.nightMode(false)
.load();