First on (the compressed size after the sawtooth, the original edge is very delicate feeding ~)
Characteristics:
1: Support ring with Word, ring without word (the middle cover on the circle picture, to achieve the effect of every day the player in the notification bar play progress), solid
2: Thread safe, no need to write handler to change UI
3: Custom size, color, border thickness
The code comes from the Internet and is modified on this basis
The idea is to create a sub-class of view, in the OnDraw method
1: Painting for the first time, drawing a round background
2: The second painting, according to the percentage to draw the arc progress ()
If the text is realistic, the text is centered.
Fortunately, the canvas object has provided us with a way to draw various shapes.
Draw a circle: drawcircle
Stooped: DrawText
Drawing radian: DrawArc
So the core code is drawn as follows: Full project download
@Overrideprotected voidOnDraw (canvas canvas) {Super. OnDraw (canvas); //Scale Background intCenter = (int) (GetWidth () * 0.5f); intRadius = (int) (Center-mhalfborder);//radius of the ringMpaint.setcolor (Mbackcolor);//set the color of the ringMpaint.setstyle (style);//Set HollowMpaint.setstrokewidth (Mborderwidth);//set the width of the ringMpaint.setantialias (true);//Anti- aliasingCanvas.drawcircle (center, center, Radius, mpaint);//Draw a circle intPercent = (int) (Value * 100f/max); //text if(Mmode = =Stroke_text) {Mpaint.setstrokewidth (0); Mpaint.setcolor (Mtextcolor); Mpaint.settextsize (mtextsize); Mpaint.settypeface (Typeface.default_bold); //Set Font floatTexthalfwidth = Mpaint.measuretext (Percent + "%") *0.5f; Canvas.drawtext (Percent+ "%", Center-texthalfwidth, center +texthalfsize, Mpaint); } //Progress if(value>0) {mpaint.setstrokewidth (mborderwidth); Mpaint.setcolor (Mfontcolor); RECTF Oval=NewRECTF (Center-radius, Center-radius, center + RADIUS, center +radius); intAngle = (int) (* percent/100f); Mpaint.setstyle (style); Canvas.drawarc (oval, startpos, Angle, Isfill, mpaint); } }
"Android" Loop progress bar implementation