Methods Summary |
---|
public static android.content.res.ColorStateList | addFirstIfMissing(android.content.res.ColorStateList colorStateList, int state, int color)If the color state list does not already have an entry matching the
specified state, prepends a state set and color pair to a color state
list.
This is a workaround used in TimePicker and DatePicker until we can
add support for theme attributes in ColorStateList.
final int[][] inputStates = colorStateList.getStates();
for (int i = 0; i < inputStates.length; i++) {
final int[] inputState = inputStates[i];
for (int j = 0; j < inputState.length; j++) {
if (inputState[j] == state) {
return colorStateList;
}
}
}
final int[][] outputStates = new int[inputStates.length + 1][];
System.arraycopy(inputStates, 0, outputStates, 1, inputStates.length);
outputStates[0] = new int[] { state };
final int[] inputColors = colorStateList.getColors();
final int[] outputColors = new int[inputColors.length + 1];
System.arraycopy(inputColors, 0, outputColors, 1, inputColors.length);
outputColors[0] = color;
return new ColorStateList(outputStates, outputColors);
|
public static android.content.res.ColorStateList | createFromXml(Resources r, org.xmlpull.v1.XmlPullParser parser)Create a ColorStateList from an XML document, given a set of {@link Resources}.
final AttributeSet attrs = Xml.asAttributeSet(parser);
int type;
while ((type=parser.next()) != XmlPullParser.START_TAG
&& type != XmlPullParser.END_DOCUMENT) {
}
if (type != XmlPullParser.START_TAG) {
throw new XmlPullParserException("No start tag found");
}
return createFromXmlInner(r, parser, attrs);
|
private static android.content.res.ColorStateList | createFromXmlInner(Resources r, org.xmlpull.v1.XmlPullParser parser, android.util.AttributeSet attrs)Create from inside an XML document. Called on a parser positioned at a
tag in an XML document, tries to create a ColorStateList from that tag.
final ColorStateList colorStateList;
final String name = parser.getName();
if (name.equals("selector")) {
colorStateList = new ColorStateList();
} else {
throw new XmlPullParserException(
parser.getPositionDescription() + ": invalid drawable tag " + name);
}
colorStateList.inflate(r, parser, attrs);
return colorStateList;
|
public int | describeContents()
return 0;
|
public int | getColorForState(int[] stateSet, int defaultColor)Return the color associated with the given set of {@link android.view.View} states.
final int setLength = mStateSpecs.length;
for (int i = 0; i < setLength; i++) {
int[] stateSpec = mStateSpecs[i];
if (StateSet.stateSetMatches(stateSpec, stateSet)) {
return mColors[i];
}
}
return defaultColor;
|
public int[] | getColors()Return the colors in this {@link ColorStateList}.
return mColors;
|
public int | getDefaultColor()Return the default color in this {@link ColorStateList}.
return mDefaultColor;
|
public int[][] | getStates()Return the states in this {@link ColorStateList}.
return mStateSpecs;
|
private void | inflate(Resources r, org.xmlpull.v1.XmlPullParser parser, android.util.AttributeSet attrs)Fill in this object based on the contents of an XML "selector" element.
int type;
final int innerDepth = parser.getDepth()+1;
int depth;
int[][] stateSpecList = ArrayUtils.newUnpaddedArray(int[].class, 20);
int[] colorList = new int[stateSpecList.length];
int listSize = 0;
while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
&& ((depth=parser.getDepth()) >= innerDepth
|| type != XmlPullParser.END_TAG)) {
if (type != XmlPullParser.START_TAG) {
continue;
}
if (depth > innerDepth || !parser.getName().equals("item")) {
continue;
}
int alphaRes = 0;
float alpha = 1.0f;
int colorRes = 0;
int color = 0xffff0000;
boolean haveColor = false;
int i;
int j = 0;
final int numAttrs = attrs.getAttributeCount();
int[] stateSpec = new int[numAttrs];
for (i = 0; i < numAttrs; i++) {
final int stateResId = attrs.getAttributeNameResource(i);
if (stateResId == 0) break;
if (stateResId == com.android.internal.R.attr.alpha) {
alphaRes = attrs.getAttributeResourceValue(i, 0);
if (alphaRes == 0) {
alpha = attrs.getAttributeFloatValue(i, 1.0f);
}
} else if (stateResId == com.android.internal.R.attr.color) {
colorRes = attrs.getAttributeResourceValue(i, 0);
if (colorRes == 0) {
color = attrs.getAttributeIntValue(i, color);
haveColor = true;
}
} else {
stateSpec[j++] = attrs.getAttributeBooleanValue(i, false)
? stateResId : -stateResId;
}
}
stateSpec = StateSet.trimStateSet(stateSpec, j);
if (colorRes != 0) {
color = r.getColor(colorRes);
} else if (!haveColor) {
throw new XmlPullParserException(
parser.getPositionDescription()
+ ": <item> tag requires a 'android:color' attribute.");
}
if (alphaRes != 0) {
alpha = r.getFloat(alphaRes);
}
// Apply alpha modulation.
final int alphaMod = MathUtils.constrain((int) (Color.alpha(color) * alpha), 0, 255);
color = (color & 0xFFFFFF) | (alphaMod << 24);
if (listSize == 0 || stateSpec.length == 0) {
mDefaultColor = color;
}
colorList = GrowingArrayUtils.append(colorList, listSize, color);
stateSpecList = GrowingArrayUtils.append(stateSpecList, listSize, stateSpec);
listSize++;
}
mColors = new int[listSize];
mStateSpecs = new int[listSize][];
System.arraycopy(colorList, 0, mColors, 0, listSize);
System.arraycopy(stateSpecList, 0, mStateSpecs, 0, listSize);
|
public boolean | isOpaque()Indicates whether this color state list is opaque, which means that every
color returned from {@link #getColorForState(int[], int)} has an alpha
value of 255.
final int n = mColors.length;
for (int i = 0; i < n; i++) {
if (Color.alpha(mColors[i]) != 0xFF) {
return false;
}
}
return true;
|
public boolean | isStateful()Indicates whether this color state list contains more than one state spec
and will change color based on state.
return mStateSpecs.length > 1;
|
public java.lang.String | toString()
return "ColorStateList{" +
"mStateSpecs=" + Arrays.deepToString(mStateSpecs) +
"mColors=" + Arrays.toString(mColors) +
"mDefaultColor=" + mDefaultColor + '}";
|
public static android.content.res.ColorStateList | valueOf(int color)Creates or retrieves a ColorStateList that always returns a single color.
// TODO: should we collect these eventually?
synchronized (sCache) {
final WeakReference<ColorStateList> ref = sCache.get(color);
ColorStateList csl = ref != null ? ref.get() : null;
if (csl != null) {
return csl;
}
csl = new ColorStateList(EMPTY, new int[] { color });
sCache.put(color, new WeakReference<ColorStateList>(csl));
return csl;
}
|
public android.content.res.ColorStateList | withAlpha(int alpha)Creates a new ColorStateList that has the same states and
colors as this one but where each color has the specified alpha value
(0-255).
final int[] colors = new int[mColors.length];
final int len = colors.length;
for (int i = 0; i < len; i++) {
colors[i] = (mColors[i] & 0xFFFFFF) | (alpha << 24);
}
return new ColorStateList(mStateSpecs, colors);
|
public void | writeToParcel(android.os.Parcel dest, int flags)
final int N = mStateSpecs.length;
dest.writeInt(N);
for (int i = 0; i < N; i++) {
dest.writeIntArray(mStateSpecs[i]);
}
dest.writeIntArray(mColors);
|