Android ColorState使用

来源:互联网 发布:高分少女 知乎: 编辑:程序博客网 时间:2024/05/17 07:50

1.前言

在Android开发者中,对图片的使用是必不可少的,有时候不会切图,有时候是因为图标过多导致应用程序包过大等等,常见的就是在图标的几种状态,一般都是默认和选中两种,我们给图片着色来让它只用一张图标可以配置任何我们想要的图标颜色,我们这里提到一个小技巧来处理这些事情,我们尽可能的写一些代码来改变这种情况。

2.问题

在官方的做法一般是我们会去像下面那样写资源文件通过配置不同的图片来解决来标明在不同状态下对应显示的图标

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_pressed="true"          android:drawable="@drawable/button_pressed" /> <!-- pressed -->    <item android:state_focused="true"          android:drawable="@drawable/button_focused" /> <!-- focused -->    <item android:state_hovered="true"          android:drawable="@drawable/button_focused" /> <!-- hovered -->    <item android:drawable="@drawable/button_normal" /> <!-- default --></selector>

但是有时候我们需要动态改变图标颜色,而这个图标颜色又比较多,当然把每种颜色都切出来很显然不显示。只能通过写代码给图标着色来解决。

3.实践

提起着色,Android SDK给我们提供了一个这样的类:ColorStateList,它用来设置在不同状态下对应的颜色,经常做法如下:

<selector xmlns:android="http://schemas.android.com/apk/res/android">   <item android:state_focused="true" android:color="@color/testcolor1"/>   <item android:state_pressed="true" android:state_enabled="false" android:color="@color/testcolor2" />   <item android:state_enabled="false" android:color="@color/testcolor3" />   <item android:color="@color/testcolor5"/> </selector>

我们可以以此入手:

  • 继承ImageView。
  • 自定义属性colorStateList,写selector文件。
<selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:color="@color/colorPrimary" android:state_selected="true"/>    <item android:color="@color/colorAccent"  /></selector>
  • 重写drawableStateChanged()方法:官网上说当drawable的状态在改变时会调用此方法。
  • 调用setColorFilter()方法为图片着色

    完整代码:

public class TintStateImage extends ImageView{    private static final int IMAGE_DEFAULT_COLOR = Color.GRAY;    private ColorStateList mColorStateList;    public TintStateImage(Context context) {        this(context,null);    }    public TintStateImage(Context context, AttributeSet attrs) {        this(context, attrs, 0);    }    public TintStateImage(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.TintStateImage);        mColorStateList = ta.getColorStateList(R.styleable.TintStateImage_colorStateList);        ta.recycle();    }    @Override    protected void drawableStateChanged() {        super.drawableStateChanged();        if (mColorStateList != null && mColorStateList.isStateful()){            int color = mColorStateList.getColorForState(getDrawableState(),                    IMAGE_DEFAULT_COLOR);            super.setColorFilter(color, PorterDuff.Mode.SRC_IN);        }    }}

使用:

<com.yuxingxin.library.TintStateImage    android:id="@+id/selected_state"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    app:colorStateList="@drawable/image_style"    android:src="@mipmap/ic_home_black_24dp"    />

别忘了给你的view设置状态就可以了。

demo:https://github.com/yuxingxin/TintStateImage

0 0
原创粉丝点击