Android Graphics.drawable之—LevelListDrawable学习



/**

 *
 * 转载请标明出处:http://blog.csdn.net/u013598111/article/details/50208349

 *   @author:【JunTao_sun】
 *
 *
*/

LevelListDrawable

extends DrawableContainer
java.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.

这是一组资源管理着 许多轮流替换的drawable , 每一个item都标志着最大的一个最大的值。   

可以给对象 设置级别 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>


maxLevel 对应代码的setlevel(int)


<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>






  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值