转载请注明:https://blog.youkuaiyun.com/u012854870/article/details/79973728
在Android开发中,有时候需要对一个ImageView设置很多不同图片以表示某种应用状态,比如,典型的是手机的信号强度从强到弱有多种状态图;wifi有解锁和未解锁状态,解锁和未解锁状态的图标也是很多种。如果每次都一一的通过ImageView设置图片的src来达到这一目的,实在是太过于繁琐,且维护和管理起来不便。因此,引入ImageView的setImageLevel和level-list实现这一目的。
那就在drawable目录下先分类写个level-list:test.xml
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@mipmap/iv_down_arrow"
android:maxLevel="0"/>
<item
android:drawable="@mipmap/iv_up_arrow"
android:maxLevel="1"/>
<item
android:drawable="@mipmap/iv_up_arrow2"
android:maxLevel="2"/>
<item
android:drawable="@mipmap/iv_up_arrow3"
android:maxLevel="3"/>
<item
android:drawable="@mipmap/iv_up_arrow4"
android:maxLevel="4"/>
<item
android:drawable="@mipmap/iv_up_arrow5"
android:maxLevel="5"/>
<item
android:drawable="@mipmap/iv_up_arrow6"
android:maxLevel="6"/>
<item
android:drawable="@mipmap/iv_up_arrow7"
android:maxLevel="7"/>
</level-list>
然后在ImageView中添加为src
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/test"
/>
或者
ImageView imageView=(ImageView)findViewById(R.id.imageView);
imageView.setImageResource(R.drawable.test);
最后代码中使用
imageView.setImageLevel(1);