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)