Selector

Selector中的各種狀態(tài)詳解

Android進階之路 - selector狀態(tài)選擇器

selector原理簡述過程

ViewStateUtil工具類

public class ViewStateUtil {
    /*//設(shè)置是否按壓狀態(tài),一般在true時設(shè)置該屬性抡诞,表示已按壓狀態(tài)穷蛹,默認(rèn)為false
    android:state_pressed
    //設(shè)置是否選中狀態(tài),true表示已選中蛙吏,false表示未選中
    android:state_selected
    //設(shè)置是否勾選狀態(tài)璧疗,主要用于CheckBox和RadioButton,true表示已被勾選,false表示未被勾選
    android:state_checked
    //設(shè)置勾選是否可用狀態(tài)系瓢,類似state_enabled,只是state_enabled會影響觸摸或點擊事件,state_checkable影響勾選事件
    android:state_checkable
    //設(shè)置是否獲得焦點狀態(tài)酬土,true表示獲得焦點,默認(rèn)為false,表示未獲得焦點
    android:state_focused
    //設(shè)置觸摸或點擊事件是否可用狀態(tài),一般只在false時設(shè)置該屬性专甩,表示不可用狀態(tài)
    android:state_enabled*/

    private static final int[] DRAWABLE_STATE_DEFAULT = new int[0];
    private static final int[] DRAWABLE_STATE_PRESSED = new int[]{android.R.attr.state_pressed};
    private static final int[] DRAWABLE_STATE_SELECTED = new int[]{android.R.attr.state_selected};
    private static final int[] DRAWABLE_STATE_CHECKED = new int[]{android.R.attr.state_checked};
    private static final int[] DRAWABLE_STATE_CHECKABLE = new int[]{android.R.attr.state_checkable};
    private static final int[] DRAWABLE_STATE_FOCUSED = new int[]{android.R.attr.state_focused};
    private static final int[] DRAWABLE_STATE_ENABLED = new int[]{android.R.attr.state_enabled};
    private static final int[] DRAWABLE_STATE_DISABLED = new int[]{-android.R.attr.state_enabled};

    private ViewStateUtil() {
    }

    public static Drawable createPressedSelector(Context context, int[] resource) {
        StateListDrawable drawable = new StateListDrawable();
        drawable.addState(DRAWABLE_STATE_PRESSED, AppCompatResources.getDrawable(context, resource[1]));
        drawable.addState(DRAWABLE_STATE_DEFAULT, AppCompatResources.getDrawable(context, resource[0]));
        drawable.setState(DRAWABLE_STATE_DEFAULT);
        return drawable;
    }

    public static Drawable createPressedSelector(Drawable drawableNormal, Drawable drawablePressed) {
        StateListDrawable drawable = new StateListDrawable();
        drawable.addState(DRAWABLE_STATE_PRESSED, drawablePressed);
        drawable.addState(DRAWABLE_STATE_DEFAULT, drawableNormal);
        drawable.setState(DRAWABLE_STATE_DEFAULT);
        return drawable;
    }

    public static Drawable createPressedSelector(int colorNormal, int colorPressed) {
        StateListDrawable drawable = new StateListDrawable();
        drawable.addState(DRAWABLE_STATE_PRESSED, new ColorDrawable(colorPressed));
        drawable.addState(DRAWABLE_STATE_DEFAULT, new ColorDrawable(colorNormal));
        drawable.setState(DRAWABLE_STATE_DEFAULT);
        return drawable;
    }

    public static Drawable createCornerPressedSelector(int colorNormal, int colorPressed, float corner) {
        StateListDrawable drawable = new StateListDrawable();
        GradientDrawable normal = new GradientDrawable();
        normal.setColor(colorNormal);
        normal.setCornerRadius(corner);
        GradientDrawable pressed = new GradientDrawable();
        pressed.setColor(colorPressed);
        pressed.setCornerRadius(corner);
        drawable.addState(DRAWABLE_STATE_PRESSED, pressed);
        drawable.addState(DRAWABLE_STATE_DEFAULT, normal);
        drawable.setState(DRAWABLE_STATE_DEFAULT);
        return drawable;
    }


