Android Custom Learning (5) Customizing ProgressBar

Source: Internet
Author: User
Tags drawtext getcolor

Customize the last issue of view learning, first.



I believe we all have their own way of implementation, here on my method for reference.


/** * Scale Brush */private Paint linepaint;/** * Progress Brush */private Paint textpaint;/** * Progress */private int index = 0;/** * Scale */priv ate int ruling = 0;
private void Init () {linepaint = new Paint (); Linepaint.setcolor (Color.Black); Linepaint.setstrokewidth (5); Linepaint.setantialias (True); Linepaint.setstyle (style.fill); textpaint = new Paint (linepaint); Textpaint.settextsize (+); Textpaint.setcolor (Color.White); handler.sendemptymessage (0); Handler.sendemptymessagedelayed (1, 2000);}
I believe we can all understand the above. Next is the most critical OnDraw ()

@Overrideprotected void OnDraw (canvas canvas) {super.ondraw (canvas);//Move Canvas canvas.translate (getwidth ()/2, GetHeight ( )//2);//Fill Background Canvas.drawcolor (Getresources (). GetColor (R.color.gray));//Total ticks int count = 30;//Progress Word occupies size rect rect = new Rect ( ); String indexsstring = string.valueof (index), if (Index <) {indexsstring = "0" + indexsstring;} Textpaint.gettextbounds (indexsstring, 0, Indexsstring.length (), rect);//Progress Font Canvas.drawtext (indexsstring,- Rect.width ()/2, Rect.height ()/2,textpaint);//rotate the canvas, draw the scale, and change the color according to the ruling value for (int i = 0; i < count; i++) {if (I <= r uling) {Linepaint.setcolor (Getresources (). GetColor (R.color.pink));} else {linepaint.setcolor (color.black);} Canvas.drawline (0, 0, linepaint), Canvas.rotate (360/count, 0, 0);}}
Comments should be very detailed do not understand can also leave a message, the next look at the effect



After completing this, I believe the second and extremely easy!


/** * Ring */private Paint rectpaint;/** * Scale */private paint linepaint;/** * Fill */private paint fillpaint;/** * Progress word */priva TE Paint textpaint;private int index = 0;private int index2 = 0;public ProgressView2 (context context) {This (context, NULL) ;//TODO auto-generated Constructor Stub}public ProgressView2 (context context, AttributeSet Attrs) {This (context, attrs, 0);//TODO auto-generated Constructor Stub}public ProgressView2 (context context, AttributeSet attrs, int defstyle) {Super (Context, attrs, defstyle); init ();}

private void init () {rectpaint = new Paint (); Rectpaint.setantialias (true); Rectpaint.setstrokewidth, Rectpaint.setstyle (Style.stroke); Rectpaint.setcolor (Getresources (). GetColor ( R.color.dark) Linepaint = new Paint (rectpaint); Linepaint.setstrokewidth (Linepaint.setcolor); Getresources (). GetColor (r.color.pink)) Fillpaint = new Paint (linepaint); fillpaint.setstrokewidth (1); Fillpaint.setcolor ( Color.Black); Fillpaint.setalpha, textpaint = new Paint (); Textpaint.setstrokewidth (5); Textpaint.setantialias ( true); Textpaint.settextsize, Textpaint.setcolor (Color.White); handler.sendemptymessage (0); Handler.sendemptymessage (1);} 
@Overrideprotected void OnDraw (canvas canvas) {super.ondraw (canvas);//number of ticks int count = 60;//background color Canvas.drawcolor ( Getresources (). GetColor (R.color.gray));//Move Canvas canvas.translate (getwidth ()/2, GetHeight ()/2);//Ring size RECTF RECTF = new RECTF (-100,-100, 100, 100); String indexstring = string.valueof (index), if (Index <) {indexstring = "0" + indexstring;} Rect rect = new rect (); Textpaint.gettextbounds (indexstring, 0, <span style= "font-family:arial, Helvetica, Sans-serif ;" >indexstring </span><span style= "font-family:arial, Helvetica, Sans-serif;" >.length (), rect); </span>canvas.drawtext (indexstring,-rect.width ()/2, Rect.height ()/2,textpaint);//  Ring Canvas.drawarc (RECTF, 0, RECTF, false, rectpaint);//Fill Color Canvas.drawarc (linepaint, -90, index, false,); for (int i = 0; i < Index2; i++) {canvas.drawline (0, -90, 0, -110, fillpaint); Canvas.rotate (360/count, 0, 0);}}


PS: There is a bug in the uploaded code, and you should not use "XX" when measuring the font size in OnDraw. Length (), should be used Indexstring.length ()

I don't want to be careless.

Project Source




Android Custom Learning (5) Customizing ProgressBar

Related Article

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.