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版本可以在相关向下兼容的代码中实现。

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

"@+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中实现:

"@+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 布局



<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>



<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);

    }

你可能感兴趣的:(Android)