Selector中的各种状态详解
Android进阶之路 - selector状态选择器
selector原理简述过程
ViewStateUtil工具类
public class ViewStateUtil {
/*//设置是否按压状态,一般在true时设置该属性,表示已按压状态,默认为false
android:state_pressed
//设置是否选中状态,true表示已选中,false表示未选中
android:state_selected
//设置是否勾选状态,主要用于CheckBox和RadioButton,true表示已被勾选,false表示未被勾选
android:state_checked
//设置勾选是否可用状态,类似state_enabled,只是state_enabled会影响触摸或点击事件,state_checkable影响勾选事件
android:state_checkable
//设置是否获得焦点状态,true表示获得焦点,默认为false,表示未获得焦点
android:state_focused
//设置触摸或点击事件是否可用状态,一般只在false时设置该属性,表示不可用状态
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});
}
}