如何为 child 视图剪切路径?

How to clipping a path for child views?

我有一个扩展 FrameLayout 的自定义 View。假设我的自定义 FrameLayout 只有一个 child(ImageView 匹配他 parent 的宽度和高度).

我只想在我的自定义 FrameLayout 中用 drawChild 方法绘制他的 child 的圆形。

我该怎么做?

您需要创建一个路径并将其剪辑到 canvas。

private Path mPath = new Path();

@Override
    protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
        mPath.reset();
        mPath.addCircle(x, y, radius, Path.Direction.CW);
        canvas.clipPath(mPath);
        boolean super.drawChild(canvas, child, drawingTime);
    }