This article introduces the realization process of the particle rain effect of Android, and shares it for everyone's reference, the specific contents are as follows
First look at the effect chart:
Specific implementation methods:
1.baseview is mainly set to implement the action of raindrops, but first set, that is, abstract methods, in the subclass to implement its method
2.Rainitems Package Raindrop Series
3.Rainitems to the set of raindrops created into the panel, displayed, the concrete implementation is in this class
First, Baseview package class, subclass inheritance after the implementation method can be
public abstract class Baseview extends View {private control thread;
Public Baseview (context, AttributeSet attrs) {Super (context, attrs);
Public Baseview {Super (context);
}//encapsulation, constructs the picture, the subclass inherits needs to rewrite protected abstract void Drawsub (Canvas Canvas);
Encapsulates the Move method, which requires overriding the protected abstract void Move () after the subclass inherits;
The encapsulated initialization method protected abstract void init (); @Override protected final void OnDraw (Canvas Canvas) {//boot thread if (thread ==null) {thread = new control ()
;
Thread.Start ();
}else {drawsub (canvas);
Extends thread{@Override public void Run () {init ().
while (true) {move ();
Equivalent to refreshing the canvas postinvalidate ();
try {sleep (30);
catch (Interruptedexception e) {e.printstacktrace (); }
}
}
}
}
Two, Rainitem raindrops
public class Rainitem {private int height;
private int width;
private float startx;
private float Starty;
private float stopx;
private float stopy;
private float Sizex;
private float Sizey;
private float of = 0.5f;
Private Paint Paint;
Private Random Random = new Random ();
Public Rainitem (int height,int width) {this.height = height;
This.width = width;
Init ();
The public void init () {//startx and y corresponds to the starting and ending positions Sizex = 1 + random.nextint (10);
Sizey = + random.nextint (20);
StartX = Random.nextint (width);
Starty = random.nextint (height);
STOPX = StartX + Sizex;
Stopy = Starty + Sizey;
of = (float) (0.2 + random.nextfloat ());
Paint = new paint (); /** * Painting Raindrops * @param canvas/public void draw (canvas canvas) {PAINT.SETARGB (255, Random.nextint (255),
Random.nextint (255), Random.nextint (255));
Canvas.drawline (StartX, Starty, stopx, stopy, paint); /** * The movement behavior of raindrops * * public void MOvestep () {//size*of This is used to control speed, the so-called speed is the line increase speed startx + = sizex*of;
Stopx + = Sizex*of;
Starty + = Sizey*of;
Stopy + = Sizey*of;
Rerun if (starty>height) {init () if the boundary is exceeded;
}
}
}
Three, Rainplay specifically implemented classes
public class Rainplay extends Baseview {list<rainitem> List = new Arraylist< ;
Rainitem> ();
Control the number of raindrops private int num = 80;
Public Rainplay {Super (context);
Public Rainplay (context, AttributeSet attrs) {Super (context, attrs);
Linked with XML TypedArray TA = Context.obtainstyledattributes (attrs, R.styleable.rainview);
num = Ta.getinteger (r.styleable.rainview_rainnum,80);
Ta.recycle ();
} @Override protected void Drawsub (Canvas Canvas) {for (Rainitem item:list) {Item.draw (Canvas);
}} @Override protected void Move () {Rainitem item:list) {item.movestep (); }/** * Because get the long width is placed in the layout before it can be acquired, so need * put the thread inside initialization/@Override protected void init () {for (int i = 0 ; i < num;
i++) {Rainitem item = new Rainitem (GetHeight (), getwidth ());
List.add (item); }
}
}
Iv. value vs. xml file
<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<declare-styleable name = "Rainview" >
<attr name= "rainnum" format= "integer"/>
</declare-styleable>
</resources>
<framelayout xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:tools= "http:// Schemas.android.com/tools "
xmlns:an=" Http://schemas.android.com/apk/res/com.niuli.Rain "
android: Layout_width= "Match_parent"
android:layout_height= "match_parent"
>
< Com.niuli.Rain.Rainplay
android:layout_width= "match_parent"
android:layout_height= "Match_parent"
android:background= "#ff000000"
an:rainnum = "/>"
</FrameLayout>
I hope this article will help you learn about Android software programming.