Merge "Update local state when creating LayerDrawable from constant state" into mnc-dev

This commit is contained in:
Alan Viverette
2015-06-24 23:15:14 +00:00
committed by Android (Google) Code Review
5 changed files with 28 additions and 20 deletions

View File

@@ -575,7 +575,7 @@ public class AnimatedStateListDrawable extends StateListDrawable {
} }
} }
private void mutate() { void mutate() {
mTransitions = mTransitions.clone(); mTransitions = mTransitions.clone();
mStateIds = mStateIds.clone(); mStateIds = mStateIds.clone();
} }

View File

@@ -730,7 +730,7 @@ public class DrawableContainer extends Drawable implements Drawable.Callback {
if (origDf != null) { if (origDf != null) {
mDrawableFutures = origDf.clone(); mDrawableFutures = origDf.clone();
} else { } else {
mDrawableFutures = new SparseArray<ConstantStateFuture>(mNumChildren); mDrawableFutures = new SparseArray<>(mNumChildren);
} }
// Create futures for drawables with constant states. If a // Create futures for drawables with constant states. If a
@@ -823,6 +823,9 @@ public class DrawableContainer extends Drawable implements Drawable.Callback {
final Drawable prepared = mDrawableFutures.valueAt(keyIndex).get(this); final Drawable prepared = mDrawableFutures.valueAt(keyIndex).get(this);
mDrawables[index] = prepared; mDrawables[index] = prepared;
mDrawableFutures.removeAt(keyIndex); mDrawableFutures.removeAt(keyIndex);
if (mDrawableFutures.size() == 0) {
mDrawableFutures = null;
}
return prepared; return prepared;
} }
} }

View File

@@ -133,6 +133,7 @@ public class LayerDrawable extends Drawable implements Drawable.Callback {
mLayerState.mChildren = r; mLayerState.mChildren = r;
ensurePadding(); ensurePadding();
refreshPadding();
} }
LayerDrawable() { LayerDrawable() {
@@ -143,6 +144,7 @@ public class LayerDrawable extends Drawable implements Drawable.Callback {
mLayerState = createConstantState(state, res); mLayerState = createConstantState(state, res);
if (mLayerState.mNum > 0) { if (mLayerState.mNum > 0) {
ensurePadding(); ensurePadding();
refreshPadding();
} }
} }
@@ -162,6 +164,7 @@ public class LayerDrawable extends Drawable implements Drawable.Callback {
inflateLayers(r, parser, attrs, theme); inflateLayers(r, parser, attrs, theme);
ensurePadding(); ensurePadding();
refreshPadding();
} }
/** /**
@@ -431,6 +434,7 @@ public class LayerDrawable extends Drawable implements Drawable.Callback {
final ChildDrawable layer = createLayer(dr); final ChildDrawable layer = createLayer(dr);
final int index = addLayer(layer); final int index = addLayer(layer);
ensurePadding(); ensurePadding();
refreshChildPadding(index, layer);
return index; return index;
} }
@@ -568,6 +572,8 @@ public class LayerDrawable extends Drawable implements Drawable.Callback {
childDrawable.mDrawable = drawable; childDrawable.mDrawable = drawable;
mLayerState.invalidateCache(); mLayerState.invalidateCache();
refreshChildPadding(index, childDrawable);
} }
/** /**
@@ -1623,6 +1629,14 @@ public class LayerDrawable extends Drawable implements Drawable.Callback {
mPaddingB = new int[N]; mPaddingB = new int[N];
} }
void refreshPadding() {
final int N = mLayerState.mNum;
final ChildDrawable[] array = mLayerState.mChildren;
for (int i = 0; i < N; i++) {
refreshChildPadding(i, array[i]);
}
}
@Override @Override
public ConstantState getConstantState() { public ConstantState getConstantState() {
if (mLayerState.canConstantState()) { if (mLayerState.canConstantState()) {

View File

@@ -198,6 +198,7 @@ public class RippleDrawable extends LayerDrawable {
setColor(color); setColor(color);
ensurePadding(); ensurePadding();
refreshPadding();
updateLocalState(); updateLocalState();
} }
@@ -1013,6 +1014,7 @@ public class RippleDrawable extends LayerDrawable {
if (mState.mNum > 0) { if (mState.mNum > 0) {
ensurePadding(); ensurePadding();
refreshPadding();
} }
if (res != null) { if (res != null) {

View File

@@ -59,22 +59,10 @@ import android.util.StateSet;
* @attr ref android.R.styleable#DrawableStates_state_pressed * @attr ref android.R.styleable#DrawableStates_state_pressed
*/ */
public class StateListDrawable extends DrawableContainer { public class StateListDrawable extends DrawableContainer {
private static final String TAG = StateListDrawable.class.getSimpleName(); private static final String TAG = "StateListDrawable";
private static final boolean DEBUG = false; private static final boolean DEBUG = false;
/**
* To be proper, we should have a getter for dither (and alpha, etc.)
* so that proxy classes like this can save/restore their delegates'
* values, but we don't have getters. Since we do have setters
* (e.g. setDither), which this proxy forwards on, we have to have some
* default/initial setting.
*
* The initial setting for dither is now true, since it almost always seems
* to improve the quality at negligible cost.
*/
private static final boolean DEFAULT_DITHER = true;
private StateListState mStateListState; private StateListState mStateListState;
private boolean mMutated; private boolean mMutated;
@@ -104,16 +92,16 @@ public class StateListDrawable extends DrawableContainer {
@Override @Override
protected boolean onStateChange(int[] stateSet) { protected boolean onStateChange(int[] stateSet) {
final boolean changed = super.onStateChange(stateSet);
int idx = mStateListState.indexOfStateSet(stateSet); int idx = mStateListState.indexOfStateSet(stateSet);
if (DEBUG) android.util.Log.i(TAG, "onStateChange " + this + " states " if (DEBUG) android.util.Log.i(TAG, "onStateChange " + this + " states "
+ Arrays.toString(stateSet) + " found " + idx); + Arrays.toString(stateSet) + " found " + idx);
if (idx < 0) { if (idx < 0) {
idx = mStateListState.indexOfStateSet(StateSet.WILD_CARD); idx = mStateListState.indexOfStateSet(StateSet.WILD_CARD);
} }
if (selectDrawable(idx)) {
return true; return selectDrawable(idx) || changed;
}
return super.onStateChange(stateSet);
} }
@Override @Override
@@ -326,13 +314,14 @@ public class StateListDrawable extends DrawableContainer {
} }
} }
private void mutate() { void mutate() {
mThemeAttrs = mThemeAttrs != null ? mThemeAttrs.clone() : null; mThemeAttrs = mThemeAttrs != null ? mThemeAttrs.clone() : null;
final int[][] stateSets = new int[mStateSets.length][]; final int[][] stateSets = new int[mStateSets.length][];
for (int i = mStateSets.length - 1; i >= 0; i--) { for (int i = mStateSets.length - 1; i >= 0; i--) {
stateSets[i] = mStateSets[i] != null ? mStateSets[i].clone() : null; stateSets[i] = mStateSets[i] != null ? mStateSets[i].clone() : null;
} }
mStateSets = stateSets;
} }
int addStateSet(int[] stateSet, Drawable drawable) { int addStateSet(int[] stateSet, Drawable drawable) {