Android 仅在不包含子视图的位置绘制背景?

Android 仅在不包含子视图的位置绘制背景?,android,android-canvas,Android,Android Canvas,我试图防止在我的应用程序中过度绘制,在一个根视图中,我想检测我的子视图的位置,然后为它们不在的位置绘制背景。根据我的理解,剪辑会在路径之外进行剪辑,但我真正想做的不是在屏幕上孩子们所在的特定位置进行绘制。有什么好办法吗 编辑: 所以我已经有一段时间没有玩画布了,但我想做一些类似的事情: public class ContainerView extends FrameLayout { private final Paint mBackgroundPaint = new Paint();

我试图防止在我的应用程序中过度绘制,在一个根视图中,我想检测我的子视图的位置,然后为它们不在的位置绘制背景。根据我的理解,剪辑会在路径之外进行剪辑,但我真正想做的不是在屏幕上孩子们所在的特定位置进行绘制。有什么好办法吗

编辑: 所以我已经有一段时间没有玩画布了,但我想做一些类似的事情:

public class ContainerView extends FrameLayout {

    private final Paint mBackgroundPaint = new Paint();
    private final Path mPath = new Path();

    public ContainerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mBackgroundPaint.setColor(context.getResources().getColor(R.color.default_background));
    }

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

        canvas.drawPaint(mBackgroundPaint);

        mPath.reset();
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            mPath.addRect(child.getLeft(), child.getTop(), child.getRight(), child.getBottom(),
                    Path.Direction.CCW); // What direction do I want?
        }

        canvas.clipPath(mPath);
    }
}

克利帕斯只是不会做我想做的事,我不这么认为。我认为这将在路径外部进行剪辑,而我需要在路径内部进行剪辑。

您可能需要执行类似于“仅覆盖布局”的操作,并在其末尾执行以下操作:

迭代所有子视图 检查子对象是否可见/不透明 建立每个孩子的位置/大小列表 使用该列表进行您自己的背景剪辑
这并没有真正回答我的问题。如何在路径中剪裁?