RadioButton用Tint属性实现一张图片2中效果

前言:最近在学性能优化时接触到Tint这个非常有意思的属性,接下来用RadioButton来实现。

Tint(着色器)它能够实现图片变色,利用Tint可以将一张图片着色成不同颜色的图片,原本需要多张相同图片不同颜色则可以用一张图片替代,能够减少apk体积。

效果如图:

01.gif

接下来直接贴代码

 <RadioButton
        android:id="@+id/rb_video"
        style="@style/rbStyle"
        android:checked="true"
        android:drawableTop="@drawable/tint_rb_video_src_selector"
        android:text="@string/file_video"/>


<style name="rbStyle">//公共样式
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_gravity">center_vertical</item>
    <item name="android:gravity">center</item>
    <item name="android:drawablePadding">3dp</item>
    <item name="android:layout_weight">1</item>
    <item name="android:textSize">12sp</item>
    <item name="android:textColor">@color/tint_rb_video_texcolor_selector</item>
    <item name="android:button">@android:color/transparent</item>
    <item name="android:drawableTint">@color/tint_rb_video_color_selector</item>
</style>


文字直接用选择器好了

<item name="android:textColor">@color/tint_rb_video_texcolor_selector</item>

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/rbSelectColor"android:state_checked="true">
    <item android:color="@color/rbDefaultColor"/>
</selector>


对于RadioButton里的drawableTint属性,需要API>=23才能生效

API>=23可以直接在xml布局里main写,要注意的是:选择器里的@drawable不管是哪种选中状态必须是同一张图片,否则drawableTint不起作用

 <item name="android:drawableTint">@color/tint_rb_video_color_selector</item>
 android:drawableTop="@drawable/tint_rb_video_src_selector"

tint_rb_video_src_selector.xml

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

兼容API<23需要代码动态设置

 /**
 * 为了兼容API<23 drawableTint不起作用
 * 此代码是在MVP架构项目中Presenter copy的,所以要传一个context
 *
 * @param drawableId 需要更改的图片ID
 * @return 返回修改后的图片
 */
@Override
public Drawable setDrawableTopTintColor(int drawableId, Context context) {
    Drawable drawable = DrawableCompat.wrap(context.getResources().getDrawable(drawableId));
    ColorStateList colorStateList = context.getResources().getColorStateList(R.color.tint_rb_video_color_selector);
    DrawableCompat.setTintList(drawable, colorStateList);
    return drawable;
}


从源码浅析Tint

既然要兼容低版本的API,那么就要用v4包下的DrawableCompat类

  1. 首先要获取想要着色的图片。第一时间想到的是用DrawableCompat.wrap()方法来获取图片,看到这里有人可能会想为什么要用wrap()来获取而不是直接获取呢?接下来看看源码是怎么实现的。

先来看看DrawableCompat.wrap()方法写了什么。

public static Drawable wrap(@NonNull Drawable drawable) {
    return IMPL.wrap(drawable);
}

可以看到直接返回IMPL.wrap(drawable),但这个IMPL又是什么呢?

 /**
 * Select the correct implementation to use for the current platform.
 * 为当前平台选择正确的实现
 */
static final DrawableCompatBaseImpl IMPL;
static {
    if (Build.VERSION.SDK_INT >= 23) {
        IMPL = new DrawableCompatApi23Impl();
    } else if (Build.VERSION.SDK_INT >= 21) {
        IMPL = new DrawableCompatApi21Impl();
    } else if (Build.VERSION.SDK_INT >= 19) {
        IMPL = new DrawableCompatApi19Impl();
    } else if (Build.VERSION.SDK_INT >= 17) {
        IMPL = new DrawableCompatApi17Impl();
    } else {
        IMPL = new DrawableCompatBaseImpl();
    }
}

可以看到这个IMPL对象是根据不同平台创建的,IMPL.wrap(drawable)方法又写了什么呢,接下来点进去看看。其中DrawableCompatApi23Impl里的wrap方法直接返回drawable并没有做封装处理。

   public Drawable wrap(Drawable drawable) {
        if (!(drawable instanceof TintAwareDrawable)) {
            return new DrawableWrapperApi14(drawable);
        }
        return drawable;
    }

这里判断传进来的drawable是否为TintAwareDrawable类型,是就直接返回当前drawable。TintAwareDrawable既然是tint开头的,那就应该和tint相关的一个类。看下TintAwareDrawable里面有什么。

/**
 * Interface which allows a {@link android.graphics.drawable.Drawable} to receive tinting calls
 * from {@code DrawableCompat}.
 *
 * @hide
 */
@RestrictTo(LIBRARY_GROUP)
public interface TintAwareDrawable {
    void setTint(@ColorInt int tint);
    void setTintList(ColorStateList tint);
    void setTintMode(PorterDuff.Mode tintMode);
}

TintAwareDrawable接口提供了三个回调,上面判断不是TintAwareDrawable类型就创建DrawableWrapperApi14对象,那么DrawableWrapperApi14应该实现这个接口。

 /**
 * Creates a new wrapper around the specified drawable.
 *
 * @param dr the drawable to wrap
 */
DrawableWrapperApi14(@Nullable Drawable dr) {
    mState = mutateConstantState();
    // Now set the drawable...
    setWrappedDrawable(dr);
}

DrawableWrapperApi14这个对象将传进来的drawable封装一遍,具体是怎么封装的就不带着跟源码了,感兴趣的请自行查看源码。


2.ColorStateList是颜色状态列表,设置tint要用到,可以通过构造方法创建,也可以从xml获取

/**
 * Creates a ColorStateList that returns the specified mapping from states to colors.
 */
public ColorStateList(int[][] states, @ColorInt int[] colors) {
    mStateSpecs = states;
    mColors = colors;

    onColorsChanged();
}

这个构造方法目的是创建一个状态跟颜色一一映射的列表

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,277评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,689评论 3 393
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,624评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,356评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,402评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,292评论 1 301
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,135评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,992评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,429评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,636评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,785评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,492评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,092评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,723评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,858评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,891评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,713评论 2 354

推荐阅读更多精彩内容