Search code examples
androidandroid-canvas

Clear canvas in button click


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.


Solution

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