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

Android 低版本实现Tint--着色功能

2015-09-05 00:49 537 查看

Tint

这是个啥玩意?翻译过来 == 着色,安卓5.x以上就支持着色功能了,那具体有啥用了?看完这篇文章你就懂了。想了解更多Tint知识,可发挥强大的搜索引擎功能

PS:这里我们就实现一个低版上也可以用的着色控件—-TintEditText

需求:在焦点改变时改变EditText的DrawableLeft图片颜色

疑问:有人就说了,这很简单,添加监听判断是否有焦点然后设置不同状态的图片或是直接写个selector

上面的疑问确实可以很好的实现我们的需求,但这里我们只是重点说怎么在低版本上也能兼容Tint着色功能

图片的准备

图片素材准备一张纯白色的图片就行,无需准备两张不同状态的图片。

PS:就说这点,我们是不是有必要去研究了了?因为这样可以大大减少我们工程中用到的图片数量,至少不同状态的图片现在只需要一张了。

据说透明的图片也可以实现着色,我目前还没实现

TintEditText.java

//**
* 这里我们要导入v7兼容包
* 这里继承至AppCompatEditText是为了我们的Custom EditText可以拥有5.x以上主题效果
* Created by JaySeng on 2015/9/4.
*//
public class TintEditText extends AppCompatEditText implements View.OnFocusChangeListener {
private Drawable drawableLeft;
private Drawable wrappedDrawable;
private Drawable drawableTop;
private Drawable drawableRight;
private Drawable drawableBottom;
public TintEditText(Context context) {
super(context);
init();
}
public TintEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public TintEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
/*我们都知道,TextView的子类都是可以设置Drawable的,当前咯,不此TextView,不知道自己去查资料*/
setOnFocusChangeListener(this);
Drawable[] compoundDrawables = getCompoundDrawables();
drawableLeft = compoundDrawables[0];//左
drawableTop = compoundDrawables[1];//上
drawableRight = compoundDrawables[2];//右
drawableBottom = compoundDrawables[3];//下
if(drawableLeft != null){
/*这里判断下表示设置的左边图片*/
wrappedDrawable = DrawableCompat.wrap(drawableLeft);
drawableLeft = wrappedDrawable;
/*设置默认着色*/
DrawableCompat.setTint(wrappedDrawable, getCurrentHintTextColor());
setCompoundDrawables(drawableLeft, drawableTop, drawableRight, drawableBottom);
}
}
//**
* Called when the focus state of a view has changed.
*  当前焦点改变时回调此方法
* @param v        The view whose state has changed.
* @param hasFocus The new focus state of v.
*/
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
/*如果有焦点,就设置成当前文本的颜色,这里的颜色可以自己去修改,也可以自己自定义属性在布局里设置*/
DrawableCompat.setTint(wrappedDrawable, getCurrentTextColor());
}else{
/*如果没有焦点,就设置成当前提示文本颜色*/
DrawableCompat.setTint(wrappedDrawable, getCurrentHintTextColor());
}
setCompoundDrawables(drawableLeft,drawableTop,drawableRight,drawableBottom);
}
}


layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<com.qjay.tintdemo.TintEditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:drawableLeft="@mipmap/ic_xiugaixuqiu"
android:drawablePadding="5dp"
android:textColor="#F00" />
<com.qjay.tintdemo.TintEditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:drawableLeft="@mipmap/msg_ic_supply1"
android:drawablePadding="5dp"
android:textColor="#F00" />
<com.qjay.tintdemo.TintEditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:drawableLeft="@mipmap/msg_ic_system1"
android:drawablePadding="5dp"
android:textColor="#F00" />
</LinearLayout>


layout.xml的写法没任何改变,还是以前的用法

预览效果



效果如上图所示,其它不多说,这里说下我们的TintEditText在有焦点时底部的横线也会改变颜色,其实官方也是用的着色功能实现,大家可以看源码的实现

我用的三张图片都是纯白色,大家可以下载工程直接运行看效果

项目地址:http://download.csdn.net/detail/qjay_dev/9078999
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android Tint EditText