通过新的
android支持更新,向量可绘制获得向后兼容性.我有一个矢量图像与各种路径.我希望路径的颜色在点击一个按钮或根据输入值以编程方式更改.可以访问矢量路径的name参数吗?然后改变颜色.
解决方法
整个矢量的颜色可以使用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));
注意:如果矢量drawable设置为imageView作为背景,myImageView.getDrawable()将给出nullpointerexception.