【问题标题】:Clip For GradientDrawable.setCornerRadii not workingGradientDrawable.setCornerRadii 的剪辑不起作用
【发布时间】:2020-11-21 11:15:11
【问题描述】:

我正在尝试创建一个具有自定义半径的视图,但在使用 GradientDrawable.setCornerRadii 时遇到了问题...
如果我使用下面的代码,视图会剪辑其中的所有子视图

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

但是如果我对不同的角半径使用下面的代码,那么由于子视图超出了父视图,所以裁剪无法按预期工作

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

    }

【问题讨论】:

    标签: android view android-view cornerradius android-gradient-drawable


    【解决方案1】:

    我猜它不起作用,因为setClipToOutline 仅适用于下面的形状。

    目前,只有可以表示为矩形、圆形或圆形矩形的轮廓支持剪裁。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-02
      • 2012-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-05
      相关资源
      最近更新 更多