Clip For GradientDrawable.setCornerRadii not working

545 views Asked by At

I'm trying to create a view with custom radius but facing a problem when using GradientDrawable.setCornerRadii...
Line If i use Below code the view clips all the child view inside it

public void SetCornerRadius(View v,int Radius){
        GradientDrawable shape = new GradientDrawable();
        shape.setShape(GradientDrawable.RECTANGLE);
        shape.setCornerRadius((int)Px2Dp(Radius));
        v.setBackground(shape);
        v.setOutlineProvider(ViewOutlineProvider.BACKGROUND);
        v.setClipToOutline(true);
    }

But if i use Below code for different corner radius then Clipping is not working as expected as child view goes outside parent view

public void SetDifferentCornerRadius(View v,int TopLeftRadius,int TopRightRadius,int BottomLeftRadius,int BottomRightRadius,String backgroundColor){
        GradientDrawable shape = new GradientDrawable();
        ViewGroup vg = (ViewGroup)v;
        shape.setShape(GradientDrawable.RECTANGLE);
        shape.setColor(Color.parseColor(backgroundColor));
        shape.setCornerRadii(new float[] { (int)Px2Dp(TopLeftRadius),(int)Px2Dp(TopLeftRadius),(int)Px2Dp(TopRightRadius),(int)Px2Dp(TopRightRadius),(int)Px2Dp(BottomRightRadius),(int)Px2Dp(BottomRightRadius),(int)Px2Dp(BottomLeftRadius),(int)Px2Dp(BottomLeftRadius)});
        v.setBackground(shape);
        v.setOutlineProvider(ViewOutlineProvider.BACKGROUND);
        v.setClipToOutline(true);

    }
1

There are 1 answers

0
xon40245 On BEST ANSWER

I would guess it doesn't work because setClipToOutline only works for the shapes below.

Currently, only Outlines that can be represented as a rectangle, circle, or round rect support clipping.

https://developer.android.com/reference/android/graphics/Outline#canClip()