//---------------------------------------------------------------------------------------------------------------------------------
/*--------------- normalDrawable begin ---------------*/
GradientDrawable normalDrawable = new GradientDrawable();
// 得到随机颜色
Random random = new Random();
int alpha = 255;
int green = random.nextInt(190) + 30; // 30-220
int red = random.nextInt(190) + 30;// 30-220
int blue = random.nextInt(190) + 30;// 30-220
int argb = Color.argb(alpha, red, green, blue);
// 设置填充颜色
normalDrawable.setColor(argb); // 设置圆角半径
normalDrawable.setCornerRadius(UIUtils.dip2Px(6));
/*--------------- normalDrawable end ---------------*/
//-----------------------------------------------------------------------------------------------------------------------------------------------
/*--------------- pressedDrawable begin ---------------*/
GradientDrawable pressedDrawable = new GradientDrawable();
pressedDrawable.setColor(Color.DKGRAY);
pressedDrawable.setCornerRadius(UIUtils.dip2Px(6));
/*--------------- pressedDrawable end ---------------*/
//-------------------------------------------------------------------------------------------------------------------------------------------
// 设置一个状态图片
StateListDrawable stateListDrawable = new StateListDrawable();
// @attr ref android.R.styleable#DrawableStates_state_pressed
stateListDrawable.addState(new int[] { android.R.attr.state_pressed }, pressedDrawable);
stateListDrawable.addState(new int[] {}, normalDrawable);
需求说明:这两个类GradientDrawable和StateListDrawable;
GradientDrawable:可以自定义颜色,圆角。
StateListDrawable:就是一个状态选择器。