Created
February 19, 2019 05:17
-
-
Save pennya/4480a5d52c81882301f12e1c296384fe to your computer and use it in GitHub Desktop.
How to draw custom donut progress programmatically in Android !
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<dimen name="donut_size">200dp</dimen> | |
<dimen name="donut_stroke_size">15dp</dimen> | |
<dimen name="donut_text_size">20dp</dimen> | |
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DonutView extends View { | |
Context context; | |
int value = 0; | |
int size; | |
int strokeSize; | |
int textSize; | |
int width; | |
int height; | |
public DonutView(Context context) { | |
super(context); | |
this.context = context; | |
init(); | |
} | |
public DonutView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
this.context = context; | |
init(); | |
} | |
public DonutView(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
this.context = context; | |
init(); | |
} | |
private void init() { | |
size = getResources().getDimensionPixelSize(R.dimen.donut_size); | |
strokeSize = getResources().getDimensionPixelSize(R.dimen.donut_stroke_size); | |
textSize = getResources().getDimensionPixelSize(R.dimen.donut_text_size); | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
width = MeasureSpec.getSize(widthMeasureSpec); | |
height = MeasureSpec.getSize(heightMeasureSpec); | |
} | |
public void setValue(int value) { | |
this.value = value; | |
invalidate(); | |
} | |
@Override | |
protected void onDraw(Canvas canvas) { | |
canvas.drawColor(Color.alpha(Color.CYAN)); // clear all | |
RectF rectF = new RectF(20, 20, size - 20, size - 20); | |
Paint paint = new Paint(); | |
paint.setColor(Color.LTGRAY); | |
paint.setAntiAlias(true); | |
paint.setStyle(Paint.Style.STROKE); | |
paint.setStrokeWidth(strokeSize); | |
canvas.drawArc(rectF, 0, 360, false, paint); | |
paint.setColor(Color.RED); | |
paint.setStrokeJoin(Paint.Join.ROUND); | |
canvas.drawArc(rectF, -90, value, false, paint); | |
paint.setTextSize(textSize); | |
paint.setStrokeWidth(5); | |
paint.setColor(Color.RED); | |
String txt = String.valueOf(value); | |
int xPos = width / 2 - (int)(paint.measureText(txt) / 2); | |
int yPos = (int)(height / 2 - ((paint.descent() + paint.ascent()) / 2)); | |
canvas.drawText(txt, xPos, yPos, paint); | |
} | |
} |
Author
pennya
commented
Feb 19, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment