2013-07-01 90 views
1

我正在開發一個項目,其中有一個刪除功能,如果他想刪除 刪除 ,但是這個功能對我不起作用。 我的代碼: - enter image description here如何在畫布上擦除位圖時撤消擦除操作?

public EraserView(Context context) { 
    super(context); 
    setFocusable(true); 
    setBackgroundResource(R.drawable.back); 
    // setting paint 
    mPaint = new Paint(); 
    mPath = new Path(); 
    mPaint.setAlpha(0); 
    mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); 
    mPaint.setColor(Color.TRANSPARENT); 
    mPaint.setAntiAlias(true); 

    // getting image from resources 
    Resources r = this.getContext().getResources(); 

    Bitmap bm = BitmapFactory.decodeResource(getResources(), 
      R.drawable.image2); 

    // converting image bitmap into mutable bitmap 

    bitmap = bm.createBitmap(295, 260, Config.ARGB_8888); 
    pcanvas = new Canvas(); 
    pcanvas.setBitmap(bitmap); // drawXY will result on that Bitmap 
    pcanvas.drawBitmap(bm, 0, 0, null); 
    paths.add(mPath); 

} 

@Override 
protected void onDraw(Canvas canvas) { 

    // draw a circle that is erasing bitmap 

    pcanvas.drawCircle(x,y,r,mPaint); 
    /*for (Path objpath : paths) { 
     pcanvas.drawPath(objpath, mPaint); 
    }*/ 
    canvas.drawBitmap(bitmap, 0, 0, null); 

    setBitmap(bitmap); 
    super.onDraw(canvas); 

} 

@Override 
public boolean onTouchEvent(MotionEvent event) { 

    switch (event.getAction()) { 
    case MotionEvent.ACTION_DOWN: 
     x = (int) event.getX(); 
     y = (int) event.getY(); 

     r = 8; 
     // Atlast invalidate canvas 
     invalidate(); 
     break; 
    case MotionEvent.ACTION_UP: 
     x = (int) event.getX(); 
     y = (int) event.getY(); 

     r = 8; 
     // Atlast invalidate canvas 
     invalidate(); 
     break; 
    case MotionEvent.ACTION_MOVE: 
     x = (int) event.getX(); 
     y = (int) event.getY(); 

     r = 8; 
     // Atlast invalidate canvas 
     invalidate(); 
     break; 
    case MotionEvent.ACTION_POINTER_UP: 
     x = (int) event.getX(); 
     y = (int) event.getY(); 

     r = 8; 
     // Atlast invalidate canvas 
     invalidate(); 
     break; 
    } 
    return true; 
} 

public void setBitmap(Bitmap bitmap) { 
    this.bitmap = bitmap; 
} 

public Bitmap getBitmap() { 
    return bitmap; 
} 

public void onClickUndo() { 
    if (paths.size()>0) 
    { 
     paths.add(paths.remove(paths.size()-1)); 
     invalidate(); 
    } 
    else 
    { 

    } 
}![enter image description here][1] 
如圖

圖像我們抹去狗的某些部分後點擊撤銷按鈕狗應該進來他最初position.please任何一個指導我。

回答

0

在擦除位圖的任何部分然後恢復(Canvas.restore())並重新點擊按鈕之後,您是否嘗試將Canvas保存(Canvas.save())Canvas?

+0

感謝您的回覆,但我的需求是一步一步撤銷。 – SKT

+0

canvas save()和restore()僅保存或恢復其狀態,而不是在畫布上繪製。我的意思是它會節省旋轉轉換等,但它不會保存/恢復任何繪圖。 @SKT:您可以使用多次調用save()來保存多個狀態,然後使用restore()多次調用撤消它們。 撤銷/重做繪圖請檢查下面的演示。它可能有所幫助。 https://github.com/hardik-trivedi/PaintApp –