问题描述
我想创建活动的整页"的PDF.该视图包含一个RecyclerView,其中包含许多项目.
I want to create a PDF of "full page" of the activity. The view contains a RecyclerView with many items.
我可以使用Recyclerview的完整尺寸,但是仅从当前视图绘制文件.这是我的代码:
I can take a full dimensions of my Recyclerview but the file is drawed only of the current view. This is my code:
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();
}
}
结果是这样的:
The result is this:
我可以使用Recyclerview的所有值创建pdf吗?
Can I create the pdf with the all values of my Recyclerview?
谢谢.
推荐答案
我实现了一个帮助器类来处理将图像保存为PDF.在方法saveImageToPDF()中,我通过了:
I implemented a helper class to handle image saving to PDF. In the method saveImageToPDF() I pass:
- 一个TabLayout,位于我的recyclerView上方
- recyclerView的位图
- 上下文
获取recyclerView位图,我使用了此拍摄RecyclerView的屏幕截图完整长度
to get the recyclerView Bitmap I used this 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();
}
}
}
}
这篇关于创建完整长度的RecyclerView PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!