Android, canvas: How do I clear (delete contents of) a canvas (= bitmaps), living in a surfaceView?

AndroidAndroid CanvasSurfaceview

Android Problem Overview


In order to make a simple game, I used a template that draws a canvas with bitmaps like this:

private void doDraw(Canvas canvas) {
    for (int i=0;i<8;i++)
        for (int j=0;j<9;j++)
            for (int k=0;k<7;k++)   {
    canvas.drawBitmap(mBits[allBits[i][j][k]], i*50 -k*7, j*50 -k*7, null); } }

(The canvas is defined in "run()" / the SurfaceView lives in a GameThread.)

My first question is how do I clear (or redraw) the whole canvas for a new layout?
Second, how can I update just a part of the screen?

// This is the routine that calls "doDraw":
public void run() {
    while (mRun) {
        Canvas c = null;
        try {
            c = mSurfaceHolder.lockCanvas(null);
            synchronized (mSurfaceHolder) {
                if (mMode == STATE_RUNNING) 
                    updateGame();
                doDraw(c);          }
        } finally {
            if (c != null) {
                mSurfaceHolder.unlockCanvasAndPost(c);  }   }   }       }

Android Solutions


Solution 1 - Android

Draw transparent color with PorterDuff clear mode does the trick for what I wanted.

Canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR)

Solution 2 - Android

> How do I clear (or redraw) the WHOLE canvas for a new layout (= try at the game) ?

Just call Canvas.drawColor(Color.BLACK), or whatever color you want to clear your Canvas with.

> And: how can I update just a part of the screen ?

There is no such method that just update a "part of the screen" since Android OS is redrawing every pixel when updating the screen. But, when you're not clearing old drawings on your Canvas, the old drawings are still on the surface and that is probably one way to "update just a part" of the screen.

So, if you want to "update a part of the screen", just avoid calling Canvas.drawColor() method.

Solution 3 - Android

Found this in google groups and this worked for me..

Paint clearPaint = new Paint();
clearPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
canvas.drawRect(0, 0, width, height, clearPaint); 

This removes drawings rectangles etc. while keeping set bitmap..

Solution 4 - Android

use the reset method of Path class

Path.reset();

Solution 5 - Android

I tried the answer of @mobistry:

canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);

But it doesn't worked for me.

The solution, for me, was:

canvas.drawColor(Color.TRANSPARENT, Mode.MULTIPLY);

Maybe some one has the same problem.

Solution 6 - Android

mBitmap.eraseColor(Color.TRANSPARENT);

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

Solution 7 - Android

canvas.drawColor(Color.TRANSPARENT, Mode.MULTIPLY);

Solution 8 - Android

please paste below code on surfaceview extend class constructor.............

constructor coding

	SurfaceHolder holder = getHolder();
	holder.addCallback(this);

	SurfaceView sur = (SurfaceView)findViewById(R.id.surfaceview);
	sur.setZOrderOnTop(true);    // necessary
	holder = sur.getHolder();
	holder.setFormat(PixelFormat.TRANSPARENT);

xml coding

	<com.welcome.panelview.PanelViewWelcomeScreen
        android:id="@+id/one"
        android:layout_width="600px"
        android:layout_height="312px"
        android:layout_gravity="center"
        android:layout_marginTop="10px"
        android:background="@drawable/welcome" />

try above code...

Solution 9 - Android

Here is the code of a minimal example showing that you always have to redraw every pixel of the Canvas at each frame.

This activity draw a new Bitmap every second on the SurfaceView, without clearing the screen before. If you test it, you will see that the bitmap is not always written to the same buffer, and the screen will alternate between the two buffers.

I tested it on my phone (Nexus S, Android 2.3.3), and on the emulator (Android 2.2).

public class TestCanvas extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new TestView(this));
    }
}

class TestView extends SurfaceView implements SurfaceHolder.Callback {
	
	private TestThread mThread;
	private int mWidth;
	private int mHeight;
	private Bitmap mBitmap;
	private SurfaceHolder mSurfaceHolder;
	
