android - GradientDrawable.setCornerRadii 的剪辑不起作用
问题描述
我正在尝试创建一个具有自定义半径的视图,但在使用 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);
}
解决方案
我猜它不起作用,因为setClipToOutline
只适用于下面的形状。
目前,只有可以表示为矩形、圆形或圆形矩形的轮廓支持剪切。
https://developer.android.com/reference/android/graphics/Outline#canClip()
推荐阅读
- android - 我的清单中的哪些权限隐式添加
并避免下载平板电脑? - python - 如何根据给定的方程对数据进行适当的拟合?
- c++ - 使用 QT 执行二进制构建失败
- elasticsearch - 检查 2 个时间范围差异的 ElasticSearch 查询
- apache-kafka - Flink + Kafka + Kerberos:在 JAAS 配置中找不到“KafkaClient”条目
- aurelia - Aurelia 不会加载自定义组件
- python - 具有特定分布和另一种条件的随机数组创建
- angular - Frappe-Gantt - 不是构造函数
- android - 如何阻止安卓应用在模拟器上打开?
- spring-boot - @ControllerAdvice 中的 Spring-boot 处理 NoHandlerException