Selector

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

Android進(jìn)階之路 - 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)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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