Android改变图片背景颜色tint(着色)或 backgroundTint

发布时间:2020-06-20 07:43:25 作者:拾荒者老大
来源:网络 阅读:13155

安卓对图标着色以节省apk体积,网上有很多介绍到的,使用也很简单

<ImageView
    android:tint="@android:color/white"
    android:backgroundTint="@android:color/black"
    />

平时都这样使用,今天得在代码中改,所以记录一下

第一种

Drawable icon;
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP){
    icon = getResources().getDrawable( R.drawable.ic_back_black );
}else{
    icon = getResources().getDrawable( R.drawable.ic_back_black ,getTheme());
}
ColorFilter filter = new LightingColorFilter( Color.WHITE, Color.WHITE);
icon.setColorFilter(filter);
p_w_picpath.setImageDrawable(icon);

第二种,需要V4包

Drawable icon1;
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP){
    icon1 = getResources().getDrawable( R.drawable.ic_back_black );
}else{
    icon1 = getResources().getDrawable( R.drawable.ic_back_black ,getTheme());
}
Drawable tintIcon = DrawableCompat.wrap(icon1);
DrawableCompat.setTint(tintIcon, Color.WHITE);
//DrawableCompat.setTintList(tintIcon, Color.WHITE);
titleBack1.setImageDrawable(tintIcon);

好吧,两个都能实现.也不知道哪个更好.

网上有不少相关介绍

http://www.cnblogs.com/helloandroid/p/4779061.html

http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2016/0128/3923.html


推荐阅读:
  1. CSS 指层叠样式表 (Cascading Style Sheets)
  2. 过渡效果 loding 动画 animation

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android drawable tint

上一篇:用数组语法设置元素样式

下一篇:混合使用Azure LB和ILB访问相同web服务(1)

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》