Android LevelListDrawable
LevelListDrawable
用于管理一组 Drawable
可以为里面的 drawable 设置不同的 level,当它们绘制的时候,会根据 level 属性值获取对应的 drawable 绘制到画布上
根节点为 <level-list>
,它并没有可以设置的属性,能做的只是设置每个 <item>
的属性
<item>
属性
属性 | 说明 |
---|---|
android:drawable | 引用的位图资源,如果为空徐璈有一个 Drawable 类型的子节点 |
android:minLevel | 对应的最小值 |
android:maxLevel | 对应的最大值 |
范例
-
创建一个 空的 Android 项目
cn.twle.android.LevelListDrawable
-
使用
shapeDrawable
画圆,一式五份,不同点在于大小res/drawable/shape_circle_1.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="#2C96ED"/> <size android:height="20dp" android:width="20dp"/> </shape>
res/drawable/shape_circle_2.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="#2C96ED"/> <size android:height="40dp" android:width="40dp"/> </shape>
res/drawable/shape_circle_3.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="#2C96ED"/> <size android:height="80dp" android:width="80dp"/> </shape>
res/drawable/shape_circle_4.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="#2C96ED"/> <size android:height="160dp" android:width="160dp"/> </shape>
res/drawable/shape_circle_5.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="#2C96ED"/> <size android:height="320dp" android:width="320dp"/> </shape>
-
使用
LevelListDrawable
,设置五层res/drawable/level_circle.xml
<?xml version="1.0" encoding="utf-8"?> <level-list xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@drawable/shape_circle_1" android:maxLevel="2000"/> <item android:drawable="@drawable/shape_circle_2" android:maxLevel="4000"/> <item android:drawable="@drawable/shape_circle_3" android:maxLevel="6000"/> <item android:drawable="@drawable/shape_circle_4" android:maxLevel="8000"/> <item android:drawable="@drawable/shape_circle_5" android:maxLevel="10000"/> </level-list>
-
修改
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/img_show" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="center" android:src="@drawable/level_circle" /> </LinearLayout>
-
最后修改
MainActivity
package cn.twle.android.levellistdrawable; import android.graphics.drawable.LevelListDrawable; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ImageView; import java.util.Timer; import java.util.TimerTask; public class MainActivity extends AppCompatActivity { private ImageView img_show; private LevelListDrawable ld; private Handler handler = new Handler() { public void handleMessage(Message msg) { if (msg.what == 0x123) { if (ld.getLevel() > 10000) ld.setLevel(0); img_show.setImageLevel(ld.getLevel() + 2000); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); img_show = (ImageView) findViewById(R.id.img_show); ld = (LevelListDrawable) img_show.getDrawable(); img_show.setImageLevel(0); new Timer().schedule(new TimerTask() { @Override public void run() { handler.sendEmptyMessage(0x123); } }, 0, 200); } }