Just learned the custom view, according to the Geek Academy's tutorial to do the particle rain effect, mainly used to draw lines and multithreading, in which the abstract class design method is worth learning,
1.baseview is the main set of raindrops to achieve the action, but first set, that is, abstract methods, in subclasses to implement its methods
2.Rainitems Package Raindrop Class
3.Rainitems set of raindrops is created into the panel, which is shown in the concrete implementation in this class
I. Baseview encapsulation class, the subclass inherits the implementation method can be
Public Abstract class baseview extends View { PrivateControl thread; Public Baseview(context context, AttributeSet attrs) {Super(context, attrs); } Public Baseview(Context context) {Super(context); }//package, construction screen, subclass inheritance needs to be rewritten protected Abstract void drawsub(Canvas canvas);//Package Move method, subclass inheritance needs to be overridden protected Abstract void Move();//Encapsulation initialization method protected Abstract void Init();@Override protected Final void OnDraw(Canvas canvas) {//Start thread if(Thread = =NULL) {thread =NewControl (); Thread.Start (); }Else{drawsub (canvas); } } Public class control extends Thread{ @Override Public void Run() {init (); while(true) {move ();//equivalent to refreshing the canvasPostinvalidate ();Try{Sleep ( -); }Catch(Interruptedexception e) {E.printstacktrace (); } } } }}
Two, Rainitem Raindrop class
Public class rainitem { Private intHeightPrivate intWidthPrivate floatStartX;Private floatStarty;Private floatSTOPX;Private floatStopy;Private floatSizex;Private floatSizey;Private floatof =0.5FPrivatePaint paint;PrivateRandom random =NewRandom (); Public Rainitem(intHeightintwidth) { This. height = height; This. width = width; Init (); } Public void Init() {//startx and Y correspond to the starting and ending positions.Sizex =1+ Random.nextint (Ten); Sizey =Ten+ Random.nextint ( -); StartX = Random.nextint (width); Starty = random.nextint (height); STOPX = StartX + Sizex; Stopy = Starty + Sizey; of = (float) (0.2+ random.nextfloat ()); Paint =NewPaint (); }/** * 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 the speed, so-called speed is the increase in the speed of the lineStartX + = Sizex*of; Stopx + = Sizex*of; Starty + = Sizey*of; Stopy + = Sizey*of;//re-run if the boundary is exceeded if(Starty>height) {init (); } }}
Three. Rainplay implementation-specific classes
Public class rainplay extends baseview {list<rainitem> list =NewArraylist<rainitem> ();//Control the number of raindrops Private intnum = the; Public Rainplay(Context context) {Super(context); } Public Rainplay(context context, AttributeSet attrs) {Super(context, attrs);//link with XMLTypedArray ta = context.obtainstyledattributes (attrs, R.styleable.rainview); num = Ta.getinteger (R.styleable.rainview_rainnum, the); Ta.recycle (); }@Override protected void drawsub(Canvas canvas) { for(Rainitem item:list) {Item.draw (canvas); } }@Override protected void Move() { for(Rainitem item:list) {Item.movestep (); } }/** * Because the acquisition of the length of the width is placed in the layout before it can be obtained, so need to * put inside the thread initialization */ @Override protected void Init() { for(inti =0; i < num; i++) {Rainitem item =NewRainitem (GetHeight (), getwidth ()); List.add (item); } }}
Four. Value and 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 android:layout_width= "match_parent" android:layout_height= "match_parent" android:background= "#ff000000" An:rainnum = /></framelayout>
’
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The realization of the effect of Android---particle rain