FileDocCategorySizeDatePackage
LevelListDrawable.javaAPI DocAndroid 5.1 API8468Thu Mar 12 22:22:30 GMT 2015android.graphics.drawable

LevelListDrawable

public class LevelListDrawable extends DrawableContainer
A resource that manages a number of alternate Drawables, each assigned a maximum numerical value. Setting the level value of the object with {@link #setLevel(int)} will load the image with the next greater or equal value assigned to its max attribute. A good example use of a LevelListDrawable would be a battery level indicator icon, with different images to indicate the current battery level.

It can be defined in an XML file with the <level-list> element. Each Drawable level is defined in a nested <item>. For example:

<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:maxLevel="0" android:drawable="@drawable/ic_wifi_signal_1" />
<item android:maxLevel="1" android:drawable="@drawable/ic_wifi_signal_2" />
<item android:maxLevel="2" android:drawable="@drawable/ic_wifi_signal_3" />
<item android:maxLevel="3" android:drawable="@drawable/ic_wifi_signal_4" />
</level-list>

With this XML saved into the res/drawable/ folder of the project, it can be referenced as the drawable for an {@link android.widget.ImageView}. The default image is the first in the list. It can then be changed to one of the other levels with {@link android.widget.ImageView#setImageLevel(int)}. For more information, see the guide to Drawable Resources.

attr
ref android.R.styleable#LevelListDrawableItem_minLevel
attr
ref android.R.styleable#LevelListDrawableItem_maxLevel
attr
ref android.R.styleable#LevelListDrawableItem_drawable

Fields Summary
private LevelListState
mLevelListState
private boolean
mMutated
Constructors Summary
public LevelListDrawable()

        this(null, null);
    
private LevelListDrawable(LevelListState state, android.content.res.Resources res)

        final LevelListState as = new LevelListState(state, this, res);
        setConstantState(as);
        onLevelChange(getLevel());
    
Methods Summary
public voidaddLevel(int low, int high, Drawable drawable)

        if (drawable != null) {
            mLevelListState.addLevel(low, high, drawable);
            // in case the new state matches our current state...
            onLevelChange(getLevel());
        }
    
public voidclearMutated()

hide

        super.clearMutated();
        mMutated = false;
    
android.graphics.drawable.LevelListDrawable$LevelListStatecloneConstantState()

        return new LevelListState(mLevelListState, this, null);
    
public voidinflate(android.content.res.Resources r, org.xmlpull.v1.XmlPullParser parser, android.util.AttributeSet attrs, android.content.res.Resources.Theme theme)

        super.inflate(r, parser, attrs, theme);

        int type;

        int low = 0;

        final int innerDepth = parser.getDepth() + 1;
        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;
            }

            TypedArray a = obtainAttributes(r, theme, attrs,
                    com.android.internal.R.styleable.LevelListDrawableItem);

            low = a.getInt(
                    com.android.internal.R.styleable.LevelListDrawableItem_minLevel, 0);
            int high = a.getInt(
                    com.android.internal.R.styleable.LevelListDrawableItem_maxLevel, 0);
            int drawableRes = a.getResourceId(
                    com.android.internal.R.styleable.LevelListDrawableItem_drawable, 0);

            a.recycle();

            if (high < 0) {
                throw new XmlPullParserException(parser.getPositionDescription()
                        + ": <item> tag requires a 'maxLevel' attribute");
            }

            Drawable dr;
            if (drawableRes != 0) {
                dr = r.getDrawable(drawableRes, theme);
            } else {
                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 = Drawable.createFromXmlInner(r, parser, attrs, theme);
            }

            mLevelListState.addLevel(low, high, dr);
        }

        onLevelChange(getLevel());
    
public Drawablemutate()

        if (!mMutated && super.mutate() == this) {
            mLevelListState.mutate();
            mMutated = true;
        }
        return this;
    
protected booleanonLevelChange(int level)

        int idx = mLevelListState.indexOfLevel(level);
        if (selectDrawable(idx)) {
            return true;
        }
        return super.onLevelChange(level);
    
protected voidsetConstantState(DrawableContainerState state)

        super.setConstantState(state);

        if (state instanceof LevelListState) {
            mLevelListState = (LevelListState) state;
        }