随着新的 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));
注意:myImageView.getDrawable()
如果矢量可绘制对象设置为 imageView 作为背景,则会给出 nullpointerexception。
关于android - 如何在按钮单击时更改矢量可绘制路径的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35625099/