Android self-defined gradient progress bar

Source: Internet
Author: User

A progress bar with an arc gradient is required in the project, which is not possible with Android. I was not able to find the way to achieve, there is the great God know can be pointed. is the following

This is by inheriting the view to draw out, there are similar on the Internet, but the code is quite cumbersome, and create a lot of useless objects, to the memory management burden???
I've posted my own definition of the view code, so I can refer to it.
public class Springprogressview extends View {/** * segment color */private static final int[] Section_colors = {color.red, COLOR.P Arsecolor ("#ffa000"), color.yellow};/** * progress bar maximum value */private float maxcount;/** * progress bar Current value */private float currentcount;/** * Brush */private Paint Mpaint;private int mwidth, mheight;private rectf RECTBG = new RECTF ();p rivate rectf RECTPROGRESSBG = NE W RECTF ();p rivate lineargradient shader;public Springprogressview (context context, AttributeSet attrs, int defstyleattr {Super (context, attrs, defstyleattr); Initview (context);} Public Springprogressview (context context, AttributeSet Attrs) {Super (context, attrs); Initview (context);} Public Springprogressview (Context context) {super (context); Initview (context);} private void Initview (context context) {Mpaint = new Paint ();}  @Overrideprotected void OnLayout (Boolean changed, int left, int top, int. right, int. bottom) {super.onlayout (changed, left, Top, right, bottom); mheight = Bottom-top;mwidth = Right-left;rectbg.set (0, 0,mwidth,Mheight);}  @Overrideprotected void OnDraw (canvas canvas) {super.ondraw (canvas); Float section = currentcount/maxcount;if (shader = = NULL) {shader = new LinearGradient (0, 0, mwidth, mheight, section_colors, NULL, Shader.TileMode.CLAMP);} Mpaint.setshader (shader); Mpaint.setantialias (true); Mpaint.setstyle (Paint.Style.STROKE);//Draw progress bar outer border int round = Mheight*2/3;canvas.drawroundrect (RECTBG, round, round, mpaint);//Draw progress bar Mpaint.setstyle (Paint.Style.FILL); int pl= ( int) (mwidth* (1-section)); rectprogressbg.set (PL, 0, mwidth, mheight); Canvas.drawroundrect (RECTPROGRESSBG, round, Round, mpaint);} /* * Set maximum progress value */public void Setmaxcount (float maxCount) {this.maxcount = MaxCount;} /** * Set Current progress value */public void Setcurrentcount (float currentcount) {this.currentcount = currentcount > MaxCount?

Maxcount:currentcount;invalidate ();} public float Getmaxcount () {return maxCount;} public float Getcurrentcount () {return currentcount;}}

                                                               
? ? The above is your own definition of the View section, directly in the layout file reference can be. Setting the maximum and current progress values of the progress bar in the activity is a perfect way to achieve

? ? I also glue the code address, can download http://download.csdn.net/detail/u013122144/9495668

Android self-defined gradient progress bar

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.