Android中使用tint属性和backgroundTint属性瘦身

在Android中,ImageView有一个android:tint属性,这个属性可以改变ImageView的图片颜色,主要应用在适量图处,这样,我们就不需要在res文件中存放多张不同颜色的矢量图,可以根据自己的需求来设置相关的Tint属性来达到我们的目的,这样不仅省去了多余图片占据的空间,可以使APP瘦身,同样,在相关布局中的设计也更为明显。tint属性还可以用在selector中,达到选择器的效果。注意:在使用tint实现选择器的时候,需要在src中设置相应的selector(虽然实现的drawable是一样的,但是还是需要都写),还要在tint中使用selector才可达到相应的效果。
But,tint属性在Android5.x以上的版本才可以直接使用,那么在低与5.x版本可以在相关向下兼容的代码中实现。

在布局中简单使用(直接修改图片着色):

<ImageView
        android:id="@+id/test_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:tint="@color/colorAccent"
        android:src="@mipmap/ic_launcher"/>

方便兼容低版本,可以在相关代码中实现:

public void setTint() {
    //获得相关的Drawable
        Drawable drawable = ContextCompat.getDrawable(this, R.mipmap.ic_launcher);
        test_iv.setImageDrawable(drawable);

    //获得ConstantState状态
        Drawable.ConstantState state = drawable.getConstantState();
        Drawable test_drawable = DrawableCompat.wrap((state == null ? drawable : state.newDrawable()).mutate());

        test_drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());

    //DrawableCompat向下兼容设置tint
        DrawableCompat.setTint(test_drawable, ContextCompat.getColor(this, R.color.colorAccent));

        test_iv.setImageDrawable(test_drawable);

    }

相关选择器实现
在xml中实现:

<ImageView
        android:id="@+id/test_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:tint="@color/icon"
        android:src="@drawable/icon_selector"/>

相关drawable/icon_selector 和color/icon 布局

<!-- drawable/icon_selector -->
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" android:drawable="@mipmap/ic_launcher"/>

    <item android:drawable="@mipmap/ic_launcher"/>

</selector>

<!-- color/icon -->
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" android:color="@color/colorAccent"/>

    <item android:color="@color/colorPrimary"/>

</selector>

在代码里实现:

public void setTintSelector() {
        Drawable drawable = ContextCompat.getDrawable(this, R.mipmap.ic_launcher);
        //颜色数组
        int colors[] = new int[]{ContextCompat.getColor(this, R.color.colorAccent), ContextCompat.getColor(this, R.color.colorPrimary)};

    //状态
        int states[][] = new int[2][];

        states[0] = new int[]{android.R.attr.state_pressed};
        states[1] = new int[]{};

    //颜色状态集
        ColorStateList colorStateList = new ColorStateList(states, colors);

    //状态选择集
        StateListDrawable stateListDrawable = new StateListDrawable();

        stateListDrawable.addState(states[0], drawable);
        stateListDrawable.addState(states[1], drawable);

        Drawable.ConstantState state = stateListDrawable.getConstantState();
        drawable = DrawableCompat.wrap(state == null ? drawable : state.newDrawable()).mutate();
        DrawableCompat.setTintList(drawable, colorStateList);

        test_iv.setImageDrawable(drawable);
        test_iv.setClickable(true);
    }

但是针对与不同的View,我们一般常用的方法是设置其相应的background,So,Android也给我们提供了相应的backgroundTint的属性,举一反三,用法和tint相似。

提供一个通用的设置相关selector和着色的方法,主要是针对不同的View设置background的方式实现的。

import android.app.Activity;
import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.view.View;

/**
 * Created by Administrator on 2016/11/23.
 */

public class SelectorUtils {

    public static SelectorUtils instance;

    public static SelectorUtils newInstance() {
        if (instance == null) {
            synchronized (SelectorUtils.class) {
                if (instance == null) {
                    instance = new SelectorUtils();
                }
            }
        }
        return instance;
    }

    /**
     * 主要针对的是ImageView设置图片,
     * 图片资源为一张,通过tint来修改不同状态时显示的不同背景,
     * 以达到节约资源,减少内存的目的
     *
     * @param activity      当前的Activity或者Fragment
     * @param view          需要修改的View,主要只ImageView
     * @param drawableRes   drawable资源id
     * @param normalColor   正常时的颜色
     * @param selectorColor 选中时的颜色
     */
    public void viewSetSelector(Activity activity, View view, int drawableRes, int normalColor, int selectorColor) {
        Drawable drawable = ContextCompat.getDrawable(activity, drawableRes);
        //获得选中颜色和非选中颜色
        int colors[] = new int[]{ContextCompat.getColor(activity, selectorColor),
                ContextCompat.getColor(activity, normalColor)};
        //点击状态数组
        int states[][] = new int[2][];

        //点击状态
        states[0] = new int[]{android.R.attr.state_pressed};
        //非点击状态
        states[1] = new int[]{};
        //存放状态值和颜色
        ColorStateList colorStateList = new ColorStateList(states, colors);

        //存放相应状态和drawable
        StateListDrawable stateListDrawable = new StateListDrawable();
        stateListDrawable.addState(states[0], drawable);
        stateListDrawable.addState(states[1], drawable);

        Drawable.ConstantState state = stateListDrawable.getConstantState();
        drawable = DrawableCompat.wrap(state == null ? drawable : state.newDrawable()).mutate();

        DrawableCompat.setTintList(drawable, colorStateList);
        view.setClickable(true);
        //改变背景Drawable
        view.setBackgroundDrawable(drawable);
        //如果是ImageView,可以设置src相关
        //view.setImageDrawable(drawable);
    }

    /**
     * 改变View的状态的图示,通过Tint减少资源和内存
     *
     * @param activity    当前Activity或者Fragment
     * @param view        当前需要改变的View
     * @param drawableRes 资源id
     * @param colorRes    color资源
     */
    public void changeViewState(Activity activity, View view, int drawableRes, int colorRes) {
        Drawable drawable = ContextCompat.getDrawable(activity, drawableRes);

        int color = ContextCompat.getColor(activity, colorRes);

        Drawable.ConstantState state = drawable.getConstantState();

        drawable = DrawableCompat.wrap(state == null ? drawable : state.newDrawable()).mutate();

        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());

        DrawableCompat.setTint(drawable, color);

        view.setBackgroundDrawable(drawable);
        //如果是ImageView,可以设置src相关
        //view.setImageDrawable(drawable);

    }
  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值