日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

android 图片变颜色代码,Android -对图片Drawable进行变色

發布時間:2024/9/3 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 图片变颜色代码,Android -对图片Drawable进行变色 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Android 圖片DrawableCompat利用setTint()對圖片Drawable進行變色

1.利用color資源對Drawable變色

Drawable對象的來源不限制,可以是從資源getResource().getDrawable(int resourceId)也可以是其他的方式得到的Drawable

Drawable wrappedDrawable = DrawableCompat.wrap(drawable);

DrawableCompat.setTint(wrappedDrawable, color);

2.采用ColorStateList來改變Drawable

Drawable wrappedDrawable = DrawableCompat.wrap(drawable);

DrawableCompat.setTintList(wrappedDrawable, colors);

3.介紹一下ColorStateList

android:color="#ffff0000"/>

android:color="#ff0000ff"/>

Java代碼(用于改變按鈕的字體顏色)

Button btn=(Button)findViewById(R.id.btn);

Resources resource=(Resources)getBaseContext().getResources();

ColorStateList csl=(ColorStateList)resource.getColorStateList(R.color.button_text);

if(csl!=null){

btn.setTextColor(color_state_list);//設置按鈕文字顏色

}

4.安利一個朋友封裝的工具類

public class DrawableTintUtil {

/**

* Drawable 顏色轉化類

*

* @param drawable

* @param color資源

* @return 改變顏色后的Drawable

*/

public static Drawable tintDrawable(@NonNull Drawable drawable, int color) {

Drawable wrappedDrawable = DrawableCompat.wrap(drawable);

DrawableCompat.setTint(wrappedDrawable, color);

return wrappedDrawable;

}

/**

* Drawable 顏色轉化類

*

* @param drawable 源Drawable

* @param ColorStateList

* @return 改變顏色后的Drawable

*/

public static Drawable tintListDrawable(@NonNull Drawable drawable, ColorStateList colors) {

Drawable wrappedDrawable = DrawableCompat.wrap(drawable);

DrawableCompat.setTintList(wrappedDrawable, colors);

return wrappedDrawable;

}

}

總結

以上是生活随笔為你收集整理的android 图片变颜色代码,Android -对图片Drawable进行变色的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。