public class LevelListDrawable extends DrawableContainer
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.
DrawableContainer.DrawableContainerState
Drawable.Callback, Drawable.ConstantState
Constructor and Description |
---|
LevelListDrawable() |
Modifier and Type | Method and Description |
---|---|
void |
addLevel(int low,
int high,
Drawable drawable) |
void |
clearMutated()
Clears the mutated state, allowing this drawable to be cached and
mutated again.
|
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 boolean |
onLevelChange(int level)
Override this in your subclass to change appearance if you vary based
on level.
|
protected void |
setConstantState(DrawableContainer.DrawableContainerState state) |
applyTheme, canApplyTheme, draw, getAlpha, getChangingConfigurations, getConstantState, getCurrent, getCurrentIndex, getHotspotBounds, getIntrinsicHeight, getIntrinsicWidth, getMinimumHeight, getMinimumWidth, getOpacity, getOpticalInsets, getOutline, getPadding, invalidateDrawable, isAutoMirrored, isStateful, jumpToCurrentState, onBoundsChange, onLayoutDirectionChanged, onStateChange, scheduleDrawable, selectDrawable, setAlpha, setAutoMirrored, setColorFilter, setCurrentIndex, setDither, setEnterFadeDuration, setExitFadeDuration, setHotspot, setHotspotBounds, setTintList, setTintMode, setVisible, unscheduleDrawable, updateDensity
clearColorFilter, copyBounds, copyBounds, createFromPath, createFromResourceStream, createFromResourceStream, createFromStream, createFromXml, createFromXml, createFromXmlInner, createFromXmlInner, getBounds, getCallback, getColorFilter, getDirtyBounds, getLayoutDirection, getLevel, getState, getTransparentRegion, inflate, invalidateSelf, isFilterBitmap, isProjected, isVisible, obtainAttributes, parseTintMode, resolveOpacity, scheduleSelf, setBounds, setBounds, setCallback, setChangingConfigurations, setColorFilter, setFilterBitmap, setLayoutDirection, setLevel, setState, setTint, setXfermode, unscheduleSelf
public void addLevel(int low, int high, Drawable drawable)
protected boolean onLevelChange(int level)
Drawable
onLevelChange
in class DrawableContainer
public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs, Resources.Theme theme) throws XmlPullParserException, IOException
Drawable
inflate
in class Drawable
r
- Resources used to resolve attribute valuesparser
- XML parser from which to inflate this Drawableattrs
- Base set of attribute valuestheme
- Theme to apply, may be nullXmlPullParserException
IOException
public Drawable mutate()
Drawable
mutate
in class DrawableContainer
Drawable.ConstantState
,
Drawable.getConstantState()
public void clearMutated()
Drawable
This is hidden because only framework drawables can be cached, so custom drawables don't need to support constant state, mutate(), or clearMutated().
clearMutated
in class DrawableContainer
protected void setConstantState(DrawableContainer.DrawableContainerState state)
setConstantState
in class DrawableContainer