PS:本文系转载文章,浏览原文可读性会更好,文章开端有原文链接
目录1、LayerDrawable2、StateListDrawable3、LevelListDrawable1、LayerDrawable本篇文章是基于Android中的Drawable(一)这篇文章来持续写的,LayerDrawable 对应的 xml 文件的标签是 layer-list,它是一种层次化的 Drawable 汇合,通过将不同的 Drawable 搁置在不同的层下面从而达到一种叠加后的成果,咱们先写一个 demo,而后再对 layer-list 标签外面的属性或者子标签的属性进行阐明。(1)在 drawable 文件夹下创立一个 my_layer_list.xml 文件;<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="rectangle"> <solid android:color="#0000ff" /> </shape> </item> <item android:bottom="25dp" android:left="25dp" android:right="25dp" android:top="25dp"> <shape android:shape="rectangle"> <solid android:color="#00ff00" /> </shape> <color android:color="#FF0000"/> </item> <item android:bottom="50dp" android:left="50dp" android:right="50dp" android:top="50dp"> <shape android:shape="rectangle"> <solid android:color="#ff0000" /> </shape> </item></layer-list>(2)Activity 的布局文件 activity_main.xml ;<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <ImageView android:layout_width="500px" android:background="@drawable/my_layer_list" android:layout_height="500px" /></RelativeLayout>运行后果如下所示;
看咱们的 demo,my_layer_list.xml 文件外面有一个 layer-list 标签,layer-list 标签上面会有 item 标签,一个 item 标签其实就是一个 Drawable;layer-list 是有档次的概念,前面的 item 会笼罩后面的 item,通过应用多个 item 能够实现层层叠加的成果。item 标签的属性也挺多的,咱们列举一些咱们开发中罕用到的吧;1)android:top : 绝对于 View 的顶部外部偏移量。2)android:bottom : 绝对于 View 的底部外部偏移量。3)android:left : 绝对于 View 的右边外部偏移量。4)android:right : 绝对于 View 的左边外部偏移量。5)android:drawable : 援用一个已有的 Drawable。2、StateListDrawableStateListDrawable 在 xml 布局文件中对应的是 selector 标签,它是示意 Drawable 汇合,每个 Drawable 都对应着 View 的一种状态,零碎会依据 View 的状态来抉择适合的 Drawable,StateListDrawable次要用于设置可单击的View的背景。为了更好的了解,咱们先写一个 demo;(1)在 drawable 文件夹下新建一个 my_selector.xml 文件;<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/image2" android:state_pressed="true" /> <item android:drawable="@drawable/image" /></selector>(2)在 Activity 的布局文件 activity_main.xml 援用 my_selector.xml 文件;<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:layout_width="match_parent" android:background="@drawable/my_selector" android:text="请点击这个按钮" android:onClick="onClick" android:layout_height="300px" /></RelativeLayout>(3)在名叫 MainActivity 的 Activity 上做点击事件处理;public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onClick(View v) { }}程序一开始运行的后果如下所示;
点击 “请点击这个按钮” 这个按钮后的效果图如下所示;
从下面的 demo 咱们就明确了,如果咱们不点击 “请点击这个按钮” 这个按钮,那么就默认选用 image 来做这个按钮的背景图片对不对?如果咱们点击了这个按钮,那么就用 image2 做按钮的背景图片对不对,到这里咱们也明确了 selector 标签下的子标签 item 也是一种 Drawable。2、1) selector 标签的属性android:constantSize : 这里示意的是 StateListDrawable的固有大小是否不随着其状态的扭转而扭转的,它有2个值,一个是 true,一个是 false,默认值为false;true 示意 StateListDrawable 的固有大小放弃不变,这时它的固有大小是外部所有 Drawable 的固有大小的最大值,false则会随着状态的扭转而扭转。android:dither : 是否开启抖动成果,在Android中的Drawable(一)这篇文章也有讲到过。android:variablePadding :StateListDrawable 的 padding 示意是否随着其状态的扭转而扭转,true 示意会随着状态的扭转而扭转,false 示意 StateListDrawable 的 padding 是外部所有Drawable的 padding的最大值。2、2)selector 标签的子标签 item 的属性android:drawable : 示意一个已有 Drawable 的资源 id。android:state_pressed : 示意按下状态,比方 View 被按下后仍没有松开时的状态。android:state_focused : 示意 View 曾经获取了焦点。android:state_selected : 示意抉择了 View 。android:state_checked : 选中了 View 。android:state_enabled : View 处于可用状态,也就是能够点击的状态。3、LevelListDrawable LevelListDrawable 在 xml 文件中对应的标签是 level-list,它同样示意一个 Drawable 汇合,汇合中的每个 Drawable 都有一个等级的概念,依据不同的等级,LevelListDrawable 会切换为对应的 Drawable。好了,为了不便好了解,同样我也先写一个 demo 演示一下;(1)在 drawable 文件夹下新建一个 my_level_list.xml 文件;<?xml version="1.0" encoding="utf-8"?><level-list xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 1到 5显示这个图片--> <item android:drawable="@drawable/img_1" android:minLevel="1" android:maxLevel="5"></item> <!-- 6到 10显示这个图片--> <item android:drawable="@drawable/img_2" android:minLevel="6" android:maxLevel="10"></item> <!-- 11到 15显示这个图片--> <item android:drawable="@drawable/img_3" android:minLevel="11" android:maxLevel="15"></item> <!-- 16到20显示这个图片--> <item android:drawable="@drawable/img_4" android:minLevel="16" android:maxLevel="20"></item></level-list>(2)在 Activity 的 xml 布局文件 activity_main.xml 加上 my_level_list.xml 的援用;<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <ImageView android:id="@+id/iv" android:layout_width="match_parent" android:src="@drawable/my_level_list" android:layout_height="500px" /> <EditText android:id="@+id/et" android:layout_below="@id/iv" android:layout_width="match_parent" android:hint="请输出1-20之间的一个数字" android:layout_height="wrap_content" /> <Button android:layout_width="match_parent" android:text="确认" android:onClick="onClick" android:layout_below="@id/et" android:layout_height="wrap_content" /></RelativeLayout>(3)Activity 的子类 MainActivity对 my_level_list.xml 文件进行解决;public class MainActivity extends AppCompatActivity { ImageView mIv; EditText mEt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mIv = findViewById(R.id.iv); mEt = findViewById(R.id.et); } public void onClick(View v) { int iLevel = 0; String level = mEt.getText().toString(); LevelListDrawable levelListDrawable = (LevelListDrawable) mIv.getDrawable(); try { iLevel = Integer.valueOf(level); } catch (Exception e) { iLevel = 0; } levelListDrawable.setLevel(iLevel); }}咱们先不急着运行程序,咱们先看 img_3 的图片长什么样子(如下所示);
程序一开始运行的时候如下所示;
在文本框输出13,而后点击 “确认” 按钮,显示成果如下所示;
看下面的 my_level_list.xml 文件,当它的最小等级为10,最大等级为15时,咱们输出13就会显示img_3 的图片;如果咱们在文本框输出4,那么就会显示img_1 这张图片,因为援用img_1 的 Drawable 的最小等级是1,最大等级是5,而输出的4刚好在1-5之间。在 my_level_list.xml 文件中,下面的语法中,每个 item 示意一个 Drawable,并且有对应的等级范畴,由 android:minLevel 和 android:maxLevel 属性来指定,在最小值和最大值之间的等级会对应此 item 中的 Drawable;Drawable 的等级是有范畴的,它的等级范畴是 0-10000,默认值是0也是最小等级,最大等级是10000;如果将 View 作为背景时,能够通过 Drawable 的 setLevel 办法来设置不同的等级从而切换具体的 Drawable 。