我知道这可能有点晚了,但我从一个 iOS 项目中采用了这种方法,首先绘制轮廓,然后绘制填充弧
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float width = (float)getWidth();
float height = (float)getHeight();
float radius;
//Get radius from the bigger size
if (width > height){
radius = height/2;
}else{
radius = width/2;
}
//Create Paint Object
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setFilterBitmap(true);
paint.setAntiAlias(true);
paint.setStrokeWidth(5);
paint.setStyle(Paint.Style.STROKE);
//Create Contour Object
Path path = new Path();
float center_x, center_y;
center_x = width/2;
center_y = height/2;
//Configure rect for the outer ring
final RectF oval = new RectF();
oval.set(center_x - radius + 5,
center_y - radius + 5,
center_x + radius - 5,
center_y + radius - 5);
//Add outer arc
path.addArc(oval, 0, 90);
//Configure rect for the inner ring
oval.set(center_x - radius + 30,
center_y - radius + 30,
center_x + radius - 30,
center_y + radius - 30);
//Add inner arc to the path but draw counterclockwise
path.arcTo(oval, -270, -90);
//close path
path.close();
//Create Paint Object
Paint fillPaint = new Paint();
fillPaint.setColor(Color.YELLOW);
fillPaint.setFilterBitmap(true);
fillPaint.setAntiAlias(true);
fillPaint.setStrokeWidth(21);
fillPaint.setStyle(Paint.Style.STROKE);
//Create Contour Object
Path fillPath = new Path();
//Configure rect for the fill ring
oval.set(center_x - radius + 17,
center_y - radius + 17,
center_x + radius - 17,
center_y + radius - 17);
//Add fill arc
fillPath.addArc(oval, 0, 90);
//draw fill path
canvas.drawPath(fillPath,fillPaint);
//draw outer path
canvas.drawPath(path, paint);
}
一个有用的链接: http: //www.programering.com/a/MDO1czNwATE.html,其中 addarc 数学得到了很好的解释