1. Achieve the effect: Dynamic display of progress (showing the entire dynamic change process, and then complete, pop up a dialog box)
2. Implementation process: Can be divided into three parts of drawing a circle, arc and text, and then in the mainacitivity through the thread simulation download progress.
A. Define a class to inherit to view and add its constructor, remember to add a constructor containing the attributset parameter;
B. Define and initialize some of the data:
private int mcirclexy; private int mwidth; private float Mradius; Private Paint mcirclepaint,marcpaint,mtextpaint; Private String mshowtext= "Android"; private int mtextsize=15; private int msweepangle=0;
C. Get the width of the current screen in the constructor and call the custom Initview function
Public MyView (Context context, AttributeSet attrs) { Super(context, attrs); // Get screen height WindowManager manager= (WindowManager) GetContext (). Getsystemservice (Context.window_service); Mwidth=manager.getdefaultdisplay (). getwidth (); Initview ();}
D. Custom functions, mainly to complete the definition of circles, arcs and text brushes
Public voidInitview () {//Set the center position and radius of the circle]Mcirclexy=mwidth/2; Mradius=(float) (mwidth*0.5)/2; //set a brush for a circleMcirclepaint=NewPaint (); Mcirclepaint.setcolor (Color.Blue); //set the brush for an arcMarcpaint=NewPaint (); //Set line widthMarcpaint.setstrokewidth (60); //Set StyleMarcpaint.setstyle (Paint.Style.STROKE); //Set ColorMarcpaint.setcolor (Color.Blue); //set the text brushMtextpaint=NewPaint (); Mtextpaint.setcolor (Color.White); Mtextpaint.settextsize (mtextsize); Mtextpaint.settextalign (Paint.Align.CENTER);}
E. draw circles, arcs, and text in the OnDraw function:
protected voidOnDraw (canvas canvas) {Super. OnDraw (canvas); //Draw a matrix//Here are the left, top, right, and bottom positions of the matrix set separatelyRECTF marcrectf=NewRECTF ((float) (mwidth*0.1), (float) (mwidth*0.1), (float) (mwidth*0.9), (float) (mwidth*0.9)); //Draw a circlecanvas.drawcircle (Mcirclexy,mcirclexy,mradius,mcirclepaint); //Draw an arc /** The first parameter for the matrix constructed above, can actually be understood as in the specified matrix inside the drawing arc * The second parameter is the starting angle, here is set to 270 * The third argument is the end of the angle * here is generally set to false, if true, it will show the half of the arc Until the drawing is complete, * The last parameter sets the brush for US **/Canvas.drawarc (MARCRECTF,270,msweepangle,false, Marcpaint); //Draw Text /** The first parameter shows us the contents * The second parameter is the position where the start is displayed * The third parameter is the position of the end display * The fourth parameter is the x-axis position of the text display, which is the position of the y-axis * The last parameter for our defined brush **/Canvas.drawtext (Mshowtext,0,mshowtext.length (), mcirclexy,mcirclexy+ (MTEXTSIZE/4), mtextpaint);}
View Code
F. Customize two functions to dynamically change angles and text in other places:
Public voidSetsweepangle (intsweepvalue) { if(sweepvalue!=0) {Msweepangle=Sweepvalue; }Else{Msweepangle=0; } //note here to add, refreshinvalidate ();} Public voidSetshowtext (String text) {if(!text.equals ("") ) {Mshowtext=text; }Else{Mshowtext= "Android"; } invalidate ();}
G. Referencing in the layout file:
<main.view.com.myview.MyView android:id= "@+id/myview" android:layout_width= "Match _parent " android:layout_height=" Match_parent "/>
H. Implementation in Mainactivity:
PackageMain.view.com.myview;ImportAndroid.content.DialogInterface;ImportAndroid.os.Handler;ImportAndroid.support.v7.app.AlertDialog;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle; Public classMainactivityextendsappcompatactivity {PrivateMyView MyView; Private intMangle=0; Private floatCurrent=0; Handler Handler=NewHandler (); Runnable Runnable=NewRunnable () {@Override Public voidrun () {//Convert to percentileCurrent= (float) mangle/360*100; //set angle and text, respectivelyMyview.setshowtext ("Current ratio is:" + (int) (current); Myview.setsweepangle (MAngle); MAngle=mangle+20; if(mangle<=360){ //when not done, run the thread again after 3 secondsHandler.postdelayed (runnable,3000); }Else{ //When you are finished, the dialog box pops upAlertdialog.builder builder=NewAlertdialog.builder (mainactivity. This); Builder.setmessage ("Download done!" "). Setpositivebutton (" OK ",NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {}}). Show (); } } }; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); MyView=(MyView) Findviewbyid (R.id.myview); Handler.post (runnable); }}
View Code
Android Elite Biography Series Three view customization: Implementing a simulation download