вот код:
создать:
private Canvas mCanvas;
private Bitmap mBitmap;
private Canvas mRestoreCanvas;
private Bitmap mRestoreBitmap;
INIT:
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
mPictureWidth = w;
mPictureHeight = h;
Loge.i("mPictureWidth = " + mPictureWidth + " mPictureHeight = "
+ mPictureHeight);
if (mCanvas == null && mBitmap == null && mPictureWidth > 0
&& mPictureHeight > 0) {
mBitmap = Bitmap.createBitmap(mPictureWidth, mPictureHeight,
Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mCanvas.save();
mRestoreBitmap = Bitmap.createBitmap(mPictureWidth, mPictureHeight,
Bitmap.Config.ARGB_8888);
mRestoreCanvas = new Canvas(mRestoreBitmap);
mRestoreCanvas.save();
}
super.onSizeChanged(w, h, oldw, oldh);
}
OnDraw:
@Override
protected void onDraw(Canvas canvas) {
if (mPattern == DRAW_LINE) {
mCanvas.restore();
mCanvas.drawPath(path, paint);
mCanvas.save();
}
mCanvas.save();
if (mBitmap != null)
canvas.drawBitmap(mBitmap, 0, 0, null);
}
onTouchEvent:
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
mRestoreCanvas.drawBitmap(mBitmap, 0, 0, bitmapPaint);
}
if (mPattern == DRAW_LINE) {
return drawLines(event);
} else if (mPattern == DRAW_PATTERN) {
return drawPattern(event);
}
return false;
}
Undo:
public void dust() {
path.reset();
mBitmap.recycle();
mBitmap = Bitmap.createBitmap(mPictureWidth, mPictureHeight,
Bitmap.Config.ARGB_8888);
mCanvas.setBitmap(mBitmap);
mCanvas.drawBitmap(mRestoreBitmap, 0, 0, bitmapPaint);
invalidate();
}
Протестировано ОК;