Android Compose展示PDF文件

github源码文件

添加依赖

kotlin 复制代码
 implementation "com.github.barteksc:android-pdf-viewer:3.2.0-beta.1"

在compose中展示pdf文件

kotlin 复制代码
   AndroidView(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(600.dp),
                factory = { ctx ->
                    val parser: XmlPullParser = ctx.resources.getXml(R.xml.file_path)
                    val attributes = Xml.asAttributeSet(parser)
                    PDFView(ctx, attributes).apply {
                        isLoading = true
                    }

                },
                update = { pdfview ->
                    pdfview//.fromUri(Uri.parse(url))
                        .fromFile(File(pathAndroidPdf))
                        .defaultPage(0)
                        .onPageChange { page, pageCount ->

                        }
                        .enableAnnotationRendering(false)
                        .onLoad {
                            Log.d(TAG, "PreviewUriPage: $it")
//                        onLoadComplete(it)
                            isLoading = false
                        }
                        .spacing(10)
                        .pageFitPolicy(FitPolicy.BOTH)
                        .load()

                })

线上文件使用fromUri(Uri.parse(url))

本地文件使用.fromFile(File(pathAndroidPdf))

问题:

对于pdf文件过大的情况,渲染时间过长,在渲染过程中,没有找到对应的方法

在xml View中展示pdf文件

view:

kotlin 复制代码
  <com.github.barteksc.pdfviewer.PDFView
                android:id="@+id/fl_pdf"
                android:layout_width="match_parent"
                android:layout_height="600dp"
                android:visibility="visible"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                />

实现

kotlin 复制代码
binding.flPdf.fromUri(Uri.parse(it))
               // .fromFile(File(pathAndroidPdf))
                .defaultPage(0)
                .onPageChange { page, pageCount ->

                }
                .enableAnnotationRendering(false)
                .onLoad {
                showLoading(true)
                }
                .onRender {
                    showLoading(false)
                }
                .spacing(10)
                .pageFitPolicy(FitPolicy.BOTH)
                .load()
相关推荐
雨白14 小时前
单例设计模式:从 Java/Kotlin 实现到 Android 实战
android
hai_android18 小时前
一行 asLiveData,背后是一座生命周期的桥
android·java·kotlin
阿杰m19 小时前
Android源码第二十三章:Telephony、RIL 与 IMS——来电怎么到应用?
android
booboosui19 小时前
没人认领 .md:安卓把报告交给了音乐播放器打开
android
用户60709759149061 天前
AOSP 14 OTA 升级指导(四):update_engine 内部机制——Action 流水线与写盘
android
开开心心就好1 天前
显示器关不掉怎么办,用小工具一键息屏
算法·智能手机·pdf·计算机外设·ocr·lua·1024程序员节
weixin_537217061 天前
2027年袁东申论万能素材(PDF)
经验分享·pdf
恋猫de小郭1 天前
Jetpack Compose 1.13 发布,憋了这么久终有交代了
android·前端·flutter
JMchen1231 天前
【Android 性能优化实战 60 讲】12 异步 Inflate 实战与痛击:源码拆解原生瓶颈,手写增强版布局预加载框架
android·性能优化·实战·启动优化·异步inflate·x2c
好的佩奇2 天前
蓝牙之设备角色与连接
android·flutter·蓝牙