首页 > 分享 > Android最新的Tint,能玩出什么花?

Android最新的Tint,能玩出什么花?

最新推荐文章于 2024-11-08 09:17:36 发布

tudoubudou 于 2016-06-01 15:31:39 发布

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

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"/>12345678910

上面这个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>12345678

代码中:

..... 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); }1234567891011121314

相关知识

情侣玩出花游戏安卓版
android:layout
玩出新花样?养生也能“新中式”?
Android使用字体图标库
车灯也能玩出花,极氪开始展现灯光艺术
文字玩出花怀旧健身操怎么过-怀旧健身操通关攻略
Android移动开发
Android进阶之路
【Android开发那点破事】Android中Activity的生命周期
Android中字体的处理

网址: Android最新的Tint,能玩出什么花? https://m.huajiangbk.com/newsview1212967.html

所属分类:花卉
上一篇: 花youngapp免费下载
下一篇: 16年前,谷歌为何花5000万美