First look at the effect:
Analysis: More common in the scan results, progress bar and other scenarios
Using Canvas.drawarc (RECTF Oval, float startangle, float SweepAngle, boolean usecenter, paint paint ) to draw an arc
Some properties of paint define weight, color, style, etc.
LinearGradient to implement color line gradients
In the same way, you can draw a strip of progress bar, fan pie chart, etc., interested can try.
PackageCom.liujing.progressviewdemo;/*** * Custom ARC Progress bar * *@authorliujing*/ Public classProgressviewextendsView {//Segment Color Private Static Final int[] Section_colors ={color.green, color.yellow, color.red}; Private Static FinalString[] Alarm_level = {"Safe", "low risk", "medium danger", "high risk" }; Private floatMaxCount; Private floatCurrentcount; Private intscore; PrivateString Crrentlevel; PrivatePaint Mpaint; PrivatePaint Mtextpaint; Private intMwidth, Mheight; PublicProgressview (context context, AttributeSet attrs,intdefstyleattr) { Super(context, attrs, defstyleattr); Init (context); } PublicProgressview (Context context, AttributeSet attrs) { This(context, Attrs, 0); } PublicProgressview (Context context) { This(Context,NULL); } Private voidinit (Context context) {Mpaint=NewPaint (); Mtextpaint=NewPaint (); } @Overrideprotected voidOnDraw (canvas canvas) {Super. OnDraw (canvas); Initpaint (); RECTF RECTBLACKBG=NewRECTF (mWidth-20, mHeight-20); Canvas.drawarc (RECTBLACKBG,0, 360,false, Mpaint); Mpaint.setcolor (Color.Black); Canvas.drawtext (Score+ "Min", MWIDTH/2, MHEIGHT/2, Mtextpaint); Mtextpaint.settextsize (40); if(Crrentlevel! =NULL) {Canvas.drawtext (crrentlevel, Mwidth/2, MHEIGHT/2 + 50, Mtextpaint); } floatSection = Currentcount/MaxCount; if(Section <= 1.0f/3.0f) { if(Section! = 0.0f) {Mpaint.setcolor (section_colors[0]); } Else{mpaint.setcolor (color.transparent); } } Else { intCount = (Section <= 1.0f/3.0f * 2)? 2:3; int[] Colors =New int[Count]; System.arraycopy (Section_colors,0, colors, 0, Count); float[] positions =New float[Count]; if(Count = = 2) {positions[0] = 0.0f; positions[1] = 1.0f-positions[0]; } Else{positions[0] = 0.0f; positions[1] = (MAXCOUNT/3)/Currentcount; positions[2] = 1.0f-positions[0] * 2; } positions[positions.length-1] = 1.0f; LinearGradient Shader=NewLinearGradient (3, 3, (mWidth-3) * section, mHeight-3, colors,NULL, Shader.TileMode.MIRROR); Mpaint.setshader (shader); } canvas.drawarc (RECTBLACKBG,, section * 360,false, Mpaint); } Private voidInitpaint () {Mpaint.setantialias (true); Mpaint.setstrokewidth ((float) 40.0); Mpaint.setstyle (Style.stroke); Mpaint.setstrokecap (Cap.round); Mpaint.setcolor (color.transparent); Mtextpaint.setantialias (true); Mtextpaint.setstrokewidth ((float) 3.0); Mtextpaint.settextalign (Paint.Align.CENTER); Mtextpaint.settextsize (50); Mtextpaint.setcolor (Color.Black); } Private intDIPTOPX (intdip) { floatScale =GetContext (). Getresources (). Getdisplaymetrics (). density; return(int) (DIP * scale + 0.5f * (Dip >= 0? 1:-1)); } Public intGetscore () {returnscore; } PublicString Getcrrentlevel () {returnCrrentlevel; } Public voidsetcrrentlevel (String crrentlevel) { This. Crrentlevel =Crrentlevel; } Public floatGetmaxcount () {returnMaxCount; } Public floatGetcurrentcount () {returnCurrentcount; } Public voidSetScore (intscore) { This. score =score; if(Score = = 100) { This. Crrentlevel = alarm_level[0]; } Else if(Score >= && score < 100) { This. Crrentlevel = alarm_level[1]; } Else if(Score >= && score < 70) { This. Crrentlevel = alarm_level[2]; } Else { This. Crrentlevel = alarm_level[3]; } invalidate (); } /*** * Set the maximum progress value * *@paramMaxCount*/ Public voidSetmaxcount (floatMaxCount) { This. MaxCount =MaxCount; } /*** * Set Current progress value * *@paramCurrentcount*/ Public voidSetcurrentcount (floatCurrentcount) { This. Currentcount = currentcount > MaxCount?Maxcount:currentcount; Invalidate (); } @Overrideprotected voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) { intWidthspecmode =Measurespec.getmode (WIDTHMEASURESPEC); intWidthspecsize =measurespec.getsize (WIDTHMEASURESPEC); intHeightspecmode =Measurespec.getmode (HEIGHTMEASURESPEC); intHeightspecsize =measurespec.getsize (HEIGHTMEASURESPEC); if(Widthspecmode = =measurespec.exactly|| Widthspecmode = =measurespec.at_most) {Mwidth=widthspecsize; } Else{mwidth= 0; } if(Heightspecmode = =Measurespec.at_most|| Heightspecmode = =measurespec.unspecified) {mheight= DIPTOPX (15); } Else{mheight=heightspecsize; } setmeasureddimension (Mwidth, mheight); }}
Demo:http://files.cnblogs.com/files/liujingg/progressviewdemo.rar
Android Custom gradient Ring progress bar