为ImageView图片重新着色
透明的部分不会改变
1.第一种,设置单一颜色
————>
imageView.setColorFilter(Color.RED);
- 1
2.可以设置单一颜色或者selector
Drawable icon = getResources().getDrawable(R.drawable.tabbar_stat);
Drawable tintIcon = DrawableCompat.wrap(icon);
ColorStateList csl=getResources().getColorStateList(R.color.colorAccent);
DrawableCompat.setTintList(tintIcon,csl);
imageView.setImageDrawable(tintIcon);
- 1
- 2
- 3
- 4
- 5
color的selector:selector_tint.xml(color包)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/colorAccent" android:state_pressed="true"/>
<item android:color="@color/colorPrimary"/>
</selector>
- 1
- 2
- 3
- 4
- 5
设置selector
Drawable icon = getResources().getDrawable(R.drawable.tabbar_stat);
Drawable tintIcon = DrawableCompat.wrap(icon);
ColorStateList csl=getResources().getColorStateList(R.color.selector_tint);
DrawableCompat.setTintList(tintIcon,csl);
imageView.setImageDrawable(tintIcon);
- 1
- 2
- 3
- 4
- 5
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-ff98e99283.css" rel="stylesheet">
</div>
</article>