您的位置:首页 > 移动开发 > Android开发

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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android tint