存储绘图画布并在以后恢复它 [英] Store drawing canvas and restore it later
问题描述
我做了我的Android应用程序,用户可以画一些简单的白板。我想补充一个撤消功能,这样他就能够回到他的previous行动。
I've made a simple whiteboard on my Android app, where the user can draw something. I want to add an Undo feature so that he will be able to go back to his previous action.
在我的触摸开始
事件,我已经添加了以下code基本上将当前画布上的的ArrayList
,并创建一个新的,以避免同样的参考。
On my Touch Start
event, I've added the following code which basically adds the current canvas to an ArrayList
and create a new one to avoid same reference.
previousDrawing.add(this.canvas);
this.canvas = new Canvas(this.bitmap);
然后,在我的撤消
的方法,我已经添加了以下code:
Then, in my undo
method, I've added the following code:
if (previousDrawing.size() > 0)
{
// Remove last
this.canvas = previousDrawing.remove(previousDrawing.size() - 1);
this.canvas.setBitmap(this.bitmap);
}
它不会在所有的工作。我的意思是,我能够借鉴使用 this.canvas.drawPath(this.path,this.paint)我的画布;
而不是保存和恢复previous帆布。
It doesn't work at all. I mean, I'm able to draw on my canvas using this.canvas.drawPath(this.path, this.paint);
but not to save and restore a previous canvas.
你能帮我做到这一点?
先谢谢了。
编辑:
我也tryied使用 saveLayer方法
。但是,当我救,我不再能够在画布上绘制。这是一种正常的行为?
EDIT :
I've also tryied to use the saveLayer method
. But when I save, I'm no longer able to draw on the canvas. Is this a normal behaviour ?
推荐答案
尝试 drawBitmap
而不是 setBitmap
this.canvas.drawBitmap(this.bitmap,0,0,NULL);
修改
if (previousDrawing.size() > 0)
{
// Remove last
this.bitmap = previousDrawing.get(previousDrawing.size() - 1);
this.canvas.drawBitmap(this.bitmap, 0, 0, null);
previousDrawing.remove(previousDrawing.size() - 1);
}
参考<一个href=\"http://developer.android.com/reference/android/graphics/Canvas.html#drawBitmap%28int%5B%5D,%20int,%20int,%20float,%20float,%20int,%20int,%20boolean,%20android.graphics.Paint%29\"相对=nofollow>链接
这篇关于存储绘图画布并在以后恢复它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!