코드를 통해shape/selector 정의

1396 단어
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;
    }
}

활용단어참조
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))));

좋은 웹페이지 즐겨찾기