随着新的 Android 支持更新,矢量绘图获得了向后兼容性。我有一个带有各种路径的矢量图像。我希望路径的颜色在单击按钮时发生变化,或者基于输入值以编程方式发生变化。是否可以访问矢量路径的名称参数?然后改变颜色。
可以使用 setTint 更改整个矢量的颜色。
您必须在布局文件中设置 ImageView,如下所示:
<ImageView
android:id="@+id/myImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tint="@color/my_nice_color"
android:src="@drawable/ic_my_drawable"
android:scaleType="fitCenter" />
然后更改图像的颜色:
DrawableCompat.setTint(myImageView.getDrawable(), ContextCompat.getColor(context, R.color.another_nice_color));
Note: myImageView.getDrawable()
如果将矢量可绘制对象设置为 imageView 作为背景,则会给出 nullpointerException。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)