1

我正在做一个项目,其中有一个擦除功能我想用户可以撤消他的擦除如果他想要但这个功能对我不起作用。我的代码如下: - 在此处输入图像描述

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]

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

4

1 回答 1

0

您是否尝试过(Canvas.save())在擦除位图的任何部分之前保存画布,然后(Canvas.restore())在单击按钮后恢复和重绘?

于 2013-07-01T13:53:32.310 回答