ImageView的tint屬性

爲ImageView圖片重新着色
透明的部分不會改變

1.第一種,設置單一顏色

這裏寫圖片描述————>這裏寫圖片描述

imageView.setColorFilter(Color.RED);

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);

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>

設置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);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章