	public TestView(Context context) {
		super(context);
		mThread = new TestThread();
		mBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon);
		mSurfaceHolder = getHolder();
		mSurfaceHolder.addCallback(this);
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		mWidth = width;
		mHeight = height;
		mThread.start();
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {/* Do nothing */}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		if (mThread != null && mThread.isAlive())
			mThread.interrupt();
	}

	class TestThread extends Thread {
		@Override
		public void run() {
			while (!isInterrupted()) {
				Canvas c = null;
				try {
					c = mSurfaceHolder.lockCanvas(null);
					synchronized (mSurfaceHolder) {
						c.drawBitmap(mBitmap, (int) (Math.random() * mWidth), (int) (Math.random() * mHeight), null);
					}
				} finally {
					if (c != null)
						mSurfaceHolder.unlockCanvasAndPost(c);
				}
				
				try {
					sleep(1000);
				} catch (InterruptedException e) {
					interrupt();
				}
			}
		}	
	}
}

Solution 10 - Android

For me calling Canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR) or something similar would only work after I touch the screen. SO I would call the above line of code but the screen would only clear after I then touched the screen. So what worked for me was to call invalidate() followed by init() which is called at the time of creation to initialize the view.

private void init() {
    setFocusable(true);
    setFocusableInTouchMode(true);
    setOnTouchListener(this);

    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mPaint.setColor(Color.BLACK);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(6);

    mCanvas = new Canvas();
    mPaths = new LinkedList<>();

    addNewPath();
}

Solution 11 - Android

With the following approach, you can clear the whole canvas or just a part of it.
Please do not forget to disable Hardware acceleration since PorterDuff.Mode.CLEAR doesn’t work with hardware acceleration and finally call setWillNotDraw(false) because we override the onDraw method.

//view's constructor
setWillNotDraw(false);
setLayerType(LAYER_TYPE_SOFTWARE, null);

//view's onDraw
Paint TransparentPaint = new Paint();
TransparentPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
canvas.drawRect(0, 0, width, height, TransparentPaint); 

Solution 12 - Android

Erasing on Canvas in java android is similar erasing HTML Canvas via javascript with globalCompositeOperation. The logic was similar.

U will choose DST_OUT (Destination Out) logic.

paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));

Note: DST_OUT is more useful because it can erase 50% if the paint color have 50% alpha. So, to clear completely to transparent, the alpha of color must be 100%. Apply paint.setColor(Color.WHITE) is recommended. And make sure that the canvas image format was RGBA_8888.

After erased, go back to normal drawing with SRC_OVER (Source Over).

paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));

Update small area display literally will need to access graphic hardware, and it maybe not supported.

The most close for highest performance is using multi image layer.

Solution 13 - Android

Don't forget to call invalidate();

canvas.drawColor(backgroundColor);
invalidate();
path.reset();

Solution 14 - Android

Try to remove the view at onPause() of an activity and add onRestart()

LayoutYouAddedYourView.addView(YourCustomView); LayoutYouAddedYourView.removeView(YourCustomView);

The moment you add your view, onDraw() method would get called.

YourCustomView, is a class which extends the View class.

Solution 15 - Android

In my case, I draw my canvas into linearlayout. To clean and redraw again:

	LinearLayout linearLayout = findViewById(R.id.myCanvas);
	linearLayout.removeAllViews();

and then, I call the class with the new values:

	Lienzo fondo = new Lienzo(this,items);
	linearLayout.addView(fondo);

This is the class Lienzo:

class Lienzo extends View {
	Paint paint;
	RectF contenedor;
	Path path;
	ArrayList<Items>elementos;

	public Lienzo(Context context,ArrayList<Items> elementos) {
		super(context);
		this.elementos=elementos;
		init();
	}

	private void init() {
		path=new Path();
		paint = new Paint();
		contenedor = new RectF();
		paint.setStyle(Paint.Style.FILL);
	}


	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		contenedor.left = oneValue;
		contenedor.top = anotherValue;
		contenedor.right = anotherValue;
		contenedor.bottom = anotherValue;

