Android self-defined control-circular progress bar (with figure Diao in the middle)

Source: Internet
Author: User

Smart Home is more and more popular, in the smart home we often want to show some of the percentage of data circular bar in the middle plus a picture is a very popular definition of the view

1. First step you need to inherit the Class View

View
2. The second step is to implement three construction methods and the method of constructing the current multi-parameter call with fewer parameters in front

 PublicCircleprogressimageview (Context context) { This(Context, NULL);} PublicCircleprogressimageview (Context Context, AttributeSet attrs) { This(Context, Attrs,0);} PublicCircleprogressimageview (Context Context, AttributeSet Attrs, intDEFSTYLEATTR) {Super(Context, Attrs, DEFSTYLEATTR);    Init (context,Attrs,DEFSTYLEATTR);}
3. Step three: Take your own definition of attributes and initialize the brushes, etc.

private voidInit(Context Context, AttributeSet Attrs, intDEFSTYLEATTR) { This.Context=context;    /*** Get your own definition properties     */    TypedArrayA=context.obtainstyledattributes (attrs,r.styleable.Circleprogressimageviewattrs);    Bitmap=a.getresourceid (r.styleable.circleprogressimageviewattrs_imagers,R.mipmap.Ic_launcher);    /*** Convert picture resources to bitmap objects     */    Drawbitmap=bitmapfactory.Decoderesource(Context.getresources (),Bitmap);    /*** Initialize RECTF object     */    MRECTF=NewRECTF ();    Mpaint=NewPaint ();    Mpaint. Setantialias (true);}
4. The fourth step:onmeasureheight and width are handled in the method

protected voidonmeasure(intWidthmeasurespec, intHEIGHTMEASURESPEC) {Super. Onmeasure (Widthmeasurespec, HEIGHTMEASURESPEC);    /*** Get the current view width and height     */    width= This. GetWidth ();    Height= This. GetHeight ();    /*** To handle the left and right up and down     */    MRECTF. Left=Mcirclestorewidth/2;    MRECTF.Top=Mcirclestorewidth/2;    MRECTF. Right=width-Mcirclestorewidth/2;    MRECTF.Bottom=width-Mcirclestorewidth/2;}

5. We need to draw the OnDraw () method at this time

protected voidOnDraw(CanvasCanvas) {Super. OnDraw (Canvas);    Canvas.drawcolor (Color.TRANSPARENT);    //Draw round Beijing    Mpaint. SetColor (Getresources (). GetColor (R.color.Orange));    Mpaint. SetStyle (Paint.style.STROKE);    Mpaint. Setstrokewidth (Mcirclestorewidth);    Canvas.drawarc (MRECTF,- -, the, False,Mpaint);    /*** Progress display of drawing arcs     */    Mpaint. SetColor (Getresources (). GetColor ((R.color.Gray)));    Canvas.drawarc (MRECTF,- -,((float)Mprocessvalue/Mmaxprocessvalue)* the, False,Mpaint);    Log.D(TAG,((float)Mprocessvalue/Mmaxprocessvalue)* the+"");    /*** Drawing the middle of the picture     */    floatimageleft=width/2-Drawbitmap. GetWidth ()/2;floatimagetop=Height/2-Drawbitmap. GetHeight ()/2;    Canvas.drawbitmap (Drawbitmap,Imageleft,Imagetop,Mpaint);}
So we're going to achieve a nice and simple definition of the view own definition properties References other articles this is not the case here.

But this view is not going to turn. Only through mainactivity settings in the thread setmprocessvalue (processvalue) The call changes the value to be able to turn.

Source code Download


Android self-defined control-circular progress bar (with figure Diao in the middle)

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.