Android最新的Tint,能玩出什么花?
2016-06-01 15:31
573 查看
Android最新的Tint,能玩出什么花?
最新做项目偶然发现一个并不怎么常用的功能Tint(翻译为着色)之前也看到过,但是查询api后发现只有apk 21以后才支持,所以就没怎么再看,但是刚才稍微升入一点研究了一下,发现其实只要项目中用到v4包,实际上就可以用v4包里面的DrawableCompat实现tint。Tint可以干啥?
简单来说,我们很多在图案的按钮的点击态,现有的做法是用两张图片,例如:这样的点击态如果用tint就可以通过对同一张图片着色,从而减少一张图片了。
此外如果结合tintMode:用法可以参考这篇文章:
http://blog.csdn.net/u010687392/article/details/47399719
那么这样的点击态也是可以通过tint做到,同样可以减少一张图片资源:
废话不说了,直接上代码
<ImageView android:visibility="gone" android:background="?attr/selectableItemBackgroundBorderless" android:id="@+id/close_icon" android:src="@drawable/close_button_selector" android:padding="15dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="left|top" android:scaleType="centerInside"/>
上面这个ImageView的background一定别遗漏,否则在api 10这样的系统就没效果了。
其中close_button_selector如下:
<?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"> <bitmap android:src="@drawable/abc_ic_clear_mtrl_alpha" android:tint="@color/color_trans_0d86ff"/> </item> <item android:state_pressed="false" android:drawable="@drawable/abc_ic_clear_mtrl_alpha" /> </selector>
代码中:
..... close_button = (ImageView) findViewById(R.id.close_icon); if (Build.VERSION.SDK_INT < 21) { tintButton(close_button); } ..... public static void tintButton(@NonNull ImageView button) { ColorStateList colours = ContextCompat.getColorStateList(button.getContext(),R.drawable.button_colors); Drawable d = DrawableCompat.wrap(button.getDrawable()); DrawableCompat.setTintList(d, colours); button.setImageDrawable(d); }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories