Canvas.drawArc() artefacts

前端 未结 2 1384
伪装坚强ぢ
伪装坚强ぢ 2021-02-04 16:47

I draw an arc on canvas in a custom view as shown below. Paint and rectangle are defined outside of onDraw() and added in there for simpli

2条回答
  •  伪装坚强ぢ
    2021-02-04 17:26

    I've found the solution! :)

    Instead of this:

    RectF rectangle = new RectF(60f, 60f, 480f, 480f);
    
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setColor(0x40000000);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(120);
    
    canvas.drawArc(rectangle, 225f, 315f, false, paint);
    

    Use this:

    RectF rectangle = new RectF(60f, 60f, 480f, 480f);
    
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setColor(0x40000000);
    paint.setStyle(Paint.Style.FILL);
    
    float strokeWidth = 120;
    float startAngle = 225f;
    float sweepAngle = 315f;
    
    RectF pathRect = new RectF();
    Path path = new Path();
    float s = strokeWidth / 2;
    pathRect.left = rectangle.left - s;
    pathRect.right = rectangle.right + s;
    pathRect.top = rectangle.top - s;
    pathRect.bottom = rectangle.bottom + s;
    path.arcTo(pathRect, startAngle, sweepAngle);
    pathRect.left = rectangle.left + s;
    pathRect.right = rectangle.right - s;
    pathRect.top = rectangle.top + s;
    pathRect.bottom = rectangle.bottom - s;
    path.arcTo(pathRect, startAngle + sweepAngle, -sweepAngle);
    path.close();
    canvas.drawPath(path, paint);
    

    This code results in slower run, but it doesn't produce artefacts. :)

提交回复
热议问题