您的位置:首页 > 移动开发 > Android开发

Android中的Drawable资源—— StateListDrawable

2016-06-14 14:56 369 查看
这种使用xml来定义图片的方式在buttion中经常使用,它会根据状态来显示不同的图片资源。

存放位置:

res/drawable/

使用方式:

在Java文件中:R.drawable.filename

例如:

Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.myimage);


在xml布局文件中:@[package:]drawable/filename

例如:

<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/myninepatch" />


语法:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize=["true" | "false"]
android:dither=["true" | "false"]
android:variablePadding=["true" | "false"] >
<item
android:drawable="@[package:]drawable/drawable_resource"
android:state_pressed=["true" | "false"]
android:state_focused=["true" | "false"]
android:state_hovered=["true" | "false"]
android:state_selected=["true" | "false"]
android:state_checkable=["true" | "false"]
android:state_checked=["true" | "false"]
android:state_enabled=["true" | "false"]
android:state_activated=["true" | "false"]
android:state_window_focused=["true" | "false"] />
</selector>


例如:

<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 触摸时并且当前窗口处于交互状态 -->
<item android:state_pressed="true" android:state_window_focused="true" android:drawable= "@drawable/pic1" />
<!--  触摸时并且没有获得焦点状态 -->
<item android:state_pressed="true" android:state_focused="false" android:drawable="@drawable/pic2" />
<!--选中时的图片背景-->
<item android:state_selected="true" android:drawable="@drawable/pic3" />
<!--获得焦点时的图片背景-->
<item android:state_focused="true" android:drawable="@drawable/pic4" />
<!-- 窗口没有处于交互时的背景图片 -->
<item android:drawable="@drawable/pic5" />
</selector>


<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/button" />


另外,需要说的是,上面定义的这个xml会被相应的解析为StateListDrawable对象,所以我们也可以使用代码来进行实现。

//初始化一个空对象
StateListDrawable stalistDrawable = new StateListDrawable();
//获取对应的属性值 Android框架自带的属性 attr
int pressed = android.R.attr.state_pressed;
int window_focused = android.R.attr.state_window_focused;
int focused = android.R.attr.state_focused;
int selected = android.R.attr.state_selected;

stalistDrawable.addState(new int []{pressed , window_focused}, getResources().getDrawable(R.drawable.pic1));
stalistDrawable.addState(new int []{pressed , -focused}, getResources().getDrawable(R.drawable.pic2);
stalistDrawable.addState(new int []{selected }, getResources().getDrawable(R.drawable.pic3);
stalistDrawable.addState(new int []{focused }, getResources().getDrawable(R.drawable.pic4);
//没有任何状态时显示的图片,我们给它设置我空集合
stalistDrawable.addState(new int []{}, getResources().getDrawable(R.drawable.pic5);


上面的“-”负号表示对应的属性值为false

https://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

https://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html

http://blog.csdn.net/qinjuning/article/details/7474827
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: