java – Android – 动态设置drawable的渐变
发布时间:2020-12-15 02:24:19 所属栏目:Java 来源:网络整理
导读:我有一个 XML资源的drawable,我想使用drawable但动态设置渐变颜色.到目前为止,我有这样的事情: ?xml version="1.0" encoding="utf-8"?shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" corners android:radius
我有一个
XML资源的drawable,我想使用drawable但动态设置渐变颜色.到目前为止,我有这样的事情:
<?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()和自己做渐变更简单的方法?我正在尝试使用的一些类很难记录. 解决方法
资源主要是静态的,通常不允许修改.某些资源类型允许您“克隆”可变副本. GradientDrawable只允许您在contstuctor中设置颜色(如您所发现的),因此如果要在运行时动态控制颜色,则需要在内部创建颜色,或者更好的是,从资源中选择固定数量的背景之一. 如前所述,使用setBackgroundDrawable()在运行时安装后台. 没必要通过判断,只需Get-R-Done!
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |