我有一个来自XML资源的可绘制对象,我想使用该可绘制对象,但动态设置渐变颜色。目前我有以下代码:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="3dip">
</corners>
<gradient
android:angle="90"
android:type="linear"
android:startColor="#FFFFFFFF"
android:centerColor="#FFFF0000"
android:endColor="#FFFF0000">
</gradient>
</shape>
我认为我可以在运行时获取Drawable并将其转换为GradientDrawable,然后使用方法来动态设置颜色,从而实现颜色的动态变化。但是,GradientDrawable没有这样的方法,只能在构造函数中设置颜色。我觉得这很奇怪,因为渐变的所有其他方面都可以设置。是否有比覆盖onDraw()并手动绘制渐变更容易的方法?我正在尝试使用的一些类文档非常不好。