我有一个 Canvas ,我在上面画了很多东西,稍后我想将它设置回原始的、未涂鸦的图像。
currentBitmap = BitmapFactory.decodeResource(OCRMain.this.getResources(), R.drawable.ocrnew2, options);
currentBitmap = currentBitmap.copy(Bitmap.Config.ARGB_8888, true);
mainImageToOCR.setImageBitmap(currentBitmap);
mainImageToOCR.setAdjustViewBounds(true);
canvasForDrawingBorderBoxes = new Canvas(currentBitmap);
//Draw on Canvas
//Now, this is what I had tried, but isn't working as desired as the scribbles I draw are staying visible.
canvasForDrawingBorderBoxes = new Canvas(currentBitmap);
mainImageToOCR.invalidate();
将 Canvas 刷新到其未修改 View 的正确方法是什么?顺便说一下,这一切都发生在 UI 线程上。
提前致谢。
编辑:
感谢 Mike M,一旦我了解了幕后发生的事情,我就能够修改我的代码。我创建了另一个位图文件 tempBitmap,将其设置为 currentBitmap 的副本,并将 ImageView 设置为该文件,如下所示:
//At the beginning, before I draw on the Bitmap at all, I use tempBitmap to hold a copy of currentBitmap
tempBitmap = currentBitmap.copy(Bitmap.Config.ARGB_8888, true);
//Do drawing stuff
currentBitmap = tempBitmap;
mainImageToOCR.setImageBitmap(currentBitmap);
一个问题是,由于 Bitmap 可能是一张大图像,用户刚刚拍了一张照片,复制它可能会导致内存问题。但它尚未引发任何错误。
最佳答案
位图
实际上就是您正在绘制的内容。 Canvas
正是允许您这样做的工具。如果您希望图像恢复到原始状态,请重新加载 Bitmap
,并为 ImageView
和 Canvas
重置它。
关于android - 从头开始重绘 Canvas 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27559194/