Android Custom View-pie chart

Source: Internet
Author: User

Recently saw a custom sub-circle view, trying to do a similar one, as shown in (1):

Figure (1)

Implementation method: Custom view-colorcircle, need to know the value of the circle radius, the number of equal and sector color.

    /*** Define several colors*/    Private Static intColor[] ={color.red, Color.Blue, Color.green, Color.yellow, color.black}; /*** The default number of circles*/    Private Static intDiv_size = 3; PrivatePaint Mpaint; /**
* Circle Default Radius
*    /Private Static Final intDefault_radius = 200; Private intMradius =Default_radius;
Publiccolorcircle (Context context) { This(Context,NULL); } Publiccolorcircle (Context context, @Nullable AttributeSet attrs) { This(context, Attrs, 0); } PublicColorcircle (context context, @Nullable AttributeSet attrs,intdefstyleattr) { Super(context, attrs, defstyleattr); Mpaint=NewPaint (); Mpaint.setstyle (Paint.Style.FILL); }

In Onmeasure we need to recalculate the size of the colorcircle view and the radius of the circle based on Widthmeasurespec & Heightmeasurespec (because the default circle may be larger than the height or width of the view).

@Overrideprotected voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) {        Super. Onmeasure (Widthmeasurespec, Heightmeasurespec); intWidthmode =Measurespec.getmode (WIDTHMEASURESPEC); intWidthsize =measurespec.getsize (WIDTHMEASURESPEC); intHeightmode =Measurespec.getmode (HEIGHTMEASURESPEC); intHeightsize =measurespec.getsize (HEIGHTMEASURESPEC); intwidth; intheight; if(Widthmode = =measurespec.exactly) {width=widthsize; } Else{width= Mradius * 2 + getpaddingleft () +getpaddingright (); if(Widthmode = =measurespec.at_most) {Width=math.min (width, widthsize); }        }        if(Heightmode = =measurespec.exactly) {height=heightsize; } Else{Height= Mradius * 2 + getpaddingtop () +Getpaddingbottom (); if(Heightmode = =measurespec.at_most) {Height=math.min (width, heightsize);        }} setmeasureddimension (width, height); Mradius= (int) (Math.min (Width-getpaddingleft ()-getpaddingright (), Height-Getpaddingtop ()-Getpaddingbottom ()) * 1.0F/2); }

Finally, the fan is drawn in OnDraw by Canvas.drawarc ().

@Overrideprotected voidOnDraw (canvas canvas) {Super. OnDraw (canvas);
Pans the canvas to the center of the screen, then draws the center point as the initial point canvas.translate (GetWidth ()+ Getpaddingleft ()-getpaddingright ())/2,
(GetHeight () + getpaddingtop ()-Getpaddingbottom ())/2);
//Defines a RECTF object that represents the sector drawing area RECTF Oval = new RECTF (-mradius,-mradius, Mradius, Mradius); floatFirstangle = 0.0f; floatDivideangle = (* 1.0f)/div_size;//The angle of each fan according to the Div_size . for(inti=0; i<div_size; i++) {Mpaint.setcolor (color[i]); Canvas.drawarc (Oval, (Firstangle+ i * divideangle), Divideangle,true, Mpaint); } }

Public void setdivsize (int size) {
Div_size = SIZE;
Invalidate ();
}

public int Getdivsize () {
Div_size = SIZE;
}

Finally, a setdivsize () interface is also reserved for easy customization of colorcircle view dynamic fan number. I am here to dynamically switch div_size through the Seekbar.

Mcolorcircle =(colorcircle) Findviewbyid (r.id.color_circle); Mseekbar=(SeekBar) Findviewbyid (R.id.seek_bar); Mseekbar.setmax (4);//Because of the number of colors, here the maximum value of Seekbar is set to 4.
int pro = Mcolorcircle.getsize (); Mseekbar.setprogress (Pro); Mseekbar.setonseekbarchangelistener (NewSeekbar.onseekbarchangelistener () {@Override Public voidOnprogresschanged (SeekBar SeekBar,intProgressBooleanFromuser) {Mcolorcircle.setdivsize (Progress+ 1); } @Override Public voidOnstarttrackingtouch (SeekBar SeekBar) {} @Override Public voidOnstoptrackingtouch (SeekBar SeekBar) {}});

As follows:

Android Custom View-pie chart

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.