Custom Layout that rounds the corners of its content

后端 未结 7 2049
北海茫月
北海茫月 2020-11-29 04:08

I would like to create a generic ViewGroup which can then be reused in XML layouts to round the corners of anything that is put into it.

For some reason canvas

7条回答
  •  遥遥无期
    2020-11-29 04:53

    U need to override the drawChild() method to clip childViews.

    @Override
    protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
        int flag = canvas.save();
        canvas.clipPath(pathToClip);
        boolean result=super.drawChild(canvas, child, drawingTime);
        canvas.restoreToCount(flag);
        return result;
    }
    

    If u want to clip the background of the ViewGroup too,override draw() instead.like this

    @Override
    public void draw(Canvas canvas) {
        int flag = canvas.save();
        canvas.clipPath(pathToClip);
        super.draw(canvas);
        canvas.restoreToCount(flag);
    }
    

提交回复
热议问题