diff options
author | Alan Viverette <alanv@google.com> | 2014-02-22 16:52:46 -0800 |
---|---|---|
committer | Alan Viverette <alanv@google.com> | 2014-02-22 16:52:46 -0800 |
commit | cc3f33853b8ea5ce56ebfb14fdc3c000a45e0d78 (patch) | |
tree | 8fe03ef221d6ec94050734c761b83033498d8953 /core/java | |
parent | cba616d85f16e956928fda5e55bd202a767306c4 (diff) | |
download | frameworks_base-cc3f33853b8ea5ce56ebfb14fdc3c000a45e0d78.zip frameworks_base-cc3f33853b8ea5ce56ebfb14fdc3c000a45e0d78.tar.gz frameworks_base-cc3f33853b8ea5ce56ebfb14fdc3c000a45e0d78.tar.bz2 |
Allow alpha modulation in ColorStateList items
This makes it much easier to define a disabled state color as an alpha
modulation of some base color.
Change-Id: Ie2ecf40b1f6560fcd8eda208d780616dddfc0d08
Diffstat (limited to 'core/java')
-rw-r--r-- | core/java/android/content/res/ColorStateList.java | 40 |
1 files changed, 30 insertions, 10 deletions
diff --git a/core/java/android/content/res/ColorStateList.java b/core/java/android/content/res/ColorStateList.java index 431226a..2893522 100644 --- a/core/java/android/content/res/ColorStateList.java +++ b/core/java/android/content/res/ColorStateList.java @@ -17,12 +17,14 @@ package android.content.res; import android.graphics.Color; + import com.android.internal.util.ArrayUtils; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import android.util.AttributeSet; +import android.util.MathUtils; import android.util.SparseArray; import android.util.StateSet; import android.util.Xml; @@ -172,7 +174,7 @@ public class ColorStateList implements Parcelable { * Fill in this object based on the contents of an XML "selector" element. */ private void inflate(Resources r, XmlPullParser parser, AttributeSet attrs) - throws XmlPullParserException, IOException { + throws XmlPullParserException, IOException { int type; @@ -195,6 +197,8 @@ public class ColorStateList implements Parcelable { continue; } + int alphaRes = 0; + float alpha = 1.0f; int colorRes = 0; int color = 0xffff0000; boolean haveColor = false; @@ -206,17 +210,20 @@ public class ColorStateList implements Parcelable { for (i = 0; i < numAttrs; i++) { final int stateResId = attrs.getAttributeNameResource(i); if (stateResId == 0) break; - if (stateResId == com.android.internal.R.attr.color) { + 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; + ? stateResId : -stateResId; } } stateSpec = StateSet.trimStateSet(stateSpec, j); @@ -229,10 +236,18 @@ public class ColorStateList implements Parcelable { + ": <item> tag requires a 'android:color' attribute."); } + if (alphaRes != 0) { + alpha = r.getFraction(alphaRes, 1, 1); + } + + // 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; } - + if (listSize + 1 >= listAllocated) { listAllocated = ArrayUtils.idealIntArraySize(listSize + 1); @@ -300,6 +315,7 @@ public class ColorStateList implements Parcelable { return mDefaultColor; } + @Override public String toString() { return "ColorStateList{" + "mStateSpecs=" + Arrays.deepToString(mStateSpecs) + @@ -307,14 +323,16 @@ public class ColorStateList implements Parcelable { "mDefaultColor=" + mDefaultColor + '}'; } + @Override public int describeContents() { return 0; } + @Override public void writeToParcel(Parcel dest, int flags) { final int N = mStateSpecs.length; dest.writeInt(N); - for (int i=0; i<N; i++) { + for (int i = 0; i < N; i++) { dest.writeIntArray(mStateSpecs[i]); } dest.writeIntArray(mColors); @@ -322,17 +340,19 @@ public class ColorStateList implements Parcelable { public static final Parcelable.Creator<ColorStateList> CREATOR = new Parcelable.Creator<ColorStateList>() { + @Override public ColorStateList[] newArray(int size) { return new ColorStateList[size]; } + @Override public ColorStateList createFromParcel(Parcel source) { final int N = source.readInt(); - int[][] stateSpecs = new int[N][]; - for (int i=0; i<N; i++) { + final int[][] stateSpecs = new int[N][]; + for (int i = 0; i < N; i++) { stateSpecs[i] = source.createIntArray(); } - int[] colors = source.createIntArray(); + final int[] colors = source.createIntArray(); return new ColorStateList(stateSpecs, colors); } }; |