安卓:色调使用DrawableCompat [英] Android: Tint using DrawableCompat

查看:636
本文介绍了安卓:色调使用DrawableCompat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想着色之前的Andr​​oid API级别21的图像我已经成功地用有色项目:

I'm trying to tint an image prior to Android API level 21. I've successfully tinted items using:

<android:tint="@color/red"/>

不过,我似乎无法弄清楚如何通过code上ImageView的做到这一点:

However, I can't seem to figure out how to do this through code on an ImageView:

Drawable iconDrawable = this.mContext.getResources().getDrawable(R.drawable.somedrawable);
DrawableCompat.setTint(iconDrawable, this.mContext.getResources().getColor(R.color.red));
imageView.setImageDrawable(iconDrawable);

我已经尝试设置TintMode但是这似乎使没有什么不同。我使用V4的兼容性类DrawableCompat不正确?

I've tried setting the TintMode but this seems to make no different. Am I using the v4 compatibility class DrawableCompat incorrectly?

推荐答案

不是由 DrawableCompat 支持previously着色。 从支持库22.1起,你可以做到这一点,但你需要做的是这样的:

Previously tinting was not supported by DrawableCompat. Starting from support library 22.1 you can do that, but you need do it in this way:

Drawable normalDrawable = getResources().getDrawable(R.drawable.drawable_to_tint);
Drawable wrapDrawable = DrawableCompat.wrap(normalDrawable);
DrawableCompat.setTint(wrapDrawable, getResources().getColor(R.color.colorPrimaryLight));

这篇关于安卓:色调使用DrawableCompat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