android – 如何更改矢量可绘的路径的颜色按钮点击

通过新的 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.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...