3

我正在使用创建圆圈的自定义视图:

canvas.drawCircle(width/2, width/2, radius, paint1);

现在,我想从里面清空圆圈,就像一个戒指

绘制另一个较小的白色圆圈会引发另一个问题。我打算让我的视图听取用户点击。我只想听戒指上的点击声

有没有办法排除画布的某些部分?

4

5 回答 5

9

为了画一个圆环(即一个没有填充但只有一个边框的圆,里面的区域是透明的)这样做:

Paint myPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
myPaint.setStyle(Paint.Style.STROKE);
int strokeWidth=4;  // or whatever
myPaint.setStrokeWidth(strokeWidth);
myPaint.setColor(0xffff0000);   //color.RED
float radius=(float) (0.5*(w+h)*0.5);
canvas.drawCircle((float)0.5*w, (float)0.5*h, radius, myPaint);

对 drawCircle 的调用也可以用 drawArc 代替,如下面的代码。任何一种方法都行得通。秘诀是将绘画风格设置为 Paint.Style.STROKE。

RectF oval = new RectF(w/2 - radius, w/2 - radius, w/2 + radius, w/2 + radius);
canvas.drawArc(oval, 0, 360, false, myPaint); 
于 2013-09-15T20:00:40.793 回答
0

要绘制圆环,请使用 drawArc :

RectF oval = new RectF(width/2 - radius, width/2 - radius, width/2 + radius, width/2 + radius);
canvas.drawArc(oval, 0, 360, false, paint1);
于 2012-09-04T12:24:38.017 回答
0

我通过绘制一个内部白色圆圈并根据我拥有的两个半径长度过滤点击来解决这个问题。

所以,你不能画一个空的圆圈,但你可以让一个圆圈表现得像一个空的圆圈。

谢谢。

于 2012-09-16T08:43:53.253 回答
0

此解决方案有效:

Paint paintPath;

int circleRadius = (int)
    TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, context.getResources()
    .getDisplayMetrics());

paintPath = new Paint();
paintPath.setStrokeWidth(circleRadius);
paintPath.setColor(Color.WHITE);
paintPath.setStyle(Paint.Style.FILL_AND_STROKE);
paintPath.setAntiAlias(true);
paintPath.setPathEffect(null);
paintPath.setRasterizer(null);

里面抽奖

RectF rectF = new RectF(centerx - radius, centery - radius,
    centerx + radius, centery + radius);

Path myPath = new Path();
for (int i = 0; i <= 360; i += 1)
{
    myPath.addArc(rectF, i, 1);
}
canvas.drawPath(myPath, paintPath);
于 2015-03-08T14:04:28.123 回答
0

更短的方式:

draw = function() {
    ctx.globalCompositeOperation = "lighter";
    ctx.beginPath();
    // ctx.fillStyle = "green";
    ctx.arc(this.x, this.y, this.radius, 0, Math.PI*2, false);
    // ctx.fill();
ctx.strokeStyle = "yellow";
ctx.stroke();
    ctx.closePath();
}
于 2022-02-21T16:16:43.317 回答