Android custom learning (5) custom Progressbar

Source: Internet
Author: User
Tags drawtext getcolor

Android custom learning (5) custom Progressbar

The last phase of custom View learning is now available.



I believe everyone has their own implementation methods. Here I will post my methods for reference.


/* Scale Paint brush */private Paint linePaint;/* progress Paint */private Paint textPaint;/* progress */private int index = 0; /*** scale */private 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(70);textPaint.setColor(Color.WHITE);handler.sendEmptyMessage(0);handler.sendEmptyMessageDelayed(1, 2000);}
I believe everyone can understand the above .. Next is the most critical onDraw ()

@ Overrideprotected void onDraw (Canvas canvas) {super. onDraw (canvas); // move the canvas. translate (getWidth ()/2, getHeight ()/2); // fill in the background canvas. drawColor (getResources (). getColor (R. color. gray); // The total number of scales int count = 30; // The percentage of progress words in the Rect rect = new Rect (); String indexsString = String. valueOf (index); if (index <10) {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 (int I = 0; I <count; I ++) {if (I <= ruling) {linePaint. setColor (getResources (). getColor (R. color. pink);} else {linePaint. setColor (Color. BLACK);} canvas. drawLine (0,100, 0,120, linePaint); canvas. rotate (360/count, 0, 0 );}}
You can leave a message if you are not familiar with the comments. Next, let's take a look at the effect.



After this is done, I believe that the second one is coming!


/* ** Ring */private Paint rectPaint;/* scale */private Paint linePaint;/* fill */private Paint fillPaint; /*** progress word */private 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(20);rectPaint.setStyle(Style.STROKE);rectPaint.setColor(getResources().getColor(R.color.dark));linePaint = new Paint(rectPaint);linePaint.setStrokeWidth(20);linePaint.setColor(getResources().getColor(R.color.pink));fillPaint = new Paint(linePaint);fillPaint.setStrokeWidth(1);fillPaint.setColor(Color.BLACK);fillPaint.setAlpha(60);textPaint = new Paint();textPaint.setStrokeWidth(5);textPaint.setAntiAlias(true);textPaint.setTextSize(70);textPaint.setColor(Color.WHITE);handler.sendEmptyMessage(0);handler.sendEmptyMessage(1);}
@ Overrideprotected void onDraw (Canvas canvas) {super. onDraw (canvas); // Number of scales int count = 60; // specifies the background color canvas. drawColor (getResources (). getColor (R. color. gray); // move the canvas. translate (getWidth ()/2, getHeight ()/2); // RectF rectF = new RectF (-100,-100,100,100); String indexString = String. valueOf (index); if (index <10) {indexString = "0" + indexString;} Rect rect = new Rect (); textPaint. getTextBounds (indexString, 0, indexString. length (), rect); canvas. drawText (indexString,-rect. width ()/2, rect. height ()/2, textPaint); // circle canvas. drawArc (rectF, 0,360, false, rectPaint); // fill color canvas. drawArc (rectF,-90, index, false, linePaint); for (int I = 0; I <index2; I ++) {canvas. drawLine (0,-90, 0,-110, fillPaint); canvas. rotate (360/count, 0, 0 );}}

Source code




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.