List of usage examples for android.util AttributeSet getAttributeNameResource
public int getAttributeNameResource(int index);
From source file:com.bilibili.magicasakura.utils.ColorStateListUtils.java
protected static int[] extractStateSet(AttributeSet attrs) { int j = 0;/*from ww w .j a v a2 s . co m*/ 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 android.R.attr.color: case android.R.attr.alpha: // Ignore attributes from StateListDrawableItem and // AnimatedStateListDrawableItem. continue; default: states[j++] = attrs.getAttributeBooleanValue(i, false) ? stateResId : -stateResId; } } states = StateSet.trimStateSet(states, j); return states; }
From source file:me.henrytao.mdcore.core.MdCompat.java
/** * A temporary fix for android.content.res.ColorStateList.inflate */// w w w. j ava2s .co m public static ColorStateList getColorStateList(Context context, int resId) throws IOException, XmlPullParserException { XmlResourceParser parser = context.getResources().getXml(resId); AttributeSet attrs = Xml.asAttributeSet(parser); Resources r = context.getResources(); final int innerDepth = parser.getDepth() + 2; int depth; int type; List<int[]> customStateList = new ArrayList<>(); List<Integer> customColorList = new ArrayList<>(); while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && ((depth = parser.getDepth()) >= innerDepth || type != XmlPullParser.END_TAG)) { if (type != XmlPullParser.START_TAG || depth > innerDepth || !parser.getName().equals("item")) { continue; } // Parse all unrecognized attributes as state specifiers. int j = 0; final int numAttrs = attrs.getAttributeCount(); int color = 0; float alpha = 1.0f; int[] stateSpec = new int[numAttrs]; for (int i = 0; i < numAttrs; i++) { final int stateResId = attrs.getAttributeNameResource(i); switch (stateResId) { case android.R.attr.color: int colorAttrId = attrs.getAttributeResourceValue(i, 0); if (colorAttrId == 0) { String colorAttrValue = attrs.getAttributeValue(i); colorAttrId = Integer.valueOf(colorAttrValue.replace("?", "")); color = getColorFromAttribute(context, colorAttrId); } else { color = ContextCompat.getColor(context, colorAttrId); } break; case android.R.attr.alpha: try { alpha = attrs.getAttributeFloatValue(i, 1.0f); } catch (Exception e) { String alphaAttrValue = attrs.getAttributeValue(i); alpha = getFloatFromAttribute(context, Integer.valueOf(alphaAttrValue.replace("?", ""))); } break; default: stateSpec[j++] = attrs.getAttributeBooleanValue(i, false) ? stateResId : -stateResId; } } stateSpec = StateSet.trimStateSet(stateSpec, j); color = modulateColorAlpha(color, alpha); customColorList.add(color); customStateList.add(stateSpec); } int[] colors = new int[customColorList.size()]; int[][] states = new int[customStateList.size()][]; int i = 0; for (int n = states.length; i < n; i++) { colors[i] = customColorList.get(i); states[i] = customStateList.get(i); } return new ColorStateList(states, colors); }
From source file:android.support.v7.content.res.AppCompatColorStateListInflater.java
/** * Fill in this object based on the contents of an XML "selector" element. *///from w ww. ja v a 2 s.co m private static ColorStateList inflate(@NonNull Resources r, @NonNull XmlPullParser parser, @NonNull AttributeSet attrs, @Nullable Resources.Theme theme) throws XmlPullParserException, IOException { final int innerDepth = parser.getDepth() + 1; int depth; int type; int defaultColor = DEFAULT_COLOR; int[][] stateSpecList = new int[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 || depth > innerDepth || !parser.getName().equals("item")) { continue; } final TypedArray a = obtainAttributes(r, theme, attrs, R.styleable.ColorStateListItem); final int baseColor = a.getColor(R.styleable.ColorStateListItem_android_color, Color.MAGENTA); float alphaMod = 1.0f; if (a.hasValue(R.styleable.ColorStateListItem_android_alpha)) { alphaMod = a.getFloat(R.styleable.ColorStateListItem_android_alpha, alphaMod); } else if (a.hasValue(R.styleable.ColorStateListItem_alpha)) { alphaMod = a.getFloat(R.styleable.ColorStateListItem_alpha, alphaMod); } a.recycle(); // Parse all unrecognized attributes as state specifiers. int j = 0; final int numAttrs = attrs.getAttributeCount(); int[] stateSpec = new int[numAttrs]; for (int i = 0; i < numAttrs; i++) { final int stateResId = attrs.getAttributeNameResource(i); if (stateResId != android.R.attr.color && stateResId != android.R.attr.alpha && stateResId != R.attr.alpha) { // Unrecognized attribute, add to state set stateSpec[j++] = attrs.getAttributeBooleanValue(i, false) ? stateResId : -stateResId; } } stateSpec = StateSet.trimStateSet(stateSpec, j); // Apply alpha modulation. If we couldn't resolve the color or // alpha yet, the default values leave us enough information to // modulate again during applyTheme(). final int color = modulateColorAlpha(baseColor, alphaMod); if (listSize == 0 || stateSpec.length == 0) { defaultColor = color; } colorList = GrowingArrayUtils.append(colorList, listSize, color); stateSpecList = GrowingArrayUtils.append(stateSpecList, listSize, stateSpec); listSize++; } int[] colors = new int[listSize]; int[][] stateSpecs = new int[listSize][]; System.arraycopy(colorList, 0, colors, 0, listSize); System.arraycopy(stateSpecList, 0, stateSpecs, 0, listSize); return new ColorStateList(stateSpecs, colors); }
From source file:com.nttec.everychan.ui.theme.CustomThemeHelper.java
@Override public View onCreateView(View parent, String name, Context context, AttributeSet attrs) { int mappingCount = 0; if (attrs != null) { for (int i = 0, size = attrs.getAttributeCount(); i < size; ++i) { String value = attrs.getAttributeValue(i); if (!value.startsWith("?")) continue; int attrId = resources.getIdentifier(value.substring(1), null, null); //Integer.parseInt(value.substring(1)); if (attrId == 0) { Logger.e(TAG, "couldn't get id for attribute: " + value); continue; }/*from w w w . jav a 2 s. c o m*/ int index = customAttrs.indexOfKey(attrId); if (index >= 0) { mappingKeys[mappingCount] = attrs.getAttributeNameResource(i); mappingValues[mappingCount] = customAttrs.valueAt(index); ++mappingCount; } } } if (mappingCount == 0 && textColorPrimaryOverridden == textColorPrimaryOriginal) return null; View view = instantiate(name, context, attrs); if (view == null) return null; boolean shouldOverrideTextColor = textColorPrimaryOverridden != textColorPrimaryOriginal && view instanceof TextView; for (int i = 0; i < mappingCount; ++i) { switch (mappingKeys[i]) { case android.R.attr.background: view.setBackgroundColor(mappingValues[i]); break; case android.R.attr.textColor: if (view instanceof TextView) { ((TextView) view).setTextColor(mappingValues[i]); shouldOverrideTextColor = false; } else { Logger.e(TAG, "couldn't apply attribute 'textColor' on class " + name + " (not instance of TextView)"); } break; case android.R.attr.divider: if (view instanceof ListView) { ListView listView = (ListView) view; int dividerHeight = listView.getDividerHeight(); listView.setDivider(new ColorDrawable(mappingValues[i])); listView.setDividerHeight(dividerHeight); } else { Logger.e(TAG, "couldn't apply attribute 'divider' on class " + name + " (not instance of ListView)"); } break; default: String attrResName = null; try { attrResName = resources.getResourceName(mappingKeys[i]); } catch (Exception e) { attrResName = Integer.toString(mappingKeys[i]); } Logger.e(TAG, "couldn't apply attribure '" + attrResName + "' on class " + name); } } if (shouldOverrideTextColor) { TextView tv = (TextView) view; if (tv.getCurrentTextColor() == textColorPrimaryOriginal) { tv.setTextColor(textColorPrimaryOverridden); } } return view; }