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

android 代码编写selector--StateListDrawable使用

2016-03-25 14:38 549 查看
在项目开发中肯定会遇到各种点击效果,一般都是创建一个xml文件然后放在drawable文件夹下面,最后控件设置background引用。如下:

<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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android xml 控件