在Android中填充两个圆圈的交叉区域

时间:2015-07-04 23:34:40

标签: android android-layout android-canvas

我的应用程序包含一个calculatePosition(),它绘制了四个圆圈:

private void calculatePosition(){
    Paint paint = new Paint();
    paint.setColor(Color.RED);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(10);

    float ratio = (float) (640/3.45);

    float radiusB1 = Calculations.convertscale(scale, position[0] * ratio);
    float radiusB2 = Calculations.convertscale(scale, position[1] * ratio);
    float radiusB3 = Calculations.convertscale(scale, position[2] * ratio);
    float radiusB4 = Calculations.convertscale(scale, position[3] * ratio);



    canvas.drawCircle(broadCaster1x, broadCaster1y, radiusB1, paint);
    canvas.drawCircle(broadCaster2x, broadCaster2y, radiusB2, paint);
    canvas.drawCircle(broadCaster3x, broadCaster3y, radiusB3, paint);
    canvas.drawCircle(broadCaster4x, broadCaster4y, radiusB4, paint);
}

我想知道在Android中是否可以填充两个圆圈之间的交叉区域,如下图所示。如果是,是否有任何基本的例子如何获得它?。

enter image description here

1 个答案:

答案 0 :(得分:1)

使用' Canvas.clipPath'

例如:

Path pathA = new Path();
pathA.addCircle(xCenterA, yCenterA, radiusA, Direction.CW);
Path pathB = new Path();
pathB.addCircle(xCenterB, yCenterB, radiusB, Direction.CW);
canvas.clipPath(pathA);
canvas.clipPath(pathB, Region.Op.DIFFERENCE);