Source code and executable file: Http://files.cnblogs.com/rainboy2010/ProgressChart.zip
Because of the project needs, I try to define a progress loop to show the progress, the effect is as follows:
The main code is as follows:
public class Progresschart extends View{private context context;//ring background brush private paint paintbg;//ring Progress brush Private Paint PA intprogress; Text Brush 1 private Paint paintText1; Text Brush 2 private Paint paintText2; Width of the ring private float progresswidth; Area of the ring private RECTF roundrect; The area of the text private Rect textrect; A single-character region private Rect charrect; The degree at which each increment is drawn is the private float rotatedegree = 1.0F; The drawing begins with the degree of private float startdegree = 0.0F; The degree of the end of the private float enddegree; Background color private int bgColor; Progress color private int progresscolor; The color of the middle progress percent of the string is private int textcolor; The text size of the string private float text1size; The text size of the string private float text2size; Draw the string private string text= "0%"; Public Progresschart (Context context) {This (context, null);} Public Progresschart (context context, AttributeSet Attrs) {This (context, attrs, 0);} Public Progresschart (ContExt context, AttributeSet Attrs,int defstyleattr) {Super (context, attrs, defstyleattr); This.context=context;init ( ATTRS);} private void init (AttributeSet attrs) {TypedArray Mtypedarray = context.obtainstyledattributes (Attrs, R.styleable.progresschart); This.progresswidth = Mtypedarray.getdimension (r.styleable.progresschart_progresswidth, 8); This.text1size = Mtypedarray.getdimension (r.styleable.progresschart_textsize1, 32); This.text2size = Mtypedarray.getdimension (R.styleable.progresschart_textsize2, 20); This.bgcolor = Mtypedarray.getcolor (R.styleable.progresschart_bgcolor,color.parsecolor ("#fff2f2f2")); This.progresscolor = Mtypedarray.getcolor (R.styleable.progresschart_progresscolor,color.parsecolor ("#fffd0000")) ; This.textcolor = Mtypedarray.getcolor (R.styleable.progresschart_txtcolor,color.parsecolor ("#fffd0000")); Mtypedarray.recycle (); This.textrect = new Rect (); This.charrect = new Rect (); THIS.PAINTBG = new Paint (); This.paintBg.setStyle (Paint.Style.STROKE); This.paintBg.setStrokeWidth (This.progresswidth); This.paintBg.setColor (This.bgcolor); this.paintprogress = new Paint (); This.paintProgress.setStyle (Paint.Style.STROKE); This.paintProgress.setStrokeWidth (This.progresswidth); This.paintProgress.setColor (This.progresscolor); This.painttext1 = new Paint (); This.paintText1.setTextSize (this.text1size); This.paintText1.setTextAlign (Paint.Align.CENTER); This.paintText1.setColor (This.textcolor); THIS.PAINTTEXT2 = new Paint (); This.paintText2.setTextSize (this.text2size); This.paintText2.setTextAlign (Paint.Align.CENTER); This.paintText2.setColor (This.textcolor);} public Boolean setprogress (String progress) {This.text = Decimalformat.getpercentinstance (). Format (double.valueof ( Progress)); This.startdegree = 0.0F; This.enddegree = (360.0F * float.valueof (Progress). Floatvalue ()); This.rotatedegree = (this.enddegree/40.0f); Invalidate (); return true;} @Overrideprotected void OnDraw (canvas canvas) {//Draw ring background Canvas.drawarc (This.roundrect, 0.0F, 360.0F, False, THIS.PAINTBG); /Draw Progress if (This.startdegree < This.enddegree) {Canvas.drawarc (This.roundrect, -90.0f, This.startdegree, False, this.paintprogress); This.startdegree + = This.rotatedegree; Invalidate (); } else {Canvas.drawarc (This.roundrect, -90.0f, This.enddegree, False, this.paintprogress); } if (! Textutils.isempty (This.text)) {//Draw text This.paintText1.getTextBounds (this.text, 0, This.text.length (), this.textr ECT); This.paintText2.getTextBounds ("%", 0, 1, this.charrect); Fontmetricsint fontmetricsint = This.paintText1.getFontMetricsInt (); Float y = this.roundRect.top + (This.roundrect.bottom-this.roundrect.top-fontmetricsint.bottom + fontmetricsint.top)/ 2.0f-5 * FONTMETRICSINT.TOP/5; Canvas.drawtext (This.text.replace ("%", ""), This.roundRect.centerX ()-This.charRect.width ()/2, y, this.painttext1); CaNvas.drawtext ("%", This.roundRect.centerX () + this.textRect.width ()/2-this.charrect.width ()/2, y, this.painttext2); }} @Override protected void onsizechanged (int w, int h, int oldw, int oldh) {this.roundrect = new RECTF (This.prog Resswidth, This.progresswidth, W-this.progresswidth, h-this.progresswidth);}}
Android One custom progress ring: Progresschart