gpt4 book ai didi

android - PdfDocument - 在布局末尾剪切页面

转载 作者:行者123 更新时间:2023-11-30 05:05:12 24 4
gpt4 key购买 nike

我正在使用 Android 创建一个 Pdf 文件 PdfDocument类(class)。我从 XML 文件(ConstraintLayout 中的 LinearLayout)扩展布局,然后在运行时通过添加带有外部数据的新行来完成。 p>

问题是输出的 pdf 可能很长也可能很短。我想避免在扩充布局之前指定页面高度。但是,我找不到在将布局添加到页面之前测量布局高度的解决方案

这是实际的代码:

PdfDocument document = new PdfDocument();

//set page width and height
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(227,992, 1).create();
PdfDocument.Page page = document.startPage(pageInfo);

//inflating the layout
View v = getLayoutInflater().inflate(R.layout.rapportino, null);

//add data to layout
//...

//measure the layout, draw it and finish page
int measureWidth = View.MeasureSpec.makeMeasureSpec(page.getCanvas().getWidth(), View.MeasureSpec.EXACTLY);
int measuredHeight = View.MeasureSpec.makeMeasureSpec(page.getCanvas().getHeight(), View.MeasureSpec.EXACTLY);
v.measure(measureWidth, measuredHeight);
v.layout(0, 0, measureWidth, measuredHeight);
v.draw(page.getCanvas());
document.finishPage(page);

//write to file..

有没有办法根据 View 的高度创建 pdf?

编辑:这是我正在膨胀的布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<TextView
android:id="@+id/rapportino_indirizzo_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:gravity="end"
android:textColor="#000000"
android:textSize="@dimen/rapportino_global_textsize" />

<!--
other textview, linearlayout and imageview
..
..
-->

</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

最佳答案

所以,我用一个很奇怪的解决方案解决了。由于向 View 添加布局会导致计算高度,因此我在创建 pdf 的 Activity 中创建了一个不可见 View (具有 pdf 的宽度):

<!-- add this to your activity -->
<LinearLayout
android:visibility="invisible"
android:id="@+id/output_pdf_view"
android:orientation="vertical"
android:layout_width="226dp"
android:layout_height="wrap_content" />

然后我将膨胀的布局添加到不可见 View (在用外部数据填充它之后)。使用观察器,您可以获得计算时的高度。

//inflating the layout
View v = getLayoutInflater().inflate(R.layout.rapportino, null);

//add data to layout (rows, images etc.)
//...

//find the invisible view
LinearLayout invisibleOutputPdfView = findViewById(R.id.output_pdf_view);

//add the pdf layout and observe changes
invisibleOutputPdfView.addView(v);
invisibleOutputPdfView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
//remove the listener
invisibleOutputPdfView.getViewTreeObserver().removeOnGlobalLayoutListener(this);

int height = invisibleOutputPdfView.getHeight();
createPDF(height, ..);
}
});

注意:出于某种原因,在生成 pdf 时,dppt 之间似乎存在 1:1 的比例。因此不需要转换。

关于android - PdfDocument - 在布局末尾剪切页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54686236/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com