如何在画布上擦除位图时撤消擦除?

时间:2013-07-01 13:45:29

标签: android

我正在开发一个项目,其中有一个我想用户可以撤消的删除功能     如果他想要他的擦除,但这个功能对我不起作用。                                                                        我的代码是: - 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]

如图所示我们擦除狗的某些部分后,点击撤消按钮狗应该进入他的初始位置。请任何人指导我。

1 个答案:

答案 0 :(得分:0)

您是否尝试在删除位图的任何部分之前保存(Canvas.save()) Canvas,然后在点击按钮后重新恢复(Canvas.restore())并重新绘制?