Undo In A Drawing App
Before I start I would like you to tell that I am using a very improper method for the 'undo' operation. I have a drawing app which will save each strokes you draw on the view, and
Solution 1:
Well, I would not store the bitmaps in files at all. If it's a simple drawing app, then you could (I would) just have an arrayList of say 20 Bitmaps and store the bitmaps to the list. If the length ever gets above 20, remove the first item. This will give you some undo buffer and allow you to more easily handle the operation. As for saving to the sd card look here.
~Aedon
Edit 1:
Use this method to create the bitmap of the canvas.
public Bitmap toBitmap(Canvas canvas) {
Bitmapb= Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
canvas.setBitmap(b);
draw(canvas);
return b;
}
Sample onToushListener:
newOnTouchListener() {
publicvoidonTouch(View v, MotionEvent e) {
switch(e.getAction()) {
caseMotionEvent.ACTION_DOWN: mIsGesturing = true; mCurrentDrawPath.moveTo(...); // Fall throughcaseMotionEvent.ACTION_MOVE: // create the draw path the way you are now.caseMotionEvent.ACTION_UP: mIsGesturing = false; addNewBufferBitmap(toBitmap(canvas)); break;
}
}
};
And onDraw(canvas) will look similar to this:
publicvoidonDraw(Canvas canvas) {
canvas.drawBitmap(getLastBufferBitmap(), 0, 0, null);
if (mCurrentDrawPath != null) canvas.drawPath(mCurrentDrawPath, mPathPaint);
if (mIsGesturing) mCanvas = canvas;
}
Solution 2:
This code is working in my drawing app..
privateSlate mSlate;
privateTiledBitmapCanvas mTiledCanvas;
publicvoidclickUndo(View unused) {
mSlate.undo();
}
publicvoidundo() {
if (mTiledCanvas == null) {
Log.v(TAG, "undo before mTiledCanvas inited");
}
mTiledCanvas.step(-1);
invalidate();
}
Post a Comment for "Undo In A Drawing App"