i want to do undo features in in my application.. for this i searched i net found that take arraylist of x,y points which i have done below code i am unable for undo the drawing
private Slate mSlate;
private TiledBitmapCanvas mTiledCanvas;
public void clickUndo(View unused) {
mSlate.undo();
}
public void undo() {
if (mTiledCanvas == null) {
Log.v(TAG, "undo before mTiledCanvas inited");
}
mTiledCanvas.step(-1);
invalidate();
}
Hey I have used a kind of trick to remove the black line.In my erase button, I have set the color to white, instead of using XferMode..
if(erase){
paintColor = Color.parseColor(newColor);
drawPaint.setColor(paintColor);
}
I didn't try, but i think use two canvas and bitmaps is helpful, on draw the current state, one is still former state, only need to restore the former one. But it only can restore on step.
here some code: create:
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();
}
Tested OK;