android 代码编写selector--StateListDrawable使用
2016-03-25 14:38
549 查看
在项目开发中肯定会遇到各种点击效果,一般都是创建一个xml文件然后放在drawable文件夹下面,最后控件设置background引用。如下:
这是一个给Button使用的点击效果背景选择,这种不同状态显示不同背景的xml文件我们称为selector。其实selector的本质是一个drawable对象。
这样一两个效果还好,但是当项目里面的点击效果特别多的时候如果再一个个去写的话无疑是很费精力。这种情况下我们就可以使用StateListDrawable用代码来写。接下来我们从本地和网络图片两种来介绍。
一、本地:
这里要注意添加的顺序,只要有一个状态与之相配,背景就会被换掉。所以不要把大范围放在前面了,例如如果
这里还有其他的效果就不一一列举了,
最后在代码中给控件添加:
二、接下来是扩展网络图片:
这里说一下最后的resId参数,这里就是文章开头说的在xml中写的selector,防止图片加载失败后没有点击图片显示的情况。使用方法和本地一样
这里的图片下载是用的开源框架universal-image-loader。
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/back_press" android:state_pressed="true"/> <item android:drawable="@drawable/back_press" android:state_focused="true"/> <item android:drawable="@drawable/back"/> </selector>
这是一个给Button使用的点击效果背景选择,这种不同状态显示不同背景的xml文件我们称为selector。其实selector的本质是一个drawable对象。
这样一两个效果还好,但是当项目里面的点击效果特别多的时候如果再一个个去写的话无疑是很费精力。这种情况下我们就可以使用StateListDrawable用代码来写。接下来我们从本地和网络图片两种来介绍。
一、本地:
public class DrawableUtil { public static StateListDrawable addStateListBgDrawable(Context context, int idNormal, int idPressed) { StateListDrawable drawable = new StateListDrawable(); drawable.addState(new int[] { android.R.attr.state_selected }, context .getResources().getDrawable(idPressed)); drawable.addState(new int[] { android.R.attr.state_pressed }, context .getResources().getDrawable(idPressed)); drawable.addState(new int[] { android.R.attr.state_enabled }, context .getResources().getDrawable(idNormal)); drawable.addState(new int[] {}, context.getResources().getDrawable(idNormal)); return drawable; } }
这里要注意添加的顺序,只要有一个状态与之相配,背景就会被换掉。所以不要把大范围放在前面了,例如如果
drawable.addState(new int[] {},context.getResources().getDrawable(idNormal));放在第一个的话,就没有什么效果了。
这里还有其他的效果就不一一列举了,
最后在代码中给控件添加:
button.setBackground(DrawableUtil.addStateListDrawable(context, R.drawable.pic1, R.drawable.pic2));传入一张正常显示图片和一张点击下的图片就好了。
二、接下来是扩展网络图片:
/** * 显示网络图片 * * @param context * @param idNormalUrl 正常图片 * @param idPressedUrl 按下选中图片 * @param resId 图片加载失败的默认selector * @return */ @SuppressLint("NewApi") public static void SetnetSelector(final Context context, final ImageView mImage, final String idNormalUrl, final String idPressedUrl, final int resId) { final StateListDrawable drawable = new StateListDrawable(); // 显示图片的配置 final DisplayImageOptions options = new DisplayImageOptions.Builder() .cacheInMemory(true).cacheOnDisk(true) .bitmapConfig(Bitmap.Config.ARGB_8888).build(); ImageLoader.getInstance().loadImage(idPressedUrl, options, new SimpleImageLoadingListener() { @SuppressWarnings("deprecation") @Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { // TODO Auto-generated method stub Drawable draw = new BitmapDrawable(loadedImage); drawable.addState( new int[] { android.R.attr.state_selected }, draw); drawable.addState( new int[] { android.R.attr.state_pressed }, draw); ImageLoader.getInstance().loadImage(idNormalUrl, options, new SimpleImageLoadingListener() { @Override public void onLoadingComplete( String imageUri, View view, Bitmap loadedImage) { // TODO Auto-generated method stub Drawable draw = new BitmapDrawable( loadedImage); drawable.addState( new int[] { android.R.attr.state_enabled }, draw); drawable.addState(new int[] {}, draw); mImage.setBackground(drawable); super.onLoadingComplete(imageUri, view, loadedImage); } @Override public void onLoadingFailed( String imageUri, View view, FailReason failReason) { // TODO Auto-generated method stub mImage.setBackgroundResource(resId); super.onLoadingFailed(imageUri, view, failReason); } }); super.onLoadingComplete(imageUri, view, loadedImage); } @Override public void onLoadingFailed(String imageUri, View view, FailReason failReason) { // TODO Auto-generated method stub mImage.setBackgroundResource(resId); super.onLoadingFailed(imageUri, view, failReason); } }); }
这里说一下最后的resId参数,这里就是文章开头说的在xml中写的selector,防止图片加载失败后没有点击图片显示的情况。使用方法和本地一样
`button.setBackground(DrawableUtil.addStateListDrawable(context, R.drawable.pic1, R.drawable.pic2,R.drawable.localXml));`,就是多传一个失败情况下的selector。
这里的图片下载是用的开源框架universal-image-loader。
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories