用代码快速构建selector的工具类SelectorHepler

SelectorHepler工具类

一般需要点击效果或者选中、焦点的状态改变后也改变效果的话,需要在drawable文件夹下面新建一个xml文件,然后写一个selector。相对来说比较麻烦,所以我写了一个用代码快速建一个selector的工具类。

SelectorHepler代码:

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.InsetDrawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.StateListDrawable;

public class SelectorHepler {
    private static SelectorHepler util;

    public static SelectorHepler getInstance() {

        if (util == null) {
            util = new SelectorHepler();

        }
        return util;

    }

    private SelectorHepler() {
        super();
    }

    /**
     * 传入Drawable的id,得到一个Selector,一般给setBackgroundDrawable使用
     * @param context
     * @param idNormal
     * @param idPressed
     * @param idFocused
     * @param idUnable
     * @return
     */
    public StateListDrawable getSelectorDrawable(Context context, int idNormal,
            int idPressed, int idFocused, int idUnable) {
        StateListDrawable bg = new StateListDrawable();
        Drawable normal = idNormal == -1 ? null : context.getResources()
                .getDrawable(idNormal);
        Drawable pressed = idPressed == -1 ? null : context.getResources()
                .getDrawable(idPressed);
        Drawable focused = idFocused == -1 ? null : context.getResources()
                .getDrawable(idFocused);
        Drawable unable = idUnable == -1 ? null : context.getResources()
                .getDrawable(idUnable);
        // View.PRESSED_ENABLED_STATE_SET
        bg.addState(new int[] { android.R.attr.state_pressed,
                android.R.attr.state_enabled }, pressed);
        // View.ENABLED_FOCUSED_STATE_SET
        bg.addState(new int[] { android.R.attr.state_enabled,
                android.R.attr.state_focused }, focused);
        // View.ENABLED_STATE_SET
        bg.addState(new int[] { android.R.attr.state_enabled }, normal);
        // View.FOCUSED_STATE_SET
        bg.addState(new int[] { android.R.attr.state_focused }, focused);
        // View.WINDOW_FOCUSED_STATE_SET
        bg.addState(new int[] { android.R.attr.state_window_focused }, unable);
        // View.EMPTY_STATE_SET
        bg.addState(new int[] {}, normal);
        return bg;
    }

    /**
     * 得到点击改变状态的Selector,一般给setBackgroundDrawable使用
     * @param context
     * @param idNormal
     * @param idPressed
     * @return
     */
    public StateListDrawable getPressedSelectorDrawable(Context context, int idNormal,
            int idPressed) {

        Drawable normal = idNormal == -1 ? null : context.getResources()
                .getDrawable(idNormal);
        Drawable pressed = idPressed == -1 ? null : context.getResources()
                .getDrawable(idPressed);

        StateListDrawable bg = getPressedSelectorDrawable( normal, pressed);
        return bg;
    }

    /**
     * 得到点击改变状态的Selector,一般给setBackgroundDrawable使用
     * @param normal
     * @param pressed
     * @return
     */
    public StateListDrawable getPressedSelectorDrawable(
            Drawable normal, Drawable pressed) {
        StateListDrawable bg = new StateListDrawable();

        bg.addState(new int[] { android.R.attr.state_pressed,
                android.R.attr.state_enabled }, pressed);

        bg.addState(new int[] { android.R.attr.state_enabled }, normal);

        bg.addState(new int[] {}, normal);
        return bg;
    }

    /**得到选中改变状态的Selector,一般给setBackgroundDrawable使用
     * @param context
     * @param idNormal
     * @param idchecked
     * @return
     */
    public StateListDrawable getCheckedSelectorDrawable(Context context, int idNormal,
            int idchecked) {

        Drawable normal = idNormal == -1 ? null : context.getResources()
                .getDrawable(idNormal);
        Drawable checked = idchecked == -1 ? null : context.getResources()
                .getDrawable(idchecked);

        StateListDrawable bg = getCheckedSelectorDrawable(normal, checked);
        return bg;
    }

    /**
     * 得到选中改变状态的Selector,一般给setBackgroundDrawable使用
     * @param normal
     * @param checked
     * @return
     */
    public StateListDrawable getCheckedSelectorDrawable(
            Drawable normal, Drawable checked) {
        StateListDrawable bg = new StateListDrawable();

        bg.addState(new int[] { android.R.attr.state_checked,
                android.R.attr.state_enabled }, checked);

        bg.addState(new int[] { android.R.attr.state_enabled }, normal);

        bg.addState(new int[] {}, normal);
        return bg;
    }

