代码动态改变SVG矢量图颜色

本文介绍了一种在Android中修改SVG图片颜色的方法,并提供了一个实用的函数实现。该函数接收ImageView、SVG资源ID及期望的颜色作为参数,通过使用AppCompatDrawableManager获取SVG资源并调整其颜色。
    /**
     * 改变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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值