Android - 剪辑ViewGroups儿童在众多drawables区域之一

时间:2013-08-20 15:55:07

标签: java android android-layout android-canvas

我现在正在使用Android App上的一些自定义视图,现在我遇到了我不知道如何解决的问题。我使用背景drawables和paddings为drawables(而不是视图)绘制视图边框。例如,典型的FrameLayout看起来像这样:

enter image description here

但我的问题开始时,在ViewGroups中,儿童应该剪裁为蓝色区域,但它们是这样绘制的:

enter image description here

我的观点是当绿色区域(任何类型的孩子)被剪裁到蓝色区域,包括半径等时,会出现这种情况。

我已经尝试过clipToPadding()和Canvas.clipPath(),但最终却没有。

任何人都有类似情况可以提供帮助吗?

1 个答案:

答案 0 :(得分:0)

好的,我找到了简单的解决方案,我改变了我的onDraw方法做类似的事情:

 @Override
protected void onDraw(Canvas canvas) {

    getBackground().draw(canvas);
    mPath.reset();
    getDrawingRect(tmpRect);

    if (mRoundRect.left != tmpRect.left || mRoundRect.right != tmpRect.right ||
            mRoundRect.top != tmpRect.top || mRoundRect.bottom != tmpRect.bottom) {
        mRoundRect.set(tmpRect.left+border, tmpRect.top+border, tmpRect.right-border, tmpRect.bottom-border);
        mPath.addRoundRect(mRoundRect, floatsRadiuses, Path.Direction.CW);
        canvas.clipPath(mPath);
    }

    int childrenSize = getChildCount();
    for(int i=0;i<childrenSize;i++){
        View child = getChildAt(i);
        drawChild(canvas,child,getDrawingTime());
    }


}