    /**
     * 得到焦点改变即改变状态的Selector,一般给setBackgroundDrawable使用
     * @param context
     * @param idNormal
     * @param idchecked
     * @return
     */
    public StateListDrawable getFocusedSelectorDrawable(Context context, int idNormal,
            int idchecked) {

        Drawable normal = idNormal == -1 ? null : context.getResources()
                .getDrawable(idNormal);
        Drawable checked = idchecked == -1 ? null : context.getResources()
                .getDrawable(idchecked);

        StateListDrawable bg = getFocusedSelectorDrawable(normal, checked);
        return bg;
    }

    /**
     * 得到焦点改变即改变状态的Selector,一般给setBackgroundDrawable使用
     * @param normal
     * @param focused
     * @return
     */
    public StateListDrawable getFocusedSelectorDrawable(
            Drawable normal, Drawable focused) {
        StateListDrawable bg = new StateListDrawable();

        bg.addState(new int[] { android.R.attr.state_enabled,
                android.R.attr.state_focused }, focused);
        bg.addState(new int[] { android.R.attr.state_focused }, focused);
        bg.addState(new int[] { android.R.attr.state_enabled }, normal);

        bg.addState(new int[] {}, normal);
        return bg;
    }

    /**
     * 得到可以改变状态的Selector,一般给setTextColor使用
     * @param normal
     * @param pressed
     * @param focused
     * @param unable
     * @param checked
     * @return
     */
    public ColorStateList getColorStateList(int normal, int pressed,
            int focused, int unable, int checked) {
        int[] colors = new int[] { pressed, focused, normal, focused, unable,
                checked, normal };
        int[][] states = new int[7][];
        states[0] = new int[] { android.R.attr.state_pressed,
                android.R.attr.state_enabled };
        states[1] = new int[] { android.R.attr.state_enabled,
                android.R.attr.state_focused };
        states[2] = new int[] { android.R.attr.state_enabled };
        states[3] = new int[] { android.R.attr.state_focused };
        states[4] = new int[] { android.R.attr.state_window_focused };
        states[5] = new int[] { android.R.attr.state_checked,
                android.R.attr.state_enabled };
        states[6] = new int[] {};
        ColorStateList colorList = new ColorStateList(states, colors);
        return colorList;
    }

    /**
     * 得到点击改变状态的Selector,一般给setTextColor使用
     * @param normal
     * @param pressed
     * @return
     */
    public ColorStateList getPressedSelectorColor(int normal, int pressed) {
        int[] colors = new int[] { pressed, normal, normal };
        int[][] states = new int[3][];
        states[0] = new int[] { android.R.attr.state_pressed,
                android.R.attr.state_enabled };
        states[1] = new int[] { android.R.attr.state_enabled };
        states[2] = new int[] {};
        ColorStateList colorList = new ColorStateList(states, colors);
        return colorList;
    }

    /**
     * 得到选中改变状态的Selector,一般给setTextColor使用
     * @param normal
     * @param pressed
     * @return
     */
    public ColorStateList getCheckedSelectorColor(int normal, int pressed) {
        int[] colors = new int[] { pressed, normal, normal };
        int[][] states = new int[3][];
        states[0] = new int[] { android.R.attr.state_checked,
                android.R.attr.state_enabled };
        states[1] = new int[] { android.R.attr.state_enabled };
        states[2] = new int[] {};
        ColorStateList colorList = new ColorStateList(states, colors);
        return colorList;
    }

    /**
     * 将多张图片合并生成一个Drawable
     * @param down
     * @param up
     * @return
     */
    public LayerDrawable getLayerDrawable(Drawable down, Drawable up) {
        Drawable[] layerList = { new InsetDrawable(down, 0, 0, 0, 0),
                new InsetDrawable(up, 0, 0, 0, 0) };

        return new LayerDrawable(layerList);
    }

    /**
     * 将多张图片生成一个连续播放的Drawable动画
     * @param time
     * @param dr
     * @return
     */
    public AnimationDrawable getAnimationDrawable(int time, Drawable... dr) {
        AnimationDrawable animationDrawable = new AnimationDrawable();
        for (int i = 0; i < dr.length; i++) {
            animationDrawable.addFrame(dr[i], time);
        }
         animationDrawable.setOneShot(false);
            animationDrawable.start();
        return animationDrawable;
    }

}

使用方法:

