"Android" Loop progress bar implementation

Source: Internet
Author: User
Tags drawtext

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.