private StateListDrawable getStateSelector(ImageEntity image){
StateListDrawable stalistDrawable = new StateListDrawable();
//int normal = android.R.attr.state_empty;
//int pressed = android.R.attr.state_pressed;
//int focused = android.R.attr.state_focused;
//int selected = android.R.attr.state_selected;
Bitmap bitmap1 = FileImageUtil.getImageLocal(image.getPath1());
Drawable drawable1 = new BitmapDrawable(context.getResources(), bitmap1);
stalistDrawable.addState(new int[]{-android.R.attr.state_pressed, android.R.attr.state_enabled}, drawable1);
Bitmap bitmap2 = FileImageUtil.getImageLocal(image.getPath2());
Drawable drawable2 = new BitmapDrawable(context.getResources(), bitmap2);
stalistDrawable.addState(new int []{android.R.attr.state_pressed}, drawable2);
return stalistDrawable;
}
注意:
stalistDrawable.addState(new int[]{-android.R.attr.state_pressed, android.R.attr.state_enabled}, drawable1);这段代码中,如果没有'-android.R.attr.state_presse',就无法实现selector的效果
2.colorstatelist问题
int[][] states = {{android.R.attr.state_empty},{android.R.attr.state_pressed, android.R.attr.state_selected}};
int[] colors = {context.getResources().getColor(R.color.white), context.getResources().getColor(R.color.yellow)};
ColorStateList colorStateList = new ColorStateList(states, colors);
return colorStateList;
}