I recently simulated an android game and saw a snowing effect. I searched the internet for a long time and found that most of them were simulated by the particle effects of search engines.
I have never used the android game engine, so I decided to write it myself.
I found a demo of "female sanhua" on the Internet, and it was converted to this snowy demo. The female is scattered; click to open the link
The snow effect is nothing more than making the "Snowflake" images on the Screen repeatedly repainted.
The snow action here needs to be controlled by a thread.
/** Updates the interface to realize snow */private refreshhandler mredrawhandler = new refreshhandler (); Class refreshhandler extends handler {@ overridepublic void handlemessage (Message MSG) {// snow. addrandomsnow (); snow. invalidate (); sleep (100);} public void sleep (long delaymillis) {This. removemessages (0); sendmessagedelayed (obtainmessage (0), delaymillis );}};
When the thread starts, it initializes the snowflake image and loads the image into the memory.
Then, let the thread re-paint the image at intervals. During re-painting, the distance between the Y axis is increased to snow.
@ Overridepublic void ondraw (canvas) {super. ondraw (canvas); For (INT x = 0; x <10; x + = 1) {If (snows [X]. y> = view_height) {snows [X]. y = 0; addrandomsnow ();} // the speed at which the snowflake falls. Snows [X]. Y + = 5; // If (RNG. nextboolean () {// randomly generates a number to make the snowflake move horizontally. Int RNG = RNG. nextint (3); snows [X]. X + = 2-RNG;} canvas. drawbitmap (bitmap_snows [x/6], (float) snows [X]. x), (float) snows [X]. y), mpaint );}}
The initial position of the snowflake is the vertex of the Y axis. I have set 10 snowflakes here. At the beginning, 10 numbers are randomly generated in the X axis direction, which serves as the starting position of the snowflake and then the X axis.
When the snow float out of the screen, the Y coordinate is reset to 0, and the X coordinate is also re-generated. To achieve the smart Effect of snowflake, a random X-axis floating effect is generated.
Of course, the effect is not very good, you can optimize it: for example, add more snowflake images to achieve the effect of nearly distant snow.
If you are interested, please add ......
: Android snow demo