    public static Drawable createSelectedSelector(Context context, int[] resource) {
        StateListDrawable drawable = new StateListDrawable();
        drawable.addState(DRAWABLE_STATE_SELECTED, AppCompatResources.getDrawable(context, resource[1]));
        drawable.addState(DRAWABLE_STATE_DEFAULT, AppCompatResources.getDrawable(context, resource[0]));
        drawable.setState(DRAWABLE_STATE_DEFAULT);
        return drawable;
    }

    public static Drawable createSelectedSelector(Drawable drawableNormal, Drawable drawableSelected) {
        StateListDrawable drawable = new StateListDrawable();
        drawable.addState(DRAWABLE_STATE_SELECTED, drawableSelected);
        drawable.addState(DRAWABLE_STATE_DEFAULT, drawableNormal);
        drawable.setState(DRAWABLE_STATE_DEFAULT);
        return drawable;
    }

    public static Drawable createSelectedSelector(int colorNormal, int colorSelected) {
        StateListDrawable drawable = new StateListDrawable();
        drawable.addState(DRAWABLE_STATE_SELECTED, new ColorDrawable(colorSelected));
        drawable.addState(DRAWABLE_STATE_DEFAULT, new ColorDrawable(colorNormal));
        drawable.setState(DRAWABLE_STATE_DEFAULT);
        return drawable;
    }

    public static Drawable createCornerSelectedSelector(int colorNormal, int colorSelected, float corner) {
        StateListDrawable drawable = new StateListDrawable();
        GradientDrawable normal = new GradientDrawable();
        normal.setColor(colorNormal);
        normal.setCornerRadius(corner);
        GradientDrawable pressed = new GradientDrawable();
        pressed.setColor(colorSelected);
        pressed.setCornerRadius(corner);
        drawable.addState(DRAWABLE_STATE_SELECTED, pressed);
        drawable.addState(DRAWABLE_STATE_DEFAULT, normal);
        drawable.setState(DRAWABLE_STATE_DEFAULT);
        return drawable;
    }


    public static Drawable createCheckedSelector(Context context, int[] resource) {
        StateListDrawable drawable = new StateListDrawable();
        drawable.addState(DRAWABLE_STATE_CHECKED, AppCompatResources.getDrawable(context, resource[1]));
        drawable.addState(DRAWABLE_STATE_DEFAULT, AppCompatResources.getDrawable(context, resource[0]));
        drawable.setState(DRAWABLE_STATE_DEFAULT);
        return drawable;
    }

    public static Drawable createDisabledSelector(Context context, int[] resource) {
        StateListDrawable drawable = new StateListDrawable();
        drawable.addState(DRAWABLE_STATE_DISABLED, AppCompatResources.getDrawable(context, resource[2]));
        drawable.addState(DRAWABLE_STATE_PRESSED, AppCompatResources.getDrawable(context, resource[1]));
        drawable.addState(DRAWABLE_STATE_DEFAULT, AppCompatResources.getDrawable(context, resource[0]));
        drawable.setState(DRAWABLE_STATE_DEFAULT);
        return drawable;
    }

    public static Drawable createDisabledSelector2(Context context, int[] resource) {
        StateListDrawable drawable = new StateListDrawable();
        drawable.addState(DRAWABLE_STATE_DISABLED, AppCompatResources.getDrawable(context, resource[1]));
        drawable.addState(DRAWABLE_STATE_DEFAULT, AppCompatResources.getDrawable(context, resource[0]));
        drawable.setState(DRAWABLE_STATE_DEFAULT);
        return drawable;
    }


    public static ColorStateList createSelectedColorStateList(int normalColor, int selectedColor) {
        return new ColorStateList(new int[][]{DRAWABLE_STATE_SELECTED, DRAWABLE_STATE_DEFAULT}, new int[]{selectedColor, normalColor});
    }

    public static ColorStateList createSelectedColorStateList(int normalColor, int selectedColor, int disableColor) {
        return new ColorStateList(new int[][]{DRAWABLE_STATE_DISABLED, DRAWABLE_STATE_SELECTED, DRAWABLE_STATE_DEFAULT}, new int[]{disableColor, selectedColor, normalColor});
    }

