Android drawable level-list

来源:互联网 发布:linux使用gcc调试 编辑:程序博客网 时间:2024/05/18 21:39

Android 布局中imageview的src不仅可以设置一张图片或者一个颜色,还可以设置一个level-list(和前面文章说的那个selector有某种程度上很相似),直接上一个代码把:

在drawable中定义个level-list:

<?xml version="1.0" encoding="utf-8"?><level-list xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@drawable/shape1"          android:maxLevel="25"          android:minLevel="0"/>    <item android:drawable="@drawable/shape_oval"          android:maxLevel="50"          android:minLevel="26"/>    <item android:drawable="@drawable/shape_rectangle"          android:maxLevel="75"          android:minLevel="51"/>    <item android:drawable="@drawable/shape_ring"          android:maxLevel="100"          android:minLevel="76"/></level-list>

然后可以直接设置src:

<ImageView        android:id="@+id/imageView"        android:layout_width="60dp"        android:layout_height="60dp"        android:layout_gravity="center_horizontal"        android:layout_marginTop="30dp"        android:src="@drawable/level_res"/>
什么意思?

level-list中有几个item   没一个item还对应着一个最大值和一个最小值,这是一个范围,我们可以获取到一个imageview的drawable,然后对其设置level,如果这个levle介于某个item设置的最大值和最小值之间,那么此时这个imageview将会显示这个item对应的shape样式,如下:

 Drawable drawable = mLevelimageView.getDrawable();        if (drawable != null) {            drawable.setLevel(level);        }


也就是说上面的level-list中定义的是四个item样式,我们可以设置imageview的level值然后让其像是对应的shape样式。就这样,有关shape样式的详细介绍,以后再做一个文章吧,shape样式也是在drawable中定义一个shape样式文件:

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="oval">    <size android:height="100dp"        android:width="100dp"></size>    <gradient android:centerX="0.5"        android:centerY="0.5"        android:type="sweep"        android:startColor="@color/colorPrimary"        android:endColor="@color/colorAccent"></gradient></shape>



原创粉丝点击