How to clear the Canvas in android?
<p>In my application, have a requirement of drawing a circle on an image. Right now I use canvas and the <code>Canvas</code> holds this image. My requirement is that I first draw a circle with 20 radius, then after a second clear the previous circle and draw a 25 radius circle and so on.</p>

<p>Below is my <code>Fragment</code> code.</p>

<pre><code>import android.app.Activity;
    import android.content.pm.ActivityInfo;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;

    /**
     * Created by user on 5/23/2016.
     */
    public class StepTwentyOneFragment extends Fragment {

        private CanvasView customCanvas;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.step21_fragment, container, false);
            customCanvas=(CanvasView)v.findViewById(R.id.signature_canvas);

            final Button button1=(Button)v.findViewById(R.id.step18button1);

            button1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if(v.getId()==R.id.step18button1){

                        customCanvas.clear2();

                        v.setBackgroundResource(R.drawable.button_border_5);
                        button1.setTextColor(Color.WHITE);

                    }
                }
            });





            return v;
        }

        public static StepTwentyOneFragment newInstance() {

            StepTwentyOneFragment f = new StepTwentyOneFragment();
            Bundle b = new Bundle();

            f.setArguments(b);

            return f;
        }


        @Override
        public void setUserVisibleHint(boolean isVisibleToUser) {
            super.setUserVisibleHint(isVisibleToUser);
            if(isVisibleToUser) {
                Activity a = getActivity();
                if(a != null) a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            }
        }
</code></pre>

<p>Below is how I draw the Image and the circle. My <code>Fragment</code> class acess this class.</p>

<pre><code>import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

/**
 * Created by user on 4/28/2016.
 */
public class CanvasView extends View {

    public int width;
    public int height;
    private Bitmap mBitmap;
    private Canvas mCanvas;
    private Path mPath;
    Context context;
    private Paint mPaint;
    private float mX, mY;
    private static final float TOLERANCE = 5;

    public CanvasView(Context c, AttributeSet attrs) {
        super(c, attrs);
        context = c;

        mPath = new Path();

        mPaint = new Paint();
        mPaint.setStrokeWidth(3);
        mPaint.setColor(Color.CYAN);
    }

    // override onDraw
    @Override
    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);

        mCanvas=canvas;

        Drawable d = getResources().getDrawable(R.drawable.circle_1);

        int canvasHeight= canvas.getHeight();
        int canvasWidth= canvas.getWidth();

        Log.d("Height - "," / "+canvas.getHeight());
        Log.d("Width - "," / "+canvas.getWidth());

//         DisplayMetrics displaymetrics = new DisplayMetrics();
//        ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
//        int height = displaymetrics.heightPixels;
//        int width = displaymetrics.widthPixels;

        float h=canvasHeight/2;
        float w=canvasWidth/2;
        float r=(canvasWidth/2) - ((canvasWidth/2)/100)*20;

        d.setBounds(0, 0, canvasWidth, canvasHeight);
        d.draw(canvas);

        canvas.drawCircle(w, h, r, mPaint);
    }

    public void clear2(){

        mCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);

        Drawable d = getResources().getDrawable(R.drawable.circle);

        int canvasHeight= mCanvas.getHeight();
        int canvasWidth= mCanvas.getWidth();

        Log.d("Height - "," / "+mCanvas.getHeight());
        Log.d("Width - "," / "+mCanvas.getWidth());

//         DisplayMetrics displaymetrics = new DisplayMetrics();
//        ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
//        int height = displaymetrics.heightPixels;
//        int width = displaymetrics.widthPixels;

        float h=canvasHeight/2;
        float w=canvasWidth/2;
        float r=(canvasWidth/2) - ((canvasWidth/2)/100)*20;

        d.setBounds(0, 0, canvasWidth, canvasHeight);
        d.draw(mCanvas);
        mCanvas.restore();

    }

}
</code></pre>

<p>However the issue is that I am not able to clear the <code>Canvas</code> at all. Even though I have a method called <code>clear2</code> it is not clearing anything. Also I am not sure how to draw the other circle once the clear is done. I believe this "drawing" code has to be in my <code>Fragment</code> class? Please help.</p>