@act262
2017-06-03T11:38:22.000000Z
字数 1198
阅读 1432
Android_Drawable
在selector或者StateListDrawable的从上到下按顺序优先匹配
StateListDrawable.java
// 添加对应状态的Drawable时插入Container数组的最后public void addState(int[] stateSet, Drawable drawable) {if (drawable != null) {mStateListState.addStateSet(stateSet, drawable);// in case the new state matches our current state...onStateChange(getState());}}@Override// 主动或者被动触发调用protected boolean onStateChange(int[] stateSet) {final boolean changed = super.onStateChange(stateSet);int idx = mStateListState.indexOfStateSet(stateSet);// 都没有匹配到,则使用兜底方案if (idx < 0) {idx = mStateListState.indexOfStateSet(StateSet.WILD_CARD);}return selectDrawable(idx) || changed;}
StateListState
// 获取指定状态的索引int indexOfStateSet(int[] stateSet) {final int[][] stateSets = mStateSets;final int N = getChildCount();for (int i = 0; i < N; i++) {if (StateSet.stateSetMatches(stateSets[i], stateSet)) {return i;}}return -1;}
StateSet.java
/*** A state specification that will be matched by all StateSets.*/public static final int[] WILD_CARD = new int[0];/*** A state set that does not contain any valid states.*/public static final int[] NOTHING = new int[] { 0 };public static boolean stateSetMatches(int[] stateSpec, int[] stateSet) {// 这里优先匹配了if (stateSet == null) {return (stateSpec == null || isWildCard(stateSpec));}// 匹配指定的顺序,从上到下优先匹配// ...}
