How to create a 'fog of war' bitmap overlay in Android

主宰稳场 提交于 2019-12-11 10:06:20

问题


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.

  1. Create a bitmap to hold the black overlay
  2. Fill bitmap with black
  3. Paint a transparent circle over it of where you whant to punch holes
  4. Paint the overlay over your game canvas
  5. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!