Android custom gradient ring progress bar, android gradient Ring

Source: Internet
Author: User
Tags drawtext

Android custom gradient ring progress bar, android gradient Ring

First look at the effect:

 

Analysis: common in scenarios such as scan results and progress bars

Draw an arc using canvas. drawArc (RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint)

Some attributes of Paint define the width, color, style, etc.

LinearGradient implements linear gradient of colors

In the same way, you can draw a long progress bar, a fan chart, and so on. If you are interested, try again ..

Package com. liujing. progressviewdemo;/***** custom arc progress bar ** @ author liujing */public class ProgressView extends View {// segment Color private static final int [] SECTION_COLORS = {Color. GREEN, Color. YELLOW, Color. RED}; private static final String [] ALARM_LEVEL = {"security", "low", "medium", "high"}; private float maxCount; private float currentCount; private int score; private String crw.level; private Paint mPaint; Private Paint mTextPaint; private int mWidth, mHeight; public ProgressView (Context context, AttributeSet attrs, int defStyleAttr) {super (context, attrs, defStyleAttr); init (context );} public ProgressView (Context context, AttributeSet attrs) {this (context, attrs, 0);} public ProgressView (Context context) {this (context, null );} private void init (Context context) {mPaint = new Paint (); mTextP Aint = new Paint () ;}@ Override protected void onDraw (Canvas canvas) {super. onDraw (canvas); initPaint (); RectF rectBlackBg = new RectF (20, 20, mWidth-20, mHeight-20); canvas. drawArc (rectBlackBg, 0,360, false, mPaint); mPaint. setColor (Color. BLACK); canvas. drawText (score + "score", mWidth/2, mHeight/2, mTextPaint); mTextPaint. setTextSize (40); if (cr1_level! = Null) {canvas. drawText (cr1_level, mWidth/2, mHeight/2 + 50, mTextPaint);} float section = currentCount/maxCount; if (section <= 1.0f/3.0f) {if (section! = 0.0f) {mPaint. setColor (SECTION_COLORS [0]);} else {mPaint. setColor (Color. TRANSPARENT) ;}} else {int count = (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 = new LinearGr Adient (3, 3, (mWidth-3) * section, mHeight-3, colors, null, Shader. tileMode. MIRROR); mPaint. setShader (shader);} canvas. drawArc (rectBlackBg, 180, section * 360, false, mPaint);} private void initPaint () {mPaint. setAntiAlias (true); mPaint. setStrokeWidth (float) 40.0); mPaint. setStyle (Style. STROKE); mPaint. setStrokeCap (Cap. ROUND); mPaint. setColor (Color. TRANSPARENT); mTextPaint. setAntiAlias (tru E); mTextPaint. setStrokeWidth (float) 3.0); mTextPaint. setTextAlign (Paint. align. CENTER); mTextPaint. setTextSize (50); mTextPaint. setColor (Color. BLACK);} private int dipToPx (int dip) {float scale = getContext (). getResources (). getDisplayMetrics (). density; return (int) (dip * scale + 0.5f * (dip> = 0? 1:-1);} public int getScore () {return score;} public String getCrrentLevel () {return crrentLevel;} public void setCrrentLevel (String crrentLevel) {this. crw.level = crw.level;} public float getMaxCount () {return maxCount;} public float getCurrentCount () {return currentCount;} public void setScore (int score) {this. score = score; if (score = 100) {this. crw.level = ALARM_LEVEL [0 ];} Else if (score >=70 & score <100) {this. crrentLevel = ALARM_LEVEL [1];} else if (score >=30 & score <70) {this. crw.level = ALARM_LEVEL [2];} else {this. crrentLevel = ALARM_LEVEL [3];} invalidate ();}/***** set the maximum progress value ** @ param maxCount */public void setMaxCount (float maxCount) {this. maxCount = maxCount;}/***** set the current progress value ** @ param currentCount */public void setCurrentCount (floa T currentCount) {this. currentCount = currentCount> maxCount? MaxCount: currentCount; invalidate () ;}@ Override protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {int widthSpecMode = MeasureSpec. getMode (widthMeasureSpec); int widthSpecSize = MeasureSpec. getSize (widthMeasureSpec); int heightSpecMode = MeasureSpec. getMode (heightMeasureSpec); int heightSpecSize = 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

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.