简体   繁体   中英

Clip For GradientDrawable.setCornerRadii not working

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

    }

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM