?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | public class DrawableUtil { /** * 定义一个shape资源 * * @param rgb * @param corneradius * @return */ public static GradientDrawable getDrawable( int rgb, int corneradius) { GradientDrawable gradientDrawable = new GradientDrawable(); gradientDrawable.setColor(rgb); gradientDrawable.setGradientType(GradientDrawable.RECTANGLE); gradientDrawable.setCornerRadius(corneradius); gradientDrawable.setStroke(UIUtils.dp2px(1 ), rgb); return gradientDrawable; } public static StateListDrawable getSelector(Drawable normalDrawable,Drawable pressDrawable) { StateListDrawable stateListDrawable = new StateListDrawable(); //给当前的颜色选择器添加选中图片指向状态,未选中图片指向状态 stateListDrawable.addState(new int []{android.R.attr.state_enabled, android.R.attr.state_pressed}, pressDrawable); stateListDrawable.addState(new int []{android.R.attr.state_enabled}, normalDrawable); //设置默认状态 stateListDrawable.addState(new int []{}, normalDrawable); return stateListDrawable; } } |
运用 ?
| 1 2 3 4 5 6 | int r = random.nextInt(210); int g = random.nextInt(210 ); int b = random.nextInt(210 ); tv.setBackground( DrawableUtil.getSelector(DrawableUtil.getDrawable(Color.rgb(r, g, b), UIUtils.dp2px( 5)), DrawableUtil.getDrawable(Color.WHITE, UIUtils.dp2px(5)))); |