Android progress bar learning, android progress bar

Source: Internet
Author: User

Android progress bar learning, android progress bar

Custom Attributes

<! -- RoundColor ring color roundProgressColor progress color roundWidth ring width textColor text color textSize text size max maximum textIsDisplayable show progress text style STROKE hollow FILL solid --> <declare-styleable name = "RoundProgressBar"> <attr name = "roundColor" format = "color"/> <attr name = "roundProgressColor" format = "color"/> <attr name = "roundWidth" format = "dimension"> </attr> <attr name = "textColor" format = "color"/> <attr name = "textSize" format = "dimension"/> <attr name = "max" format = "integer"> </attr> <attr name = "textIsDisplayable" format = "boolean"> </attr> <attr name = "style"> <enum name = "STROKE" value = "0"> </enum> <enum name = "FILL" value = "1"> </enum> </attr> </declare -styleable>

 

Public RoundProgressBar (Context context, AttributeSet attrs, int defStyleAttr) {super (context, attrs, defStyleAttr); mPaint = new Paint (); /*** get custom attributes */TypedArray typedArray = context. obtainStyledAttributes (attrs, R. styleable. roundProgressBar); // the background color mRoundColor = typedArray. getColor (R. styleable. roundProgressBar_roundColor, Color. RED); // progress color mRoundProgressColor = typedArray. getColor (R. styleable. roundProgressBar_roundProgressColor, Color. BLUE); // The width of the circle mRoundWidth = typedArray. getDimension (R. styleable. roundProgressBar_roundWidth, 20); // In The Middle Of The font color, mTextColor = typedArray. getColor (R. styleable. roundProgressBar_textColor, Color. BLUE); // The font size of the intermediate progress display (mTextSize = typedArray. getDimension (R. styleable. roundProgressBar_textSize, 15); // maximum mMax = typedArray. getInteger (R. styleable. roundProgressBar_max, 100); // whether the text displays mTextIsDisplayable = typedArray. getBoolean (R. styleable. roundProgressBar_textIsDisplayable, true); // solid or hollow mStyle = typedArray. getInt (R. styleable. roundProgressBar_style, 0); typedArray. recycle ();}

 

 

Draw

// The center of the circle int centerOfCircle = getWidth ()/2; // radius int radius = (int) (centerOfCircle-mRoundWidth/2); // set the brush mPaint. setAntiAlias (true); // the color of the ring mPaint. setColor (mRoundColor); // set the hollow mPaint. setStyle (Paint. style. STROKE); // The width of the paint brush mPaint. setStrokeWidth (mRoundWidth); // draw a circle canvas. drawCircle (centerOfCircle, centerOfCircle, radius, mPaint);/*** painting percentage */mPaint. setStrokeWidth (0); // font size mPaint. setText Size (mTextSize); // paint brush color mPaint. setColor (mTextColor); // The mPaint font. setTypeface (Typeface. DEFAULT_BOLD); // calculate the percentage int percent = (int) (float) mProgress/(float) mMax) * 100); // measure the font width float textWidth = mPaint. measureText (percent + "%"); // determines whether the progress text is not 0 and the style is hollow if (mTextIsDisplayable & percent! = 0 & mStyle = STROKE) {canvas. drawText (percent + "%", centerOfCircle-textWidth/2, centerOfCircle + textWidth/2, mPaint);}/*** set progress */mPaint. setColor (mRoundProgressColor); // paint width mPaint. setStrokeWidth (mRoundWidth); mPaint. setAntiAlias (true); RectF oval = new RectF (centerOfCircle-radius, centerOfCircle-radius, centerOfCircle + radius, centerOfCircle + radius); switch (mStyle) {case STROKE: // hollow mPaint. setStyle (Paint. style. STROKE); // draw an ARC/* start angle */canvas. drawArc (oval, 180,360 * mProgress/mMax, false, mPaint); break; case FILL: // solid mPaint. setStyle (Paint. style. FILL_AND_STROKE); // draw an arc if (mProgress! = 0) {canvas. drawArc (oval, 180,360 * mProgress/mMax, true, mPaint) ;}break ;}

 

Source code:

Https://github.com/ln0491/ProgressDemo

 

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.