Merge "Support theme attributes in StateListDrawable <item> element" into lmp-mr1-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
48c8cd06aa
@@ -4804,6 +4804,13 @@
|
|||||||
<attr name="autoMirrored"/>
|
<attr name="autoMirrored"/>
|
||||||
</declare-styleable>
|
</declare-styleable>
|
||||||
|
|
||||||
|
<!-- Represents a single state inside a StateListDrawable. -->
|
||||||
|
<declare-styleable name="StateListDrawableItem">
|
||||||
|
<!-- Reference to a drawable resource to use for the state. If not
|
||||||
|
given, the drawable must be defined by the first child tag. -->
|
||||||
|
<attr name="drawable" />
|
||||||
|
</declare-styleable>
|
||||||
|
|
||||||
<!-- Transition used to animate between states with keyframe IDs. -->
|
<!-- Transition used to animate between states with keyframe IDs. -->
|
||||||
<declare-styleable name="AnimatedStateListDrawableItem">
|
<declare-styleable name="AnimatedStateListDrawableItem">
|
||||||
<!-- Reference to a drawable resource to use for the frame. If not
|
<!-- Reference to a drawable resource to use for the frame. If not
|
||||||
|
|||||||
@@ -429,43 +429,31 @@ public class AnimatedStateListDrawable extends StateListDrawable {
|
|||||||
private int parseTransition(@NonNull Resources r, @NonNull XmlPullParser parser,
|
private int parseTransition(@NonNull Resources r, @NonNull XmlPullParser parser,
|
||||||
@NonNull AttributeSet attrs, @Nullable Theme theme)
|
@NonNull AttributeSet attrs, @Nullable Theme theme)
|
||||||
throws XmlPullParserException, IOException {
|
throws XmlPullParserException, IOException {
|
||||||
int drawableRes = 0;
|
// This allows state list drawable item elements to be themed at
|
||||||
int fromId = 0;
|
// inflation time but does NOT make them work for Zygote preload.
|
||||||
int toId = 0;
|
final TypedArray a = obtainAttributes(r, theme, attrs,
|
||||||
boolean reversible = false;
|
R.styleable.AnimatedStateListDrawableTransition);
|
||||||
|
final int fromId = a.getResourceId(
|
||||||
|
R.styleable.AnimatedStateListDrawableTransition_fromId, 0);
|
||||||
|
final int toId = a.getResourceId(
|
||||||
|
R.styleable.AnimatedStateListDrawableTransition_toId, 0);
|
||||||
|
final boolean reversible = a.getBoolean(
|
||||||
|
R.styleable.AnimatedStateListDrawableTransition_reversible, false);
|
||||||
|
Drawable dr = a.getDrawable(
|
||||||
|
R.styleable.AnimatedStateListDrawableTransition_drawable);
|
||||||
|
a.recycle();
|
||||||
|
|
||||||
final int numAttrs = attrs.getAttributeCount();
|
// Loading child elements modifies the state of the AttributeSet's
|
||||||
for (int i = 0; i < numAttrs; i++) {
|
// underlying parser, so it needs to happen after obtaining
|
||||||
final int stateResId = attrs.getAttributeNameResource(i);
|
// attributes and extracting states.
|
||||||
switch (stateResId) {
|
if (dr == null) {
|
||||||
case 0:
|
|
||||||
break;
|
|
||||||
case R.attr.fromId:
|
|
||||||
fromId = attrs.getAttributeResourceValue(i, 0);
|
|
||||||
break;
|
|
||||||
case R.attr.toId:
|
|
||||||
toId = attrs.getAttributeResourceValue(i, 0);
|
|
||||||
break;
|
|
||||||
case R.attr.drawable:
|
|
||||||
drawableRes = attrs.getAttributeResourceValue(i, 0);
|
|
||||||
break;
|
|
||||||
case R.attr.reversible:
|
|
||||||
reversible = attrs.getAttributeBooleanValue(i, false);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
final Drawable dr;
|
|
||||||
if (drawableRes != 0) {
|
|
||||||
dr = r.getDrawable(drawableRes, theme);
|
|
||||||
} else {
|
|
||||||
int type;
|
int type;
|
||||||
while ((type = parser.next()) == XmlPullParser.TEXT) {
|
while ((type = parser.next()) == XmlPullParser.TEXT) {
|
||||||
}
|
}
|
||||||
if (type != XmlPullParser.START_TAG) {
|
if (type != XmlPullParser.START_TAG) {
|
||||||
throw new XmlPullParserException(
|
throw new XmlPullParserException(
|
||||||
parser.getPositionDescription()
|
parser.getPositionDescription()
|
||||||
+ ": <item> tag requires a 'drawable' attribute or "
|
+ ": <transition> tag requires a 'drawable' attribute or "
|
||||||
+ "child tag defining a drawable");
|
+ "child tag defining a drawable");
|
||||||
}
|
}
|
||||||
dr = Drawable.createFromXmlInner(r, parser, attrs, theme);
|
dr = Drawable.createFromXmlInner(r, parser, attrs, theme);
|
||||||
@@ -477,34 +465,20 @@ public class AnimatedStateListDrawable extends StateListDrawable {
|
|||||||
private int parseItem(@NonNull Resources r, @NonNull XmlPullParser parser,
|
private int parseItem(@NonNull Resources r, @NonNull XmlPullParser parser,
|
||||||
@NonNull AttributeSet attrs, @Nullable Theme theme)
|
@NonNull AttributeSet attrs, @Nullable Theme theme)
|
||||||
throws XmlPullParserException, IOException {
|
throws XmlPullParserException, IOException {
|
||||||
int drawableRes = 0;
|
// This allows state list drawable item elements to be themed at
|
||||||
int keyframeId = 0;
|
// inflation time but does NOT make them work for Zygote preload.
|
||||||
|
final TypedArray a = obtainAttributes(r, theme, attrs,
|
||||||
|
R.styleable.AnimatedStateListDrawableItem);
|
||||||
|
final int keyframeId = a.getResourceId(R.styleable.AnimatedStateListDrawableItem_id, 0);
|
||||||
|
Drawable dr = a.getDrawable(R.styleable.AnimatedStateListDrawableItem_drawable);
|
||||||
|
a.recycle();
|
||||||
|
|
||||||
int j = 0;
|
final int[] states = extractStateSet(attrs);
|
||||||
final int numAttrs = attrs.getAttributeCount();
|
|
||||||
int[] states = new int[numAttrs];
|
|
||||||
for (int i = 0; i < numAttrs; i++) {
|
|
||||||
final int stateResId = attrs.getAttributeNameResource(i);
|
|
||||||
switch (stateResId) {
|
|
||||||
case 0:
|
|
||||||
break;
|
|
||||||
case R.attr.id:
|
|
||||||
keyframeId = attrs.getAttributeResourceValue(i, 0);
|
|
||||||
break;
|
|
||||||
case R.attr.drawable:
|
|
||||||
drawableRes = attrs.getAttributeResourceValue(i, 0);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
final boolean hasState = attrs.getAttributeBooleanValue(i, false);
|
|
||||||
states[j++] = hasState ? stateResId : -stateResId;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
states = StateSet.trimStateSet(states, j);
|
|
||||||
|
|
||||||
final Drawable dr;
|
// Loading child elements modifies the state of the AttributeSet's
|
||||||
if (drawableRes != 0) {
|
// underlying parser, so it needs to happen after obtaining
|
||||||
dr = r.getDrawable(drawableRes, theme);
|
// attributes and extracting states.
|
||||||
} else {
|
if (dr == null) {
|
||||||
int type;
|
int type;
|
||||||
while ((type = parser.next()) == XmlPullParser.TEXT) {
|
while ((type = parser.next()) == XmlPullParser.TEXT) {
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -173,29 +173,19 @@ public class StateListDrawable extends DrawableContainer {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
int drawableRes = 0;
|
// This allows state list drawable item elements to be themed at
|
||||||
|
// inflation time but does NOT make them work for Zygote preload.
|
||||||
|
final TypedArray a = obtainAttributes(r, theme, attrs,
|
||||||
|
R.styleable.StateListDrawableItem);
|
||||||
|
Drawable dr = a.getDrawable(R.styleable.StateListDrawableItem_drawable);
|
||||||
|
a.recycle();
|
||||||
|
|
||||||
int i;
|
final int[] states = extractStateSet(attrs);
|
||||||
int j = 0;
|
|
||||||
final int numAttrs = attrs.getAttributeCount();
|
|
||||||
int[] states = new int[numAttrs];
|
|
||||||
for (i = 0; i < numAttrs; i++) {
|
|
||||||
final int stateResId = attrs.getAttributeNameResource(i);
|
|
||||||
if (stateResId == 0) break;
|
|
||||||
if (stateResId == R.attr.drawable) {
|
|
||||||
drawableRes = attrs.getAttributeResourceValue(i, 0);
|
|
||||||
} else {
|
|
||||||
states[j++] = attrs.getAttributeBooleanValue(i, false)
|
|
||||||
? stateResId
|
|
||||||
: -stateResId;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
states = StateSet.trimStateSet(states, j);
|
|
||||||
|
|
||||||
final Drawable dr;
|
// Loading child elements modifies the state of the AttributeSet's
|
||||||
if (drawableRes != 0) {
|
// underlying parser, so it needs to happen after obtaining
|
||||||
dr = r.getDrawable(drawableRes, theme);
|
// attributes and extracting states.
|
||||||
} else {
|
if (dr == null) {
|
||||||
while ((type = parser.next()) == XmlPullParser.TEXT) {
|
while ((type = parser.next()) == XmlPullParser.TEXT) {
|
||||||
}
|
}
|
||||||
if (type != XmlPullParser.START_TAG) {
|
if (type != XmlPullParser.START_TAG) {
|
||||||
@@ -211,6 +201,35 @@ public class StateListDrawable extends DrawableContainer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extracts state_ attributes from an attribute set.
|
||||||
|
*
|
||||||
|
* @param attrs The attribute set.
|
||||||
|
* @return An array of state_ attributes.
|
||||||
|
*/
|
||||||
|
int[] extractStateSet(AttributeSet attrs) {
|
||||||
|
int j = 0;
|
||||||
|
final int numAttrs = attrs.getAttributeCount();
|
||||||
|
int[] states = new int[numAttrs];
|
||||||
|
for (int i = 0; i < numAttrs; i++) {
|
||||||
|
final int stateResId = attrs.getAttributeNameResource(i);
|
||||||
|
switch (stateResId) {
|
||||||
|
case 0:
|
||||||
|
break;
|
||||||
|
case R.attr.drawable:
|
||||||
|
case R.attr.id:
|
||||||
|
// Ignore attributes from StateListDrawableItem and
|
||||||
|
// AnimatedStateListDrawableItem.
|
||||||
|
continue;
|
||||||
|
default:
|
||||||
|
states[j++] = attrs.getAttributeBooleanValue(i, false)
|
||||||
|
? stateResId : -stateResId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
states = StateSet.trimStateSet(states, j);
|
||||||
|
return states;
|
||||||
|
}
|
||||||
|
|
||||||
StateListState getStateListState() {
|
StateListState getStateListState() {
|
||||||
return mStateListState;
|
return mStateListState;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user