Android对图片Drawable实现变色示例代码
作者:fengsehng 时间:2021-11-25 15:49:47
前言
本文主要给大家介绍了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
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
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;
}
}
来源:http://www.jianshu.com/p/9ab105b505a2
标签:android,drawable图片,变色
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java Swing JComboBox下拉列表框的示例代码
2022-09-16 00:24:50
![](https://img.aspxhome.com/file/2023/6/79886_0s.gif)
spring boot 枚举使用的坑整理
2022-09-17 09:03:15
C++实现的求解多元一次方程示例
2022-09-26 19:45:47
WebSocket实现Web聊天室功能
2023-11-27 06:10:52
![](https://img.aspxhome.com/file/2023/9/60189_0s.jpg)
java向多线程中传递参数的三种方法详细介绍
2023-07-11 21:52:55
Java四种常用线程池的详细介绍
2021-09-29 17:45:46
C#汉字转换拼音技术详解(高性能)
2022-01-19 02:03:16
Java线程安全中的有序性浅析
2023-07-12 03:07:58
![](https://img.aspxhome.com/file/2023/4/96114_0s.png)
Android登录注册功能 数据库SQLite验证
2023-10-01 20:58:55
![](https://img.aspxhome.com/file/2023/6/86926_0s.gif)
IDEA2022版本创建maven web项目的两种方式详解
2022-04-24 19:34:54
![](https://img.aspxhome.com/file/2023/5/84785_0s.jpg)
IDEA社区版下载安装流程详解(小白篇)
2021-11-13 12:37:18
![](https://img.aspxhome.com/file/2023/4/61684_0s.png)
使用@Value值注入及配置文件组件扫描
2023-12-01 21:24:12
详解Java实现缓存(LRU,FIFO)
2022-04-24 13:35:26
![](https://img.aspxhome.com/file/2023/1/64101_0s.png)
MyBatis根据条件批量修改字段的方式
2023-12-16 03:24:01
详解C#对路径...的访问被拒绝解决过程
2021-06-01 11:12:10
unity3d实现七天签到功能
2023-01-05 08:57:21
解决Mybatis-Plus操作分页后数据失效问题
2023-11-25 11:31:55
JDK 7 新特性小结实例代码解析
2022-04-18 13:03:07
Java concurrency线程池之线程池原理(二)_动力节点Java学院整理
2023-11-28 23:43:18
Java多线程编程实战之模拟大量数据同步
2023-09-02 21:15:59