Custom properties
<!--Roundcolor The color of the ring Roundprogresscolor progress color roundwidth The width of the ring textcolor text color textSize text size Max Max Textisdisplayable whether to show progress text style style STROKE Hollow FILL solid - <declare-styleablename= "Roundprogressbar"> <attrname= "Roundcolor"format= "Color"/> <attrname= "Roundprogresscolor"format= "Color"/> <attrname= "Roundwidth"format= "Dimension"></attr> <attrname= "TextColor"format= "Color" /> <attrname= "TextSize"format= "Dimension" /> <attrname= "Max"format= "integer"></attr> <attrname= "Textisdisplayable"format= "Boolean"></attr> <attrname= "Style"> <enumname= "STROKE"value= "0"></enum> <enumname= "FILL"value= "1"></enum> </attr> </declare-styleable>
PublicRoundprogressbar (context context, AttributeSet attrs,intdefstyleattr) { Super(context, attrs, defstyleattr); Mpaint=NewPaint (); /*** Get Custom properties*/TypedArray TypedArray=context.obtainstyledattributes (Attrs, R.styleable.roundprogressbar); //backgroundMroundcolor =Typedarray.getcolor (R.styleable.roundprogressbar_roundcolor, color.red); //Color of ProgressMroundprogresscolor =Typedarray.getcolor (R.styleable.roundprogressbar_roundprogresscolor, Color.Blue); //the width of the circleMroundwidth = Typedarray.getdimension (R.styleable.roundprogressbar_roundwidth, 20); //middle of Font colorMtextcolor =Typedarray.getcolor (R.styleable.roundprogressbar_textcolor, Color.Blue); //the font size of the intermediate progress displayMtextsize = Typedarray.getdimension (r.styleable.roundprogressbar_textsize, 15); //Maximum ValueMmax = Typedarray.getinteger (R.styleable.roundprogressbar_max, 100); //whether the text is displayedmtextisdisplayable = Typedarray.getboolean (r.styleable.roundprogressbar_textisdisplayable,true); //Solid or hollowMstyle = Typedarray.getint (r.styleable.roundprogressbar_style, 0); Typedarray.recycle (); }
Draw
//Center intCenterofcircle = GetWidth ()/2; //radius radius intRadius = (int) (CENTEROFCIRCLE-MROUNDWIDTH/2); //Set BrushMpaint.setantialias (true); //the color of the ringMpaint.setcolor (Mroundcolor); //Set HollowMpaint.setstyle (Paint.Style.STROKE); //Brush Widthmpaint.setstrokewidth (mroundwidth); //Draw a circlecanvas.drawcircle (centerofcircle, centerofcircle, radius, mpaint); /*** Draw Percent*/Mpaint.setstrokewidth (0); //Font Sizempaint.settextsize (mtextsize); //Brush ColorMpaint.setcolor (Mtextcolor); //FontMpaint.settypeface (Typeface.default_bold); //Calculate Percentage intPercent = (int) (((float) Mprogress/(float) mmax) * 100); //measure the width of a font floatTextWidth = Mpaint.measuretext (Percent + "%")); //determines whether the progress text is displayed is not 0, style is hollow if(mtextisdisplayable && percent! = 0 && Mstyle = =STROKE) {Canvas.drawtext (Percent+ "%", CENTEROFCIRCLE-TEXTWIDTH/2, Centerofcircle + TEXTWIDTH/2, Mpaint); } /*** Set Progress*/Mpaint.setcolor (Mroundprogresscolor); //Brush Widthmpaint.setstrokewidth (mroundwidth); Mpaint.setantialias (true); RECTF Oval=NewRECTF (Centerofcircle-radius, Centerofcircle-radius, centerofcircle + radius, centerofcircle +radius); Switch(mstyle) { CaseSTROKE://HollowMpaint.setstyle (Paint.Style.STROKE); //drawing Arcs /*** * Starting angle*/Canvas.drawarc (Oval,The Mprogress/mmax,false, Mpaint); Break; CaseFILL://SolidMpaint.setstyle (Paint.Style.FILL_AND_STROKE); //drawing Arcs if(mprogress!=0) {Canvas.drawarc (oval,The Mprogress/mmax,true, Mpaint); } Break; }
Source:
Https://github.com/ln0491/ProgressDemo
Android Progress Bar Learning