[英]How to resize high quality image from gallery to appear in image view?
[英]How to create high quality PDF from a view in android studio (java)?
我想為我的一項活動創建一個線性布局的 PDF 文件。 示例如下: https://medium.com/@strawberryinc0531/convert-xml-to-pdf-in-android-studio-using-pdfdocument-class-757dee166d50
我最初創建一個 bitmap。 無論是否縮放,從 bitmap 生成的 pdf 都會產生低分辨率內容(即,縮放后的 pdf 文件是模糊的)。 我可以創建線性布局的矢量化圖像來創建 PDF 嗎?
不要縮小 Bitmap 以適應 PDF 頁面大小,在從 PDF 文檔獲得的 Canvas 上設置比例。
這將以原始分辨率存儲 bitmap,但以適合頁面大小的正確大小顯示它。 因此,當您放大 PDF 時,它不會像素化,直到您用完原始 bitmap 中的細節。
例如
// Prep linearLayout to draw to Bitmap
....
// create page
PdfDocument.Page page = document.startPage(pageInfo);
Canvas pageCanvas = page.getCanvas();
// Work out scale to use to fit width of page
// In this instance the bitmap is wider than the height
// so we don't need to work out if we need a smaller scale to fit the height on to the page
float scaleWidth = (float) pageCanvas.getWidth() / linearLayout.getWidth();
// Draw the Layout to a bitmap at it's original size
Canvas bitmapCanvas = new Canvas();
Bitmap bitmap = Bitmap.createBitmap(linearLayout.getWidth(), linearLayout.getHeight(), Bitmap.Config.ARGB_8888);
bitmapCanvas.setBitmap(bitmap);
linearLayout.draw(bitmapCanvas);
// The important bit to set the page scale
pageCanvas.scale(scaleWidth, scaleWidth);
// Draw the original sized bitmap to page
Paint paint = new Paint();
paint.setAntiAlias(true);
pageCanvas.drawBitmap(bitmap,0,0,paint);
如果您查看它創建的 PDF
因此在這個例子中,包含在 PDF 中的圖像是 1322 x 669 但它顯示在 594 x 841 的 MediaBox 中,因此它有大約 2.2 倍的細節可以放大
另一種方法是將您的布局直接繪制到 PDF 頁面,而無需通過 bitmap,因為這會創建 pdf “基元”,如縮放更好的文本和線條對象,但您必須調整布局大小以適應 PDF 頁面大小。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.