Android Canvas - Draw a Hole

Is it possible to implement a picture on Android with a canvas?

Picture with hole

I want to have a hole and not only a circle on the red layer, which is yellow. I tried this (and failed) with the following code in my onDraw() -Method:

 canvas.drawBitmap(yellow, 0, 0, paint); canvas.drawBitmap(red, 0, 200, paint); Paint p = new Paint(); p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); canvas.drawCircle(300, 300, radius, p); 

But when I use this code, it makes a hole through both bitmaps. In the end, this application should be a maze with a ball, holes and other things. When the ball hits the hole, it should appear under the red bit. Can this be realized?

Answer:

If someone should have the same problem: use View , not SurfaceView. This was my mistake because bg SurfaceView cannot be transparent.

+4
android canvas paint ondraw
source share
1 answer

I think you misunderstand how canvases / bitmaps work. Neither layers nor objects are stored (unless you store them). This is just a pixel representation of the displayed image. The yellow circle above the red square is what you showed in the above image.

If you really need a red layer, you need to collect two bitmaps. Draw a hole above the red square in one bitmap, draw a yellow layer in one bitmap. On the canvas, draw a yellow bitmap, then a β€œred square with a hole” bitmap.

+2
source share

All Articles