ColorStateList color = SelectorHepler.getInstance()
                .getPressedSelectorColor(Color.WHITE, Color.BLUE);
        StateListDrawable drawable = SelectorHepler.getInstance()
                .getPressedSelectorDrawable(this, R.drawable.ic_normal,
                        R.drawable.ic_pressed);
        tv.setTextColor(color);
        tv.setBackgroundDrawable(drawable);
时间: 2024-08-29 23:26:55

用代码快速构建selector的工具类SelectorHepler的相关文章

Android快速开发系列 常用工具类

1.日志工具类L.java package com.way.common.util; import android.util.Log; /** * Log统一管理类 * * @author way * */ public class L { public static boolean isDebug = true;// 是否需要打印bug,可以在application的onCreate函数里面初始化 private static final String TAG = "way"; //

JDBC-自定义数据库工具类(DBService)

写在前面的话: (1)使用JDBC,必须要使用对应的jar包,该笔记中使用jar包:mysql-connector-java-5.1 .6-bin.jar (2)使用连接池,一定要导入对象的数据库连接池包,该笔记中使用jar包:c3p0-0.9.1.2.jar 常用连接池:dbcp连接池.c3p0连接池.druid连接池 第一版:不用数据库连接池: 1 package cn; 2 3 import java.sql.Connection; 4 import java.sql.DriverMana

(转)Android中px与dip,sp与dip等的转换工具类

功能 通常在代码中设置组件或文字大小只能用px,通过这个工具类我们可以把dip(dp)或sp为单位的值转换为以px为单位的值而保证大小不变.方法中的参数请参考http://www.cnblogs.com/wader2011/archive/2011/11/28/2266669.html 代码 /** * Android大小单位转换工具类 *  * @author wader *  */public class DisplayUtil { /**  * 将px值转换为dip或dp值,保证尺寸大小不

Android Sqlite 工具类封装

鉴于经常使用 Sqlite 数据库做数据持久化处理,进行了一点封装,方便使用. 该封装类主要支持一下功能 支持多用户数据储存 支持 Sqlite数据库升级 支持传入 Sql 语句建表 支持 SQLiteDatabase 基本操作.比如:execSQL.rawQuery.insert等等 解决了数据库并发问题 先贴上封装类代码 /** * * @ClassName: DataBaseOpenHelper * @Description: 数据库工具类 * @author lhy * @date 20

获取Spring容器Bean对象工具类

在开发中,总是能碰到用注解注入不了Spring容器里面bean对象的问题.为了解决这个问题,我们需要一个工具类来直接获取Spring容器中的bean.因此就写了这个工具类,在此记录一下,方便后续查阅.废话不多说,直接上代码. 一.代码 package com.zxy.demo.spring; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext;

HttpClientUntils工具类的使用及注意事项(包括我改进的工具类和Controller端的注意事项【附 Json 工具类】)

HttpClient工具类(我改过): package com.taotao.httpclient; import java.io.IOException; import java.net.URI; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.apache.http.NameValuePair; import org.apache.http.client.entity.Ur

JDBC-select练习&amp;jdbc工具类

一.select练习 1.说明 练习: * 定义一个方法,查询emp表的数据将其封装为对象,然后装载集合,返回. 1. 定义Emp类 2. 定义方法 public List<Emp> findAll(){} 3. 实现方法 select * from emp; 2.建表 create table emp( id int primary key not null, ename varchar(50), job_id int, mgr int, joindate date, salary deci

《java并发编程实战》读书笔记4--基础构建模块,java中的同步容器类&amp;并发容器类&amp;同步工具类,消费者模式

上一章说道委托是创建线程安全类的一个最有效策略,只需让现有的线程安全的类管理所有的状态即可.那么这章便说的是怎么利用java平台类库的并发基础构建模块呢? 5.1 同步容器类 包括Vector和Hashtable,此外还包括在JDK1.2中添加的一些功能相似的类,这些同步的封装器类由Collections.synchronizedXxx等工厂方法创建的.这些类实现线程安全的方式是:将他们的状态封装起来,并对每个共有方法都进行同步,使得每次只能有一个线程能访问容器的状态. 关于java中的Vect

封装一个简单好用的打印Log的工具类And快速开发系列 10个常用工具类

快速开发系列 10个常用工具类 http://blog.csdn.net/lmj623565791/article/details/38965311 ------------------------------------------------------------------------------------------------ 不知众多Android开发者是否在程序开发的工程中也遇到过下面的问题: 0.如何在众多log中快速找到你设置打印的那行log? 1.是否还在不断的切换标签来