54. The effect of rain on Android particles (rain effect)

Source: Internet
Author: User

ImportAndroid.content.Context;ImportAndroid.graphics.Canvas;ImportAndroid.util.AttributeSet;ImportAndroid.view.View;/*** Base class*/ Public Abstract classBaseviewextendsView {PrivateMyThread thread; Private Booleanrunning =true;  PublicBaseview (Context context, AttributeSet attrs) {Super(context, attrs); }     PublicBaseview (Context context) {Super(context); }    protected Abstract voiddrawsub (canvas canvas); protected Abstract voidlogic (); protected Abstract voidinit (); classMyThreadextendsThread {@Override Public voidrun () {init ();  while(running) {logic ();                Postinvalidate (); Try{Thread.Sleep (30); } Catch(interruptedexception e) {e.printstacktrace (); } }} @Overrideprotected Final voidOnDraw (canvas canvas) {if(Thread = =NULL) {Thread=NewMyThread ();        Thread.Start (); } Else{drawsub (canvas); }} @Overrideprotected voidOndetachedfromwindow () {Running=false; Super. Ondetachedfromwindow (); }}

1) Simply describe the behavior of individual raindrops.

The individual that draws the rain scene, raindrops (straight lines);

Let the line move up;

Dealing with boundary problems;

1   ImportAndroid.content.Context;2 ImportAndroid.graphics.Canvas;3 ImportAndroid.graphics.Paint;4 ImportAndroid.util.AttributeSet;5 6  Public classRainviewextendsBaseview {8     Private floatStartX;9     Private floatStarty;Ten     Private floatstopx; One     Private floatstopy; A     Private floatSizex; -     Private floatSizey; -     Privatepaint paint; the  -      PublicRainview (Context context, AttributeSet attrs) { -         Super(context, attrs); +         //angle.  -Sizex = 10; +Sizey = 30; A         //The following is the coordinates of the initialized line.  atStartX = 100; -Starty = 0; -         //change the angle of the line.  -STOPX = StartX +Sizex; -Stopy = Starty +Sizey; -  inPaint =NewPaint (); -         //set the line to white.  toPaint.setcolor (0xFFFFFFFF); +     } -  the      PublicRainview (Context context) { *         Super(context); $     }Panax Notoginseng  - @Override the     protected voiddrawsub (canvas canvas) { +         //Draw a line A canvas.drawline (StartX, Starty, stopx, stopy, paint); the     } +  -@Override/*let the line move.*/  $     protected voidlogic () { $  -         //magnification to change the speed by magnification.  -         floatopt = 0.5f; the  -StartX + = Sizex *opt;WuyiSTOPX + = Sizex *opt; the  -Starty + = Sizey *opt; WuStopy + = Sizey *opt; -  About         //when the line goes out of the screen, it becomes the initial position.  $         if(Starty >getheight ()) { -StartX = 100; -Starty = 0; -STOPX = StartX + 10; AStopy = Starty + 30; +         } the     } -  $}

2) Perfect raindrop behavior and tectonic rain scene.

Constructing Raindrop objects;

Raindrop size setting;

Speed setting;

The angle setting and so on add multiple raindrops;

3) Define the properties in the XML that can control the rain.

Extracting attributes that can be affected in XML;

Parsing style attributes in code and using them to control rain changes;

54. The effect of rain on Android particles (rain effect)

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.