		float angulo = -90; //starts drawing at 12 o'clock
		//total= sum of all element values
		for (int i=0;i<elementos.size();i++){
			if (elementos.get(i).angulo!=0 && elementos.get(i).visible){
				paint.setColor(elementos.get(i).backColor);
				canvas.drawArc(contenedor,angulo,(float)(elementos.get(i).value*360)/total,true,paint);

				angulo+=(float)(elementos.get(i).value*360)/total;
			}
		} //for example
	}
}

Solution 16 - Android

In my case, creating canvas every time worked for me, even though it's not memory-friendly

Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.image);
imageBitmap = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), bm.getConfig());
canvas = new Canvas(imageBitmap);
canvas.drawBitmap(bm, 0, 0, null);

Solution 17 - Android

The following worked for me:

canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.SCREEN);

Solution 18 - Android

I found my solution.

PaintView class:

public void clear() {
    mPath.reset();
    mCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
    paths.clear();
}

And MainActivity:

  clear_canvas_.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            paintView.clear();
        }
    });

Solution 19 - Android

Your first requirement, how to clear or redraw whole canvas - Answer - use canvas.drawColor(color.Black) method for clearing the screen with a color of black or whatever you specify .

Your second requirement, how to update part of the screen - Answer - for example if you want to keep all other things unchanged on the screen but in a small area of screen to show an integer(say counter) which increases after every five seconds. then use canvas.drawrect method to draw that small area by specifying left top right bottom and paint. then compute your counter value(using postdalayed for 5 seconds etc., llike Handler.postDelayed(Runnable_Object, 5000);) , convert it to text string, compute the x and y coordinate in this small rect and use text view to display the changing counter value.

Solution 20 - Android

I had to use a separate drawing pass to clear the canvas (lock, draw and unlock):

Canvas canvas = null;
try {
    canvas = holder.lockCanvas();
    if (canvas == null) {
        // exit drawing thread
        break;
    }
    canvas.drawColor(colorToClearFromCanvas, PorterDuff.Mode.CLEAR);
} finally {
    if (canvas != null) {
        holder.unlockCanvasAndPost(canvas);
    }
}

Solution 21 - Android

Just call

canvas.drawColor(Color.TRANSPARENT)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsamClemView Question on Stackoverflow
Solution 1 - AndroidSileriaView Answer on Stackoverflow
Solution 2 - AndroidWroclaiView Answer on Stackoverflow
Solution 3 - AndroidRukmal DiasView Answer on Stackoverflow
Solution 4 - AndroidRakeshView Answer on Stackoverflow
Solution 5 - AndroidDerzuView Answer on Stackoverflow
Solution 6 - AndroidheMacView Answer on Stackoverflow
Solution 7 - AndroidaliakbarianView Answer on Stackoverflow
Solution 8 - AndroidHemant Chand DungriyalView Answer on Stackoverflow
Solution 9 - AndroidGuillaume BrunerieView Answer on Stackoverflow
Solution 10 - AndroidJason CrosbyView Answer on Stackoverflow
Solution 11 - AndroiducMediaView Answer on Stackoverflow
Solution 12 - AndroidphnghueView Answer on Stackoverflow
Solution 13 - Androidjazz chakrabortyView Answer on Stackoverflow
Solution 14 - AndroidRishabh JainView Answer on Stackoverflow
Solution 15 - AndroidCarlos GómezView Answer on Stackoverflow
Solution 16 - Androidn-yView Answer on Stackoverflow
Solution 17 - AndroidJohn DohertyView Answer on Stackoverflow
Solution 18 - AndroidBipul SheikhView Answer on Stackoverflow
Solution 19 - AndroidChanduView Answer on Stackoverflow
Solution 20 - AndroidFarid ZView Answer on Stackoverflow
Solution 21 - AndroidreznicView Answer on Stackoverflow