3

I've seen multiple posts about drawing a hole over a bitmap, however that doesn't seem to work when trying to draw over a filled area, such as a rectangle.

For example:

  1. I'm drawing a background with canvas.drawRect() and canvas.drawText()

enter image description here

  1. I'm drawing a red rectangle over the background with canvas.drawPaint() or canvas.drawRect()

enter image description here

  1. What I'm trying to achieve: Draw a circle that will reveal the background (hole)

enter image description here

Any suggestions? Thanks in advance!

Similar questions: Android canvas - Draw a hole & Erase bitmap parts using PorterDuff mode however the proposed solutions do not work when trying to draw over a rectangle instead of a bitmap. (i.e. I create the background dynamically using canvas.drawRect(), canvas.drawLines(), etc., I do not have a static bitmap).

Community
  • 1
  • 1
steliosf
  • 3,669
  • 2
  • 31
  • 45
  • @DoronYakovlev-Golani I've already checked that question, however the OP is drawing a hole over a bitmap, while I'm trying to draw a hole over a canvas dynamic background (lines, rectangles, etc.). – steliosf May 13 '17 at 16:19
  • Can you add an extra layer (an extra View in between)? The bottom layer can be any dynamic drawing and the top layer an overlay with a "hole". – Doron Yakovlev Golani May 15 '17 at 12:43
  • I ended up creating a new bitmap as an overlay where I draw the background and the hole and it worked. I would prefer to solve that without the need of a bitmap, but it seems that there's no other way. You need to create a new bitmap, link a new canvas to that bitmap, draw on that canvas whatever you want as the background, then draw a hole, then add that bitmap to your initial canvas. If you draw on the initial canvas directly you will end up with a black opaque circle, that's why you need an overlay bitmap. I'll mark it as a duplicate, although I would still prefer a more efficient solution. – steliosf May 15 '17 at 18:44

0 Answers0