【发布时间】:2018-05-27 12:14:36
【问题描述】:
我正在使用本机 PDF 库 (android.graphics.pdf.PdfDocument) 生成 PDF 文件
文档应该包含一个标题,它是一个静态 imageView、一些 textview 和一个带有一堆图像的 RecyclerView,
除了recyclerview之外的一切都显示正确,
recyclerview 正在使用 Glide 库加载图像,但它们没有加载或显示,我尝试使用库预取它们,但它什么也没做,
这里是创建pdf的代码
public File createPDF(PostDB postDB) {
int addition = 0;
String postDBContent = postDB.getContent();
if (postDBContent != null) {
int estimatedLineCount = postDBContent.length() / 43;
String[] lines = postDBContent.split("\r\n|\r|\n");
estimatedLineCount += lines.length;
addition = (estimatedLineCount - 43) * 60;
}
List<String> imagesUrls = postDB.getImagesUrls();
for (String url : imagesUrls) {
Glide.with(context).load(url).preload();
}
try {
FileOutputStream fOut = context.openFileOutput(LOCAL_PATH, Context.MODE_PRIVATE);
PdfDocument document = new PdfDocument();
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(1800, 3900 + addition + 1000, 1).create();
PdfDocument.Page page = document.startPage(pageInfo);
Canvas canvas = page.getCanvas();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View content;
if (inflater != null) {
final int[] count = {0};
content = inflater.inflate(R.layout.pdf_layout, null);
ButterKnife.bind(this, content);
publisherTextView.setText(postDB.getAuthor());
Date date = new Date(postDB.getDate());
DateFormat dateFormat = DateFormat.getInstance();
String format = dateFormat.format(date);
dateTextView.setText(format);
titleTextView.setText(postDB.getTitle());
contentPreviewTextView.setText(postDBContent);
recyclerViewImages.setLayoutManager(new GridLayoutManager(context, 2));
recyclerViewImages.setAdapter(new PDFImageAdapter(postDB.getImagesUrls()));
String text = postDB.getCity() + ", " + postDB.getCountry();
location.setText(text);
int measureWidth = View.MeasureSpec.makeMeasureSpec(page.getCanvas().getWidth(), View.MeasureSpec.EXACTLY);
int measuredHeight = View.MeasureSpec.makeMeasureSpec(page.getCanvas().getHeight(), View.MeasureSpec.EXACTLY);
content.measure(measureWidth, measuredHeight);
content.layout(0, 0, page.getCanvas().getWidth(), page.getCanvas().getHeight());
content.draw(canvas);
}
document.finishPage(page);
document.writeTo(fOut);
document.close();
fOut.close();
return new File(context.getFilesDir().getPath() + "/" + LOCAL_PATH);
} catch (IOException e) {
Log.i("error", e.getLocalizedMessage());
return null;
}
}
这是我绑定 ViewHolders 的代码
public void setData(String url) {
Glide.with(imageView.getContext()).load(url)
.listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
Timber.d("onLoadFailed");
return true;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
Timber.d("onResourceReady");
return true;
}
})
.into(imageView);
}
调用数据的onBind方法,并调用setData方法。但从未调用过 Glide 的回调。
【问题讨论】:
-
我认为您需要先下载所有图像,缓存它们(例如在 getCacheDir() 中),然后创建一个 PDF。
-
RecyclerView 不是为此而设计的。你可以做的是添加一个 LinearLayout 而不是 RecyclerView 并将每个图像添加到它。如果你使用recyclerview,一些图像将不会被绘制(如果高度参数是'match_parent'。如果你将它设置为'wrap_content'......你为什么还要使用recyclerview)。无论如何,您可以使用计数器跟踪是否所有图像都已下载。当计数器到达所有要加载的图像时,您可以在画布上绘制整个布局,最终将是 pdf。如果我不清楚,请告诉我。
标签: android pdf-generation android-glide