首页 > 系统 > Android > 正文

Android ImageView的setImageLevel和level-list使用简介

2019-11-09 16:11:33
字体:
来源:转载
供稿:网友

转载一篇比较实用的不同状态得图片显示方法

Android ImageView的setImageLevel和level-list使用简介

在Android开发中,有时候需要对一个ImageView设置很多不同图片以表示某种应用状态,比如,典型的是手机的信号强度从强到弱有多种状态图;wifi有解锁和未解锁状态,解锁和未解锁状态的图标也是很多种。如果每次都一一的通过ImageView设置图片的src来达到这一目的,实在是太过于繁琐,且维护和管理起来不便。因此,引入ImageView的setImageLevel和level-list实现这一目的。比如图中有10种不同类型的手机信号表示状态图标icon:

那就先分类写两个level-list:p.xml和q.xml(都位于drawable目录下)p.xml:

[html] view plain copy<level-list xmlns:android="http://schemas.android.com/apk/res/android">      <item android:maxLevel="0" android:drawable="@drawable/p0" />      <item android:maxLevel="1" android:drawable="@drawable/p1" />      <item android:maxLevel="2" android:drawable="@drawable/p2" />      <item android:maxLevel="3" android:drawable="@drawable/p3" />      <item android:maxLevel="4" android:drawable="@drawable/p3" />  </level-list>  

q.xml:

[html] view plain copy<level-list xmlns:android="http://schemas.android.com/apk/res/android">      <item android:maxLevel="0" android:drawable="@drawable/q0" />      <item android:maxLevel="1" android:drawable="@drawable/q1" />      <item android:maxLevel="2" android:drawable="@drawable/q2" />      <item android:maxLevel="3" android:drawable="@drawable/q3" />      <item android:maxLevel="4" android:drawable="@drawable/q4" />  </level-list>  

然后,如果意图使一个ImageView显示 q3 的图标,那么通过这样的代码就可以实现:

[java] view plain copyImageView image=(ImageView)findViewById(R.id.image);          image.setImageResource(R.drawable.q);          image.setImageLevel(3);  
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表