It is too late to answer, but I hope this helps someone else. If you want to draw a circle this way, don't bother with 46.0%, as this is just a textual representation.
.
public class Circle extends View { private Paint mCircleYellow; private Paint mCircleGray; private float mRadius; private RectF mArcBounds = new RectF(); public Circle(Context context) { super(context); // create the Paint and set its color } public Circle(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initPaints(); } public Circle(Context context, AttributeSet attrs) { this(context, attrs, 0); } private void initPaints() { mCircleYellow = new Paint(Paint.ANTI_ALIAS_FLAG); mCircleYellow.setStyle(Paint.Style.FILL); mCircleYellow.setColor(Color.YELLOW); mCircleYellow.setStyle(Paint.Style.STROKE); mCircleYellow.setStrokeWidth(15 * getResources().getDisplayMetrics().density); mCircleYellow.setStrokeCap(Paint.Cap.SQUARE); // mEyeAndMouthPaint.setColor(getResources().getColor(R.color.colorAccent)); mCircleYellow.setColor(Color.parseColor("#F9A61A")); mCircleGray = new Paint(Paint.ANTI_ALIAS_FLAG); mCircleGray.setStyle(Paint.Style.FILL); mCircleGray.setColor(Color.GRAY); mCircleGray.setStyle(Paint.Style.STROKE); mCircleGray.setStrokeWidth(15 * getResources().getDisplayMetrics().density); mCircleGray.setStrokeCap(Paint.Cap.SQUARE); // mEyeAndMouthPaint.setColor(getResources().getColor(R.color.colorAccent)); mCircleGray.setColor(Color.parseColor("#76787a")); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); mRadius = Math.min(w, h) / 2f; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int w = MeasureSpec.getSize(widthMeasureSpec); int h = MeasureSpec.getSize(heightMeasureSpec); int size = Math.min(w, h); setMeasuredDimension(size, size); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Float drawUpto = 46f; float mouthInset = mRadius / 3f; mArcBounds.set(mouthInset, mouthInset, mRadius * 2 - mouthInset, mRadius * 2 - mouthInset); canvas.drawArc(mArcBounds, 0f, 360f, false, mCircleGray); canvas.drawArc(mArcBounds, 270f, drawUpto, false, mCircleYellow); }
}
Therefore, use this class in your XML file, since it is a view class.
source share