Search in sources :

Example 1 with FilterableStateListDrawable

use of com.bilibili.magicasakura.drawables.FilterableStateListDrawable in project MagicaSakura by Bilibili.

the class StateListDrawableInflateImpl method inflateDrawable.

@Override
public Drawable inflateDrawable(Context context, XmlPullParser parser, AttributeSet attrs) throws IOException, XmlPullParserException {
    StateListDrawable sd = null;
    ArrayList<int[]> states = new ArrayList<>();
    ArrayList<Drawable> drawables = new ArrayList<>();
    SparseArray<ColorFilter> mColorFilterMap = null;
    final int innerDepth = parser.getDepth() + 1;
    int type;
    int depth;
    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;
        }
        Drawable dr = DrawableUtils.getAttrDrawable(context, attrs, android.R.attr.drawable);
        states.add(DrawableUtils.extractStateSet(attrs));
        // attributes and extracting states.
        if (dr == null) {
            while ((type = parser.next()) == XmlPullParser.TEXT) {
            }
            if (type != XmlPullParser.START_TAG) {
                throw new XmlPullParserException(parser.getPositionDescription() + ": <item> tag requires a 'drawable' attribute or " + "child tag defining a drawable");
            }
            dr = DrawableUtils.createFromXmlInner(context, parser, attrs);
        } else {
            ColorFilter colorFilter = DrawableUtils.getAttrColorFilter(context, attrs, R.attr.drawableTint, R.attr.drawableTintMode);
            if (colorFilter != null) {
                if (mColorFilterMap == null) {
                    mColorFilterMap = new SparseArray<>();
                }
                mColorFilterMap.put(drawables.size(), colorFilter);
            }
        }
        drawables.add(dr);
    }
    if (states.size() >= 1) {
        if (mColorFilterMap != null) {
            sd = new FilterableStateListDrawable();
            for (int i = 0; i < states.size(); i++) {
                ((FilterableStateListDrawable) sd).addState(states.get(i), drawables.get(i), mColorFilterMap.get(i));
            }
        } else {
            sd = new StateListDrawable();
            for (int i = 0; i < states.size(); i++) {
                sd.addState(states.get(i), drawables.get(i));
            }
        }
    }
    return sd;
}
Also used : FilterableStateListDrawable(com.bilibili.magicasakura.drawables.FilterableStateListDrawable) ColorFilter(android.graphics.ColorFilter) ArrayList(java.util.ArrayList) StateListDrawable(android.graphics.drawable.StateListDrawable) Drawable(android.graphics.drawable.Drawable) FilterableStateListDrawable(com.bilibili.magicasakura.drawables.FilterableStateListDrawable) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) StateListDrawable(android.graphics.drawable.StateListDrawable) FilterableStateListDrawable(com.bilibili.magicasakura.drawables.FilterableStateListDrawable)

Example 2 with FilterableStateListDrawable

use of com.bilibili.magicasakura.drawables.FilterableStateListDrawable in project remusic by aa112901.

the class StateListDrawableUtils method inflateDrawable.

@Override
protected Drawable inflateDrawable(Context context, XmlPullParser parser, AttributeSet attrs) throws IOException, XmlPullParserException {
    StateListDrawable sd = null;
    ArrayList<int[]> states = new ArrayList<>();
    ArrayList<Drawable> drawables = new ArrayList<>();
    SparseArray<ColorFilter> mColorFilterMap = null;
    final int innerDepth = parser.getDepth() + 1;
    int type;
    int depth;
    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;
        }
        Drawable dr = getAttrDrawable(context, attrs, android.R.attr.drawable);
        states.add(extractStateSet(attrs));
        // attributes and extracting states.
        if (dr == null) {
            while ((type = parser.next()) == XmlPullParser.TEXT) {
            }
            if (type != XmlPullParser.START_TAG) {
                throw new XmlPullParserException(parser.getPositionDescription() + ": <item> tag requires a 'drawable' attribute or " + "child tag defining a drawable");
            }
            dr = createFromXmlInner(context, parser, attrs);
        } else {
            ColorFilter colorFilter = getAttrColorFilter(context, attrs, R.attr.drawableTint, R.attr.drawableTintMode);
            if (colorFilter != null) {
                if (mColorFilterMap == null) {
                    mColorFilterMap = new SparseArray<>();
                }
                mColorFilterMap.put(drawables.size(), colorFilter);
            }
        }
        drawables.add(dr);
    }
    if (states.size() >= 1) {
        if (mColorFilterMap != null) {
            sd = new FilterableStateListDrawable();
            for (int i = 0; i < states.size(); i++) {
                ((FilterableStateListDrawable) sd).addState(states.get(i), drawables.get(i), mColorFilterMap.get(i));
            }
        } else {
            sd = new StateListDrawable();
            for (int i = 0; i < states.size(); i++) {
                sd.addState(states.get(i), drawables.get(i));
            }
        }
    }
    return sd;
}
Also used : FilterableStateListDrawable(com.bilibili.magicasakura.drawables.FilterableStateListDrawable) ColorFilter(android.graphics.ColorFilter) ArrayList(java.util.ArrayList) StateListDrawable(android.graphics.drawable.StateListDrawable) Drawable(android.graphics.drawable.Drawable) FilterableStateListDrawable(com.bilibili.magicasakura.drawables.FilterableStateListDrawable) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) StateListDrawable(android.graphics.drawable.StateListDrawable) FilterableStateListDrawable(com.bilibili.magicasakura.drawables.FilterableStateListDrawable)

Aggregations

ColorFilter (android.graphics.ColorFilter)2 Drawable (android.graphics.drawable.Drawable)2 StateListDrawable (android.graphics.drawable.StateListDrawable)2 FilterableStateListDrawable (com.bilibili.magicasakura.drawables.FilterableStateListDrawable)2 ArrayList (java.util.ArrayList)2 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)2