Example usage for android.util AttributeSet getAttributeFloatValue

List of usage examples for android.util AttributeSet getAttributeFloatValue

Introduction

In this page you can find the example usage for android.util AttributeSet getAttributeFloatValue.

Prototype

public float getAttributeFloatValue(int index, float defaultValue);

Source Link

Document

Return the float value of attribute at 'index'.

Usage

From source file:me.henrytao.mdcore.core.MdCompat.java

/**
 * A temporary fix for android.content.res.ColorStateList.inflate
 *//* w w  w  .  j ava  2s .  c o  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);
}