【问题标题】:Only Draw background where Children Views are not?仅在没有子视图的情况下绘制背景?
【发布时间】:2015-07-10 19:26:04
【问题描述】:

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

编辑: 所以我有一段时间没有玩画布了,但我想做这样的事情:

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);
    }
}

clipPath 只是不会做我想做的事,我不认为。我认为那会在路径外剪辑,我需要在路径内剪辑。

【问题讨论】:

    标签: android android-canvas


    【解决方案1】:

    你可能想要做一些类似覆盖onLayout的事情,并在它的最后:

    • 遍历所有子视图
    • 检查孩子是否可见/不透明
    • 建立每个孩子的位置/大小的列表
    • 使用该列表进行您自己的背景剪辑

    【讨论】:

    • 这并不能真正回答我的问题。您将如何在路径内剪辑?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 2015-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    • 1970-01-01
    相关资源
    最近更新 更多