Clear canvas in button click

自古美人都是妖i 提交于 2019-11-29 17:47:23

Add the below in your Drawing view

Bitmap mBitmap; 
Paint mPaint;
Canvas mCanvas;  
int width,height;  
public void clear()
{
    _graphics.removeAll(_graphics); 
    mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    mCanvas = new Canvas(mBitmap);
    path = new Path();   
    invalidate();
}

Also add the below

 @Override
 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = w;
        height = h;
        mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
    }  

In your onDraw add the below

    canvas.drawBitmap(mBitmap, 0, 0, null);

To clear on button click

 mView.clear();  
import android.graphics.Color;
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawColor(Color.TRANSPARENT);
    for (Path path : _graphics) {
        canvas.drawPath(path, mPaint);
    }
}

or

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    mPaint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
    canvas.drawPaint(mPaint);
    mPaint.setXfermode(new PorterDuffXfermode(Mode.SRC));
    for (Path path : _graphics) {
        canvas.drawPath(path, mPaint);
    }
}

See this Doc for more info.

Your clear method is wrong.

public void clear(View v) {

    ((DrawingView)mView).clearView();
}

This code works for me

clearbtn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
        mBitmap.eraseColor(Color.TRANSPARENT);
        mPath.reset();
        mView.invalidate();
        }
        });

this method work for me,

public void Clear()
{
mBitmap.eraseColor(Color.TRANSPARENT);
mPath.reset();
invalidate();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!