/**
* 改变SVG图片着色
* @param imageView
* @param iconResId svg资源id
* @param color 期望的着色
*/
public void changeSVGColor(ImageView imageView,int iconResId,int color){
Drawable drawable = AppCompatDrawableManager.get().getDrawable(mContext, iconResId);
imageView.setImageDrawable(drawable);
Drawable.ConstantState state = drawable.getConstantState();
Drawable drawable1 = DrawableCompat.wrap(state == null ? drawable : state.newDrawable()).mutate();
drawable1.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
DrawableCompat.setTint(drawable1, ContextCompat.getColor(mContext, color));
imageView.setImageDrawable(drawable1);
}
注:
Drawable drawable = AppCompatDrawableManager.get().getDrawable(mContext, iconResId);
和
Drawable drawable = ContextCompat.getDrawable(mContext, iconResId);
较低api下,如红米1s,报错如下
Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #1: invalid drawable tag vector
Instead of ContextCompat.getDrawable , just use AppCompatDrawableManager.get().getDrawable .
This should work on versions higher than 23.+
http://stackoverflow.com/questions/35622438/update-android-support-library-to-23-2-0-cause-error-xmlpullparserexception-bin