使用Canvas将圆转换为环?

时间:2012-09-04 12:20:24

标签: android android-canvas

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

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

现在,我想从内部清空圆圈,例如a ring

绘制另一个较小的白色圆圈会引发另一个问题。我打算让我的观点听取用户点击。我想听一下环的点击次数

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

4 个答案:

答案 0 :(得分: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); 

答案 1 :(得分: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);

答案 2 :(得分:0)

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

所以,你不能绘制一个空圆,但你可以制作一个圆形,就像一个空圆。

感谢。

答案 3 :(得分: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);