我使用XML中的Views来绘制PdfDocument.Page.的Canvas,问题是,生成的PDF在不同的设备上看起来是不一样的(文字大小,高度和宽度),这取决于屏幕大小等。问题是,生成的PDF在不同的设备上看起来是不同的(文字大小,高度和宽度),取决于屏幕大小等。
是否有一种方法可以创建相同的视图,不管屏幕大小是什么,所以我得到相同的PDF。Dp, sp, px, pt都以某种方式尊重物理设备.也许可以在Context中覆盖一些屏幕尺寸的值等等?
这是我生成PDF文档的代码。
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.pdf_person_container, null);
PdfDocument document = new PdfDocument();
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(595,842,1).create();
PdfDocument.Page page = document.startPage(pageInfo);
int measureWidth = View.MeasureSpec.makeMeasureSpec(page.getCanvas().getWidth(), View.MeasureSpec.EXACTLY);
int measuredHeight = View.MeasureSpec.makeMeasureSpec(page.getCanvas().getHeight(), View.MeasureSpec.EXACTLY);
view.measure(measureWidth, measuredHeight);
view.layout(0, 0, page.getCanvas().getWidth(), page.getCanvas().getHeight());
view.draw(page.getCanvas());
document.finishPage(page);
document.writeTo(new FileOutputStream(filePath + "Detail.pdf"));
Configuration config = sourcecontext.getResources().getConfiguration();
config.densityDpi = DisplayMetrics.DENSITY_HIGH;
config.setTo(config);
Context context sourcecontext.createConfigurationContext(config);
并使用这个新的上下文为Inflater。