问题
I have a simple 2D game with a handful of characters which move around the screen. I'd like to create a fog of war, whereby only the background around each character is visible.
Hopefully that's clear (not too foggy?!?!). My thoughts were to create a black bitmap, then draw a 'transparent' disk centered on each of the characters. Then I can draw the background, draw the characters and finally draw this overlay bitmap over the top.
Just not sure how to draw the 'transparent' disks on to the black bitmap.
Any ideas, or alternative designs?
回答1:
Use Canvas.drawCircle() with a Paint object whose alpha is set to 0. Alternatively you could create a transparent circle with a Bitmap and draw it just like another bitmap. The Bitmap based circle would be more advantageous if you wanted strong control with a fade to black.
- Create a bitmap to hold the black overlay
- Fill bitmap with black
- Paint a transparent circle over it of where you whant to punch holes
- Paint the overlay over your game canvas
- Repeat.
回答2:
Actually, since the fog-of-war needs to be persistent, I've had to go for an opaque tiled solution, rather than a straight bitmap, as the bitmap was too large and took too long to load/save between activations.
来源:https://stackoverflow.com/questions/7289750/how-to-create-a-fog-of-war-bitmap-overlay-in-android