如何填充画布中两个圆圈之间的区域?

时间:2016-07-16 13:10:40

标签: android canvas

我想在画布中填充两个圆圈之间的区域 enter image description here

这是我的提款

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

    canvas.drawCircle(centerX, centerY, radarRadius / 3, mPaintCircle);
    canvas.drawCircle(centerX, centerY, 3 * radarRadius / 7, mPaintCircle);
}

2 个答案:

答案 0 :(得分:1)

您可以创建两个圆形路径并剪切画布,如下所示:

Path outerPath = new Path();
outerPath.addCircle(centerX, centerY, outerRadius, Direction.CW);
Path innerPath = new Path();
innerPath.addCircle(centerX, centerY, innerRadius, Direction.CW);
canvas.clipPath(outerPath);
canvas.clipPath(innerPath, Region.Op.DIFFERENCE);

同时禁用视图上的硬件加速:

view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

答案 1 :(得分:0)

您可以使用以下简单规则:)

Two nested circles

示例代码

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Two Circles</title>
</head>

<body>
    <canvas id="canvas" width="200" height="200"></canvas>

    <script>
        var canvas = document.getElementById('canvas')
        var ctx = canvas.getContext('2d')

        var cx = 100
        var cy = 100
        var r1 = 80
        var r2 = 60

        // outer circle
        ctx.fillStyle = 'red'
        ctx.beginPath()
        ctx.arc(cx, cy, r1, 0, 2 * Math.PI)
        ctx.fill()

        // inner circle
        ctx.fillStyle = 'white'
        ctx.beginPath()
        ctx.arc(cx, cy, r2, 0, 2 * Math.PI)
        ctx.fill()
    </script>
</body>

</html>