android - 从头开始重绘 Canvas 时出现问题

标签 android canvas

我有一个 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,并为 ImageViewCanvas 重置它。

关于android - 从头开始重绘 Canvas 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27559194/

相关文章:

android - 如何每 1 分钟执行一次 asynctask?

Android 错误 - 打开失败 ENOENT

android - 如何从 JNI/NDK C++ 代码访问/res/raw?

javascript - 侧面发生碰撞时向上滑行

javascript - 如何制作重叠的可悬停和可点击的图像?

android - 类似功能 Android 的 TreeView

android - 无需命令代码即可在 Android 模拟器中调用来电

HTML5 Canvas 对象事件

iphone - 我的 HTML5 Canvas 效果可以在 iPhone 或 Android 上运行吗?

javascript - 点击事件不能正常工作