Example usage for android.util StateSet trimStateSet

List of usage examples for android.util StateSet trimStateSet

Introduction

In this page you can find the example usage for android.util StateSet trimStateSet.

Prototype

public static int[] trimStateSet(int[] states, int newSize) 

Source Link

Usage

From source file:android.support.v7.content.res.AppCompatColorStateListInflater.java

/**
 * Fill in this object based on the contents of an XML "selector" element.
 *///  w  w w.  j  av  a2 s  .  c om
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.bilibili.magicasakura.utils.ColorStateListUtils.java

protected static int[] extractStateSet(AttributeSet attrs) {
    int j = 0;/*from  w  ww.j  av  a 2 s  .com*/
    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
 *///from www . j  a va 2  s.c  om
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);
}