gpt4 book ai didi

android - 以全长创建 RecyclerView 的 PDF

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:23:12 26 4
gpt4 key购买 nike

我想创建 Activity “整页”的 PDF。该 View 包含一个包含许多项目的 RecyclerView。

我可以获取我的 Recyclerview 的完整尺寸,但文件仅绘制当前 View 。这是我的代码:

    public void tela (){ // create a new document
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)

{
PdfDocument document = new PdfDocument();

getScreenshotFromRecyclerView(mRecyclerView);
content = mRecyclerView;
content.setBackgroundColor(Color.parseColor("#303030"));


PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(wil,
height, 1).create();

// create a new page from the PageInfo
PdfDocument.Page page = document.startPage(pageInfo);

// repaint the user's text into the page
content.draw(page.getCanvas());

// do final processing of the page
document.finishPage(page);

// saving pdf document to sdcard
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy - HH-mm-ss",Locale.getDefault());
String pdfName = "Revisões_"
+ sdf.format(Calendar.getInstance().getTime()) + ".pdf";

// all created files will be saved at path /sdcard/PDFDemo_AndroidSRC/
File outputFile = new File(Environment.getExternalStorageDirectory().getPath(), pdfName);

try {
outputFile.createNewFile();
OutputStream out = new FileOutputStream(outputFile);
document.writeTo(out);
document.close();
out.close();
Toast.makeText(this,"PDF gerado com sucesso",Toast.LENGTH_SHORT).show();
Log.i("Gerou", "pdf");
} catch (IOException e) {
e.printStackTrace();
}
}
}

//获取限制

    public void getScreenshotFromRecyclerView(RecyclerView view) {
RecyclerView.Adapter adapter = view.getAdapter();
if (adapter != null) {
int size = adapter.getItemCount();

for (int i = 0; i < size; i++) {
RecyclerView.ViewHolder holder = adapter.createViewHolder(view, adapter.getItemViewType(i));
adapter.onBindViewHolder(holder, i);
holder.itemView.measure(View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
holder.itemView.layout(0, 0, holder.itemView.getMeasuredWidth(), holder.itemView.getMeasuredHeight());
height += holder.itemView.getMeasuredHeight();
}
wil=view.getMeasuredWidth();


}
}

结果是这样的:
enter image description here

我可以用我的 Recyclerview 的所有值创建 pdf 吗?

谢谢。

最佳答案

我实现了一个帮助程序类来处理将图像保存为 PDF。在 saveImageToPDF() 方法中,我传递:

  • 一个 TabLayout,它在我的 recyclerView 之上
  • recyclerView 的位图
  • 背景

为了获取 recyclerView 位图,我使用了这个 Take a screenshot of RecyclerView in FULL length

public class PDFHelper {

private File mFolder;
private File mFile;
private Context mContext;

public PDFHelper(File folder, Context context) {

this.mContext = context;
this.mFolder = folder;

if(!mFolder.exists())
mFolder.mkdirs();
}

public void saveImageToPDF(View title, Bitmap bitmap, String filename) {

mFile = new File(mFolder, filename + ".pdf");
if (!mFile.exists()) {
int height = title.getHeight() + bitmap.getHeight();
PdfDocument document = new PdfDocument();
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(bitmap.getWidth(), height, 1).create();
PdfDocument.Page page = document.startPage(pageInfo);
Canvas canvas = page.getCanvas();
title.draw(canvas);

canvas.drawBitmap(bitmap, null, new Rect(0, title.getHeight(), bitmap.getWidth(),bitmap.getHeight()), null);

document.finishPage(page);

try {
mFile.createNewFile();
OutputStream out = new FileOutputStream(mFile);
document.writeTo(out);
document.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}
}

关于android - 以全长创建 RecyclerView 的 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38278900/

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