【发布时间】:2015-06-10 01:56:26
【问题描述】:
我已经阅读了 20 多个问题/答案,但我仍然无法得到我想要的。我想在一个矩形内切一个圆,如下所示:
这是我的代码:
@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setARGB(180, 0, 0, 0);
canvas.drawRect(0, 0, getWidth(), getHeight(), paint);
Path circularPath = new Path();
circularPath.addCircle(getWidth() / 2, getHeight() / 2, radius, Path.Direction.CCW);
canvas.clipPath(circularPath, Region.Op.REPLACE);
canvas.drawColor(0x00000000);
}
我的背景 (setARGB) 显示正确,但没有任何内容被剪裁。除了REPLACE,我还尝试了不同的Op 值,通过调用setLayerType(LAYER_TYPE_SOFTWARE, null); 强制软件光栅化(正如我在某些Android 版本上阅读的clipPath 不支持某些Ops)构造函数,但无济于事。如何达到预期的效果?
注意:我的最低 SDK 版本是 15,所以我不需要支持低于 4.0 的任何东西。
【问题讨论】:
-
您尝试过 Region.Op.DIFFERENCE 吗?
-
@pskink 是的,现在再次尝试确认。不幸的是,什么也没发生。
-
DIFFERENCE 对我来说效果很好,尝试使用 Color.RED 使用 canvas.drawColor,然后使用 clipPath,然后使用 drawColor 0x88000000
-
@pskink 你能发布一个 sscce 作为答案吗?
-
就这样使用:canvas.drawColor(Color.RED);路径循环路径 = 新路径(); circularPath.addCircle(getWidth() / 2, getHeight() / 2, 300, Path.Direction.CCW); canvas.clipPath(循环路径,Region.Op.DIFFERENCE); canvas.drawColor(0x66000000);
标签: android android-4.0-ice-cream-sandwich ondraw clipping custom-draw