LevelListDrawable


public class LevelListDrawable
extends DrawableContainer

java.lang.Object
   ↳ android.graphics.drawable.Drawable
     ↳ android.graphics.drawable.DrawableContainer
       ↳ android.graphics.drawable.LevelListDrawable


A resource that manages a number of alternate Drawables, each assigned a maximum numerical value. Setting the level value of the object with Drawable.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 ImageView. The default image is the first in the list. It can then be changed to one of the other levels with ImageView.setImageLevel(int). For more information, see the guide to Drawable Resources.

Summary

XML attributes

android:drawable Reference to a drawable resource to use for the frame. 
android:maxLevel The maximum level allowed for this item. 
android:minLevel The minimum level allowed for this item. 

Public constructors

LevelListDrawable()

Public methods

void addLevel(int low, int high, Drawable drawable)
void inflate(Resources r, XmlPullParser parser, AttributeSet attrs, Resources.Theme theme)

Inflate this Drawable from an XML resource optionally styled by a theme.

Drawable mutate()

Make this drawable mutable.

Protected methods

boolean onLevelChange(int level)

Override this in your subclass to change appearance if you vary based on level.

void setConstantState(DrawableContainer.DrawableContainerState state)

Inherited methods

XML attributes

android:drawable

Reference to a drawable resource to use for the frame. If not given, the drawable must be defined by the first child tag.

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

android:maxLevel

The maximum level allowed for this item.

May be an integer value, such as "100".

android:minLevel

The minimum level allowed for this item.

May be an integer value, such as "100".

Public constructors

LevelListDrawable

Added in API level 1
public LevelListDrawable ()

Public methods

addLevel

Added in API level 1
public void addLevel (int low, 
                int high, 
                Drawable drawable)

Parameters
low int

high int

drawable Drawable

inflate

Added in API level 21
public void inflate (Resources r, 
                XmlPullParser parser, 
                AttributeSet attrs, 
                Resources.Theme theme)

Inflate this Drawable from an XML resource optionally styled by a theme. This can't be called more than once for each Drawable. Note that framework may have called this once to create the Drawable instance from XML resource.

Parameters
r Resources: Resources used to resolve attribute values This value cannot be null.

parser XmlPullParser: XML parser from which to inflate this Drawable This value cannot be null.

attrs AttributeSet: Base set of attribute values This value cannot be null.

theme Resources.Theme: Theme to apply, may be null

Throws
IOException
XmlPullParserException

mutate

Added in API level 3
public Drawable mutate ()

Make this drawable mutable. This operation cannot be reversed. A mutable drawable is guaranteed to not share its state with any other drawable. This is especially useful when you need to modify properties of drawables loaded from resources. By default, all drawables instances loaded from the same resource share a common state; if you modify the state of one instance, all the other instances will receive the same modification. Calling this method on a mutable Drawable will have no effect.

Returns
Drawable This drawable. This value cannot be null.

Protected methods

onLevelChange

Added in API level 1
protected boolean onLevelChange (int level)

Override this in your subclass to change appearance if you vary based on level.

Parameters
level int

Returns
boolean Returns true if the level change has caused the appearance of the Drawable to change (that is, it needs to be drawn), else false if it looks the same and there is no need to redraw it since its last level.

setConstantState

Added in API level 1
protected void setConstantState (DrawableContainer.DrawableContainerState state)

Parameters
state DrawableContainer.DrawableContainerState: This value cannot be null.