我正在尝试将视图转换为图像,并使用iText将它们放入PDF文件中。但我失去了品质。它与Bitmap.compress()
函数有关吗?
val screen = getBitmapFromView(
context.window.findViewById(id))
try {
val stream = ByteArrayOutputStream()
screen.compress(Bitmap.CompressFormat.PNG, 100, stream)
val image = Image.getInstance(stream.toByteArray())
image.alignment = Image.ALIGN_CENTER
document.add(image)
}
我的getBitmapFromView()
功能:
private fun getBitmapFromView(view: View): Bitmap {
val returnedBitmap = Bitmap.createBitmap(view.width, view.height,
Bitmap.Config.ARGB_8888)
val canvas = Canvas(returnedBitmap)
val bgDrawable = view.background
if (bgDrawable != null)
bgDrawable.draw(canvas)
else
canvas.drawColor(Color.WHITE)
view.draw(canvas)
return returnedBitmap
}
编辑:添加了屏幕截图