Android draw circle with Path

☆樱花仙子☆ 提交于 2019-12-05 10:19:02
karl

There are two things here:

  1. You have to call canvas.drawOval(...) before drawing the arc onto the oval. Otherwise it won't show up. This is why my method didn't work.

  2. Canvas has a drawArc method that takes a start angle and a degrees to sweep out. See Canvas.drawArc(RectF, float, float, boolean, Paint). This is what I was looking for to draw circles.

EDIT: here's the relevant code from my View subclass:

private final Paint mArcPaint = new Paint() {
    {
        setDither(true);
        setStyle(Paint.Style.STROKE);
        setStrokeCap(Paint.Cap.ROUND);
        setStrokeJoin(Paint.Join.ROUND);
        setColor(Color.BLUE);
        setStrokeWidth(40.0f);
        setAntiAlias(true);
    }
};

private final Paint mOvalPaint = new Paint() {
    {
        setStyle(Paint.Style.FILL);
        setColor(Color.TRANSPARENT);
    }
};

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    RectF mOval = new RectF(left, top, right, bottom); //This is the area you want to draw on
    float sweepAngle = 270; //Calculate how much of an angle you want to sweep out here
    canvas.drawOval(mOval, mOvalPaint); 
    canvas.drawArc(mOval, 269, sweepAngle, false, mArcPaint); //270 is vertical. I found that starting the arc from just slightly less than vertical makes it look better when the circle is almost complete.
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!