Android 图片着色 Tint 详解2—xml设置、selector

上篇文章介绍了给drawable设置tint 以达到颜色变化效果,这篇介绍下通过xml设置tint、代码设置tint以及如何设置tint selector。

在xml中设置tint

可以利用 android:tint 以及 android:tintMode 属性设置布局中的着色颜色和模式。而对背景进行着色可以通过设置 android:backgroundTint 属性,但是需要注意几点:

  1. 在布局中设置android:backgroundTint 属性首先要设置 android:background 属性,否则会无效;
  2. android:backgroundTint 属性需要最小支持的api为21,所以为了兼容api版本低于21的情况,需要使用 app:backgroundTint,命名空间为xmlns:app=”http://schemas.android.com/apk/res-auto”

Code设置tint

ImageView 中有 setImageTintList 方法,可用于给 drawable 进行着色,但是同样得先设置过drawable才会生效,而且限制api 版本最低是21。另外可以给background 设置着色,view.setBackgroundTintList 同样限制api版本最低支持21。

ColorStateList colorStateList = ColorStateList.valueOf(Color.parseColor("#5a8386"));
// 需要先设置background才会生效
mBtn.setBackgroundTintList(colorStateList);

// 需要先设置image(drawable)才会生效
mImageView.setImageTintList(colorStateList);

在api < 21 时,可以使用兼容包中的控件 AppCompatEditText、AppCompatTextView、AppCompatButton等

mCompatBtn.setSupportBackgroundTintList(colorStateList);

另外可以通过给drawable 设置着色,然后设置图片或者背景 也可实现着色效果。本篇不重点介绍了,有意向可前往上一篇文章的详细讲解,梯子已经备好!

http://blog.csdn.net/jm_beizi/article/details/54916965 Android 图片着色 Tint 详解

简单示例:

Drawable bmpDrawable = ContextCompat.getDrawable(this, R.drawable.icon_beijing);

Drawable.ConstantState state = bmpDrawable.getConstantState();
// 对drawable 进行重新实例化、包装、可变操作
Drawable wrap = DrawableCompat.wrap(state == null ? bmpDrawable : state.newDrawable()).mutate();
// 进行着色 setTint和setTintList 方法都可以进行着色
DrawableCompat.setTint(wrap, Color.parseColor("#5a8386"));
//DrawableCompat.setTintList(wrap, colorStateList);

mImageView1.setImageDrawable(wrap);
//mImageView1.setBackground(wrap);
原图 这里写图片描述
ImageView 背景着色 Android tint
Button背景着色 Android tint
AppCompatButton背景着色 Android tint

Selector Tint

在res/color 文件下创建一个颜色选择器
eg:
selector_color_button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#ff4081" android:state_pressed="true" /> <!-- pressed -->
    <item android:color="#30c3a6" /> <!-- default -->
</selector>

在res/drawable文件下创建一个图片选择器
selector_drawable_button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/icon_beijing" android:state_pressed="true" />
    <item android:drawable="@drawable/icon_beijing" />
</selector>

code设置selector

非兼容包控件
1、实例化Drawable对象,设置Background

Drawable selectorDrawable = ContextCompat.getDrawable(this, R.drawable.selector_drawable_button);
mImageView2.setBackground(selectorDrawable);

2、读取颜色选择器文件实例化对象

ColorStateList colorStateList = ContextCompat.getColorStateList(this, R.color.selector_color_button);

3、设置tintList

// 这里做个版本兼容,如果api 版本>21,就使用系统提供的setBackgroundTintList方法来进行着色;如果api版本不高于21,就
// 使用设置drawable的方式进行着色
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
       mImageView2.setBackgroundTintList(colorStateListSelector);

} else {          
       mImageView2.setBackground(
              SkxDrawableHelper.tintListDrawable(mImageView2.getBackground(), colorStateListSelector));
}

4、如果是ImageView 需要设置 setClickable(true) 或者android:clickable=“true”,否则还是不会实现selector 效果。

兼容包控件
如果是兼容包里的控件如 AppCompatButton,直接用兼容控件里的着色方法就可以。而且drawable的设置直接选用图片就可以,不需要用 selector drawable

 Drawable bgDrawable = ContextCompat.getDrawable(this, R.drawable.icon_beijing);
 mCompatBtn4.setBackground(bgDrawable);
 mCompatBtn4.setSupportBackgroundTintList(colorStateListSelector);

xml设置selector

和code设置一样,有几个关键点。background、backgroundTint

1、设置Background 属性,注意这里是用的图片选择器,而不是一个单独的图片。另外即使用的是 AppCompatButton之类的兼容类属性值也需要设置成selector 而不是drawable。

  android:background="@drawable/selector_drawable_button"

2、设置背景着色,即backgroundTint属性,如果需要兼容5.0以下版本时需要使用命名空间为 app下的属性,此处应是颜色选择器。

app:backgroundTint="@color/selector_color_button"

3、如果是ImageView 需要设置 android:clickable=”true”

效果图:

目标view 效果
ImageView 通过tint设置背景选择器 ImageView 设置tint selector
Button通过tint设置背景选择器 Button 设置tint selector
AppCompatButton 通过tint设置背景选择器 这里写图片描述

关于为什么background 设置BitmapDrawable 不行,而StateListDrawable是ok的 我也没太懂。参考网上的说法感觉还是懵,下面是我列出来的测试的几个情况:
Android selector tint 测试

从测试的这几组数据来看,不确定是否跟系统版本有关系。


其实在用 drawable 选择器之前还有一个小插曲,算是我误打误撞的结果。
我上一下示例代码

     <android.support.v7.widget.AppCompatButton
            android:id="@+id/tintTest2_btn3"
            android:layout_width="0dp"
            android:layout_height="70dp"
            android:layout_weight="1"
            android:textColor="@color/selector_color_button"
            android:background="@drawable/icon_beijing"
            app:backgroundTint="@color/selector_color_button" />

上面代码对应下图左边的这个,这里没有用StateListDrawable,之前给文字设置了个颜色选择器没有删除,运行了下程序,结果替tint selector 是可行的!
这里写图片描述

发布了22 篇原创文章 · 获赞 44 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/JM_beizi/article/details/54959007