Android: How to Fill the Central Circle on Canvas?

Viewed 76

How to fill the central circle on canvas?

Question: Drawing 2 circles on a canvas

enter image description here

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    Paint p = new Paint();
    p.setColor(Color.RED);
    DashPathEffect dashPath = new DashPathEffect(new float[]{5,5}, (float)1.0);

    p.setPathEffect(dashPath);
    p.setStyle(Style.STROKE);

    for (int i = 0; i < 2; i ++) {
        canvas.drawCircle(200, 200, 50+(i*40), p);
    }

    invalidate();
}
1 Answers

You need Style.FILL for the inner circle.

private final Paint p = new Paint();
private final DashPathEffect dashPath = new DashPathEffect(new float[]{5,5}, (float)1.0);

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    p.setColor(Color.RED);
    p.setPathEffect(dashPath);
    p.setStyle(Style.STROKE);
    // outer circle
    canvas.drawCircle(200, 200, 50, p);

    p.setStyle(Style.FILL);
    // inner circle
    canvas.drawCircle(200, 200, 90, p);

    invalidate();
}
Related