Android simulated snow Effect

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.