Level List
google 说明:A Drawable that manages a number of alternate Drawables, each assigned a maximum numerical value. Setting the level value of the drawable with setLevel()
loads the drawable resource in the level list that has a android:maxLevel
value greater than or equal to the value passed to the method.
我的理解:level list就是将一些图片按照不同的level进行组合为一个drawable中,然后根据需要切换为想要的level,drawable会从你需要的level中选一个符合需求的drawable出来进行展示。
文件应该存放地址:
res/drawable/filename.xml
属性列表:
XML Attributes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Attribute Name | Related Method | Description | |||||||||
android:drawable | Reference to a drawable resource to use for the frame. | ||||||||||
android:maxLevel | The maximum level allowed for this item. | ||||||||||
android:minLevel | The minimum level allowed for this item. |
语法:
-
<?xml version="1.0" encoding="utf-8"?> <level-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/drawable_resource" android:maxLevel="integer" android:minLevel="integer"/>
<item .../>
</level-list>每个level-list可以有任意个item,每个item需要有不同的level,否则level会冲突,系统会找列表最前的进行匹配。
maxLevel,是该item所允许的最大层级数,
minLevel,是该item所允许的最小层级数,
当你setLevel(level),中的level处在minLevel和maxLevel中间时,该item会被激活替换原来的item进行展示。
下面是一个实例:
drawable:res/drawable/levellist.xml
<?xml version="1.0" encoding="utf-8"?> <level-list xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@drawable/test1" android:maxLevel="5" android:minLevel="0"/> <item android:drawable="@drawable/test2" android:maxLevel="10" android:minLevel="6"/> <item android:drawable="@drawable/test3" android:maxLevel="20" android:minLevel="11"/> </level-list>
layout:layout/activity_level_list.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" > <ImageView android:id="@+id/v" android:layout_width="100dp" android:layout_height="100dp" android:src="@drawable/levellist" /> </LinearLayout>
Java:
package com.example.drawabletest1; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; public class ActivityLevelList extends Activity { ImageView v; int level = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_level_list); v = (ImageView) findViewById(R.id.v); v.setImageLevel(0); v.setOnClickListener(new OnClickListener() { public void onClick(View view) { level++; v.setImageLevel(level); } }); } }
执行结果如图:
启动时:
点击6下后level = 6:
- 继续点击5下后: