LevelList Drawable用法

注意: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.
从类 android.graphics.drawable.DrawableContainer 继承的方法
draw, getChangingConfigurations, getConstantState, getCurrent, getIntrinsicHeight, getIntrinsicWidth, getMinimumHeight, getMinimumWidth, getOpacity, getPadding, invalidateDrawable, isStateful, scheduleDrawable, selectDrawable, setAlpha, setColorFilter, setDither, setVisible, unscheduleDrawable

 
从类 android.graphics.drawable.Drawable 继承的方法
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.

posted on 2016-08-11 15:37  逍遥散人。  阅读(3322)  评论(0编辑  收藏  举报

导航