    public static ColorStateList createPressedColorStateList(int normalColor, int pressedColor) {
        return new ColorStateList(new int[][]{DRAWABLE_STATE_PRESSED, DRAWABLE_STATE_DEFAULT}, new int[]{pressedColor, normalColor});
    }

    public static ColorStateList createPressedColorStateList(int normalColor, int pressedColor, int disableColor) {
        return new ColorStateList(new int[][]{DRAWABLE_STATE_DISABLED, DRAWABLE_STATE_PRESSED, DRAWABLE_STATE_DEFAULT}, new int[]{disableColor, pressedColor, normalColor});
    }

    public static ColorStateList createCheckedColorStateList(int normalColor, int checkedColor) {
        return new ColorStateList(new int[][]{DRAWABLE_STATE_CHECKED, DRAWABLE_STATE_DEFAULT}, new int[]{checkedColor, normalColor});
    }

    public static ColorStateList createCheckedColorStateList(int normalColor, int checkedColor, int disableColor) {
        return new ColorStateList(new int[][]{DRAWABLE_STATE_DISABLED, DRAWABLE_STATE_CHECKED, DRAWABLE_STATE_DEFAULT}, new int[]{disableColor, checkedColor, normalColor});
    }

    public static ColorStateList createDisableColorStateList(int normalColor, int disableColor) {
        return new ColorStateList(new int[][]{DRAWABLE_STATE_DISABLED, DRAWABLE_STATE_DEFAULT}, new int[]{disableColor, normalColor});
    }
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市外傅,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌奥洼,老刑警劉巖估盘,帶你破解...
    沈念sama閱讀 206,968評論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡锦担,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,601評論 2 382
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人慎皱,你說我怎么就攤上這事〉乩妫” “怎么了歉甚?”我有些...
    開封第一講書人閱讀 153,220評論 0 344
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長谴餐,這世上最難降的妖魔是什么理肺? 我笑而不...
    開封第一講書人閱讀 55,416評論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上摩桶,老公的妹妹穿的比我還像新娘转晰。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 64,425評論 5 374
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上荷憋,一...
    開封第一講書人閱讀 49,144評論 1 285
  • 那天荡碾,我揣著相機與錄音铐尚,去河邊找鬼玫膀。 笑死碉就,一個胖子當(dāng)著我的面吹牛碉熄,可吹牛的內(nèi)容都是我干的琼梆。 我是一名探鬼主播,決...
    沈念sama閱讀 38,432評論 3 401
  • 文/蒼蘭香墨 我猛地睜開眼羞海,長吁一口氣:“原來是場噩夢啊……” “哼癌幕!你這毒婦竟也來了勺远?” 一聲冷哼從身側(cè)響起和簸,我...
    開封第一講書人閱讀 37,088評論 0 261
  • 序言:老撾萬榮一對情侶失蹤浩村,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,586評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,028評論 2 325
  • 正文 我和宋清朗相戀三年古胆,在試婚紗的時候發(fā)現(xiàn)自己被綠了棺牧。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片颊乘。...
    茶點故事閱讀 38,137評論 1 334
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖颓哮,靈堂內(nèi)的尸體忽然破棺而出冕茅,到底是詐尸還是另有隱情姨伤,我是刑警寧澤,帶...
    沈念sama閱讀 33,783評論 4 324
  • 正文 年R本政府宣布鲤桥,位于F島的核電站剪芍,受9級特大地震影響峡继,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜儡循,卻給世界環(huán)境...
    茶點故事閱讀 39,343評論 3 307
  • 文/蒙蒙 一舶吗、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧择膝,春花似錦誓琼、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,333評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽呵扛。三九已至,卻和暖如春筐带,著一層夾襖步出監(jiān)牢的瞬間今穿,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,559評論 1 262
  • 我被黑心中介騙來泰國打工伦籍, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留蓝晒,地道東北人。 一個月前我還...
    沈念sama閱讀 45,595評論 2 355
  • 正文 我出身青樓帖鸦,卻偏偏與公主長得像芝薇,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子作儿,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 42,901評論 2 345

推薦閱讀更多精彩內(nèi)容