/**
*
* 转载请标明出处:http://blog.csdn.net/u013598111/article/details/50208349
* @author:【JunTao_sun】
*
*
*/
LevelListDrawable
extends DrawableContainerjava.lang.Object | |||
↳ | android.graphics.drawable.Drawable | ||
| ↳ | android.graphics.drawable.DrawableContainer | |
| | ↳ | android.graphics.drawable.LevelListDrawable |
Class Overview
A resource that manages a number of alternate Drawables, each assigned a maximum numerical value. Setting the level value of the object withsetLevel(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.
可以给对象 设置级别 level 的值 setLevel(int) ,level-list中某项的android:maxLevel数值大于或者等于setLevel设置的数值,就会被加载,
换句话说你setlevel的级别 等于maxlevel级,那级所引用的图片将显示。
。有个很典型的例子 android 系统 电量的标志 就用了这个levelListDrawable 的效果,
用不同的图片表示当前的电量级别。
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/ic_1"
android:maxLevel="0" />
<item
android:drawable="@drawable/ic_2"
android:maxLevel="1"
/>
<item
android:drawable="@drawable/ic_3"
android:maxLevel="2"
/>
</level-list></span>
<span style="font-size:18px;"> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image"
android:scaleType="centerCrop"
android:src="@drawable/level_drawable" /></span>
<span style="font-size:18px;">public class MainActivity extends Activity {
private int x=0;
private LevelListDrawable l;
private Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
int a=x%3;
l.setLevel(a);
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// LevelListDrawable c=(LevelListDrawable) getResources().getDrawable(R.drawable.clipdrawable);
ImageView image=(ImageView) this.findViewById(R.id.image);
l=(LevelListDrawable) image.getDrawable();
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
handler.obtainMessage(0x11).sendToTarget();
x++;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();
}</span>