It is common to use custom view to override the OnDraw () function, and if you need to use the wrap_content style, you also need to override the Onmeasure function.
Usually we draw a circle by drawing two different circles with the same color as the center of a radius
The following describes using the Canvas.drwarc () function to draw a circle (or arc)
canvas.drawArc(rectF,startDegree,60,false,mPaint);
The function has four parameters
Parameter 1: An RECTF type object that represents the coordinates of the bounding rectangle of the drawn ellipse. (The difference between RECTF and rect is that the coordinates are recorded in the float parameter and rect records the int parameter)
Parameter 2:startdegree:int type, which represents the angle at which to start drawing
Parameter 3:60:int type, how many angles to draw
The 4:false:boolean type of the parameter indicating whether to draw a line from the center to the arc
Parameter 5:mpaint:paint type, drawing brush
To draw a ring you need to set the angle of the drawing to 360 degrees, and the parameter 4 to False (no lines to draw to the center of the circle), in addition:
The style of the mpaint needs to be set to
//Paint.Style.FILL :填充内部//Paint.Style.FILL_AND_STROKE :填充内部和描边//Paint.Style.STROKE :仅描边mPaint.setStyle(Paint.Style.STROKE);mPaint.setStrokeWidth(100);//描边宽度
A complete example:
Public class circleview extends View { Public Circleview(Context context) { This(Context,NULL); } Public Circleview(context context, AttributeSet attrs) { This(Context, Attrs,0); } Public Circleview(context context, AttributeSet attrs,intDEFSTYLEATTR) {Super(Context, attrs, defstyleattr); Mpaint=NewPaint (); Mpaint.setstyle (Paint.Style.STROKE); Mpaint.setstrokewidth ( -); mpaint1=NewPaint (); Mpaint1.setstyle (Paint.Style.STROKE); Mpaint1.setstrokewidth ( -); Textpaint=NewPaint (); Textpaint.settextsize ( -); Textpaint.setcolor (Color.Black); }@Override protected void onmeasure(intWidthmeasurespec,intHEIGHTMEASURESPEC) {//super.onmeasure (Widthmeasurespec, heightmeasurespec); intSize=measurespec.getsize (WIDTHMEASURESPEC);intMode=measurespec.getmode (WIDTHMEASURESPEC);intresult=0;if(mode==measurespec.exactly) {result=size; }Else{result= $;if(mode==measurespec.at_most) result=math.min (result,size); } setmeasureddimension (Result,result); LinearGradient gradient=NewLinearGradient (0,0, Getmeasuredheight (), Getmeasuredwidth (),New int[]{color.red,color.yellow,color.blue},NULL, Shader.TileMode.CLAMP); Sweepgradient sweep=NewSweepgradient (0,0,New int[]{color.yellow,color.green,color.blue,color.red},NULL); Sweepgradient sweep1=NewSweepgradient (0,0,New int[]{color.red,color.green,color.blue,color.red},NULL); Mpaint.setshader (sweep); Mpaint1.setshader (SWEEP1); }PrivatePaint Textpaint;PrivatePaint Mpaint,mpaint1;Private intStartdegree= the;@Override protected void OnDraw(Canvas canvas) {Super. OnDraw (canvas); startdegree+=Ten; Startdegree=startdegree% the;floatWidth=getmeasuredwidth ();floatradias=width/2; RECTF rectf=NewRECTF ((float) (width*0.1), (float) (width*0.1), (float) (width*0.9), (float) (width*0.9)); Canvas.drawarc (Rectf,startdegree, -,false, Mpaint); Canvas.drawarc (rectf,startdegree+ the, -,false, Mpaint); Canvas.drawarc (rectf,startdegree+ the, -,false, mPaint1); Canvas.drawarc (rectf,startdegree+ -, -,false, mPaint1); String str="Hello";floatTextsize=textpaint.gettextsize (); Canvas.drawcircle (Radias, Radias, (float) (Radias *0.2), Mpaint); Canvas.drawtext (str,radias-(Textsize*str.length ()/2), Radias,textpaint); Postinvalidatedelayed ( -); }}
android--Custom Ring View