PdfDocument的java布局
我环顾了一下四周,对如何使用原生安卓工具生成Pdf有了相当的了解。对于xml/视图应该如何构造,我仍然有点困惑。因为对我来说,它永远不会上膛。我将从xml开始。如下所示
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 安卓:id="@+id/dh_pdf_viewer_root"
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent">
<com.duethealth.lib.pdfviewer.ui.PdfDocumentView
安卓:id="@+id/dh_pdf_view"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"/>
<TextView
安卓:id="@+id/dh_pdf_page_counter"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_gravity="bottom|center_horizontal"
安卓:layout_margin="@dimen/dh_padding_default"
安卓:background="@color/dh_color_background_75"
安卓:gravity="center"
安卓:padding="@dimen/dh_padding_default"
安卓:textAppearance="@style/DuetHealth.TextAppearance.ListLine2"/>
<include layout="@layout/dh_include_loading_50"/>
因此,底部的文本视图用于显示我的页面计数器(如果需要的话),我不需要扩展ViewPager,它只是一个占位符
public class PdfDocumentView extends ViewPager {
public void writePdf(File file) {
PdfDocument document = new PdfDocument();
PageInfo pageInfo = new PageInfo.Builder(300, 300, 1).create();
Page page = document.startPage(pageInfo);
View content = findViewById(R.id.dh_pdf_view);
content.measure(800, 480);
content.layout(0, 0, 800, 480);
content.draw(page.getCanvas());
document.finishPage(page);
try {
FileOutputStream fileOutputStream = new FileOutputStream(file.getPath());
document.writeTo(fileOutputStream);
document.close();
fileOutputStream.close();
} catch (IOException e) {
Timber.e(e, "Error writing PDF");
}
}
我不明白我应该为PdfDocumentView
扩展哪个视图组,或者整个文档是否可以进入ImageView或其他什么?当我进行调试时,一切似乎都正常工作,PDF被成功地从URL拉入字节流,然后写入文件,然后写入内存,没有错误。看来我的问题出在视图本身的某个地方
共 (0) 个答案