如何通过程序修改可绘制的角Android Java



我需要用Java编程修改一个xml可绘制的圆角半径。

XML如下(可以修改(:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:bottomRightRadius="32dp"
android:bottomLeftRadius="32dp"
android:topRightRadius="32dp"
android:topLeftRadius="32dp"/>
</shape>

我已经走了这么远。

Resources res = context.getResources();
Drawable customRoundedButton = ResourcesCompat.getDrawable(res, R.drawable.custom_rounded_button, null);
//  customRoundedButton somehow modify corners??
// customRoundedButton.setCornerRadii(16); // What I would like to do in pseudo code

PiaInterfaceConfiguration.getInstance().setButtonRoundCorner(R.drawable.custom_rounded_button);

问题是PiaInterfaceConfiguration只接受int.形式的可绘制引用

如果您的PiaInterfaceConfiguration只使用资源idint,那么您就无法更改这些角点,您必须创建另一个不可更改的XML或修改这个类(添加int roundCorner参数并将其设置在这个类中,就像您尝试使用注释代码一样(

编辑:设置圆角半径必须使用setCornerRadius(float)方法,属于GradientDrawable方法。所以你的Drawable customRoundedButton必须被广播到GradientDrawable(即使有纯色,梯度"特征"没有使用(

Drawable customRoundedButton = ...
if (customRoundedButton instanceof GradientDrawable) { // check just for safety
((GradientDrawable) customRoundedButton).setCornerRadius(22f); // float
}

最新更新