Clear canvas in button click

2019-01-29 11:08发布

问题:

I used following code to draw. I want to clear the previously drawn lines if the clear button is clicked.

public class MainActivity extends Activity {
    private ArrayList<Path> _graphics = new ArrayList<Path>();
    private Paint mPaint;
    Activity activity;  
    View mView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        activity = this;
        mView = new DrawingView(this);
        activity.addContentView(mView, new LayoutParams(500,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        init();
    }

    public void clear(View v) {

        new DrawingView(activity).clearView();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setDither(true);
        mPaint.setColor(0xFFFFFF00);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(3);
    }

    class DrawingView extends View {
        private Path path;

        public DrawingView(Context context) {
            super(context);
            path = new Path();
            this.setBackgroundColor(Color.BLACK);
        }

        public boolean onTouchEvent(MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                path.moveTo(event.getX(), event.getY());
                path.lineTo(event.getX(), event.getY());
            } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
                path.lineTo(event.getX(), event.getY());
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                path.lineTo(event.getX(), event.getY());
                _graphics.add(path);
            }
            invalidate();
            return true;
        }

        @Override
        public void onDraw(Canvas canvas) {         
            for (Path path : _graphics) {
                canvas.drawPath(path, mPaint);
            }
        }

        public void clearView() {
            path = new Path();          
            invalidate();
        }
    }
}

Thank you.

回答1:

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();  


回答2:

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();
}


回答3:

This code works for me

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


回答4:

this method work for me,

public void Clear()
{
mBitmap.eraseColor(Color.TRANSPARENT);
mPath.reset();
invalidate();
}