注意:item元素級別里面不能出現負數,只能是正整數,並且要么是升序,要么是倒序,順序亂了不起作用。
1 <?xml version="1.0" encoding="utf-8"?> 2 <level-list 3 xmlns:android="http://schemas.android.com/apk/res/android" > 4 <item 5 android:drawable="@drawable/drawable_resource" 6 android:maxLevel="integer" 7 android:minLevel="integer" /> 8 </level-list>
XML定義的Drawable的一種,以<level-list>作為根元素,其間可包含任意多個<item>節點,每一個<item>節點包含一個drawable對象和maxLevel與minLevel值,如:
1 <?xml version="1.0" encoding="utf-8"?> 2 <level-list xmlns:android="http://schemas.android.com/apk/res/android" > 3 <item android:minLevel="0" android:maxLevel="10" android:drawable="@drawable/b1" /> 4 <item android:minLevel="11" android:maxLevel="20" android:drawable="@drawable/b2" /> 5 <item android:minLevel="21" android:maxLevel="30" android:drawable="@drawable/b3" /> 6 <item android:minLevel="31" android:maxLevel="40" android:drawable="@drawable/b4" /> 7 </level-list>
當我們向LevelListDrawable對象提供一個Level值后,LevelListDrawable對象就會從前往后查看每一個<item>,當某個<item>節點的Level范圍滿足提供的那個Level值后,就會返回該<item>結點里指定的drawable。並且不會繼續往后找。所以定義這個LevelListDrawable時要注意各個<item>的順序。比如:
1 <?xml version="1.0" encoding="utf-8"?> 2 <level-list xmlns:android="http://schemas.android.com/apk/res/android" > 3 <item android:maxLevel="40" android:drawable="@drawable/b4" /> 4 <item android:maxLevel="10" android:drawable="@drawable/b1" /> 5 <item android:maxLevel="20" android:drawable="@drawable/b2" /> 6 <item android:maxLevel="30" android:drawable="@drawable/b3" /> 7 </level-list>
那么無論提供什么樣的Level值,都不會返回后面三個<item>里的drawable(這里如果提供的Level值超過40,將返回一個空對象)。
可以通過Drawable對象的setLevel(int)方法來提供Level值。
比如當我們將一個LevelListDrawable作為一個View的background后,可以通過View的getBackground()方法獲取這個Drawable對象,然后調用這個Drawable對象的setLevel()方法,提供不同的Level值,就可以改變View的背景。這個可以用來制作諸如進度條、音量調節等效果。
ImageView組件還提供了setImageLevel()方法來快捷設置android:src指定的LevelListDrawable的Level值(android:backgroudn指定的背景還是要通過View的形式來更改)。
【官方文檔】
android.graphics.drawable
類 LevelListDrawable
java.lang.Object

android.graphics.drawable.Drawable

android.graphics.drawable.DrawableContainer

android.graphics.drawable.LevelListDrawable
所有已實現的接口:Drawable.Callback
public class LevelListDrawableextends DrawableContainer
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:
1 <level-list xmlns:android="http://schemas.android.com/apk/res/android"> 2 <item android:maxLevel="0" android:drawable="@drawable/ic_wifi_signal_1" /> 3 <item android:maxLevel="1" android:drawable="@drawable/ic_wifi_signal_2" /> 4 <item android:maxLevel="2" android:drawable="@drawable/ic_wifi_signal_3" /> 5 <item android:maxLevel="3" android:drawable="@drawable/ic_wifi_signal_4" /> 6 </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).
嵌套類摘要 |
---|
從類 android.graphics.drawable.DrawableContainer 繼承的嵌套類/接口
|
---|
DrawableContainer.DrawableContainerState |
從類 android.graphics.drawable.Drawable 繼承的嵌套類/接口
|
---|
Drawable.Callback, Drawable.ConstantState |
構造方法摘要 | |
---|---|
LevelListDrawable() |
方法摘要 | |
---|---|
void |
addLevel(int low, int high, Drawable drawable) |
void |
inflate(Resources r, XmlPullParser parser, AttributeSet attrs) |
Drawable |
mutate() Make this drawable mutable. |
draw, getChangingConfigurations, getConstantState, getCurrent, getIntrinsicHeight, getIntrinsicWidth, getMinimumHeight, getMinimumWidth, getOpacity, getPadding, invalidateDrawable, isStateful, scheduleDrawable, selectDrawable, setAlpha, setColorFilter, setDither, setVisible, unscheduleDrawable |
clearColorFilter, copyBounds, copyBounds, createFromPath, createFromResourceStream, createFromStream, createFromXml, createFromXmlInner, getBounds, getLevel, getState, getTransparentRegion, invalidateSelf, isVisible, resolveOpacity, scheduleSelf, setBounds, setBounds, setCallback, setChangingConfigurations, setColorFilter, setFilterBitmap, setLevel, setState, unscheduleSelf |
從類 java.lang.Object 繼承的方法
|
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
構造方法詳細信息 |
---|
LevelListDrawable
public LevelListDrawable()
方法詳細信息 |
---|
addLevel
public void addLevel(int low, int high, Drawable drawable)
inflate
public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs) throws XmlPullParserException, IOException
覆蓋:類 Drawable 中的 inflate拋出:XmlPullParserExceptionIOException
mutate
public Drawable mutate()
從類 Drawable 復制的描述Make this drawable mutable. This operation cannot be reversed. A mutabledrawable is guaranteed to not share its state with any other drawable.This is especially useful when you need to modify properties of drawablesloaded from resources. By default, all drawables instances loaded fromthe same resource share a common state; if you modify the state of oneinstance, all the other instances will receive the same modification.Calling this method on a mutable Drawable will have no effect.覆蓋:類 DrawableContainer 中的 mutate返回:This drawable.