Androidダイナミック生成ボタンスタイル


ボタンスタイルの動的生成
次の操作を行います.
    int borderColor = Color.parseColor("#2E3135");
    int bgColor = Color.parseColor("#00FF00");

    //   View    ,     、    、    、    。
    GradientDrawable shape = DrawableUtils.createShape(1, 4, borderColor, bgColor);
    btn1.setBackground(shape);

    int textNormalColor = Color.parseColor("#2E3135");
    int textPressColor = Color.parseColor("#FF0000");
    int textFocusColor = Color.parseColor("#00FF00");
    int textEnableColor = Color.parseColor("#0000FF");

    //           
    ColorStateList colorStateList = DrawableUtils.createColorStateList(textNormalColor, textPressColor, textFocusColor, textEnableColor);
    btn2.setTextColor(colorStateList);
    /* android:focusableInTouchMode="true"          */

    int color1 = Color.parseColor("#FF0000");
    int color2 = Color.parseColor("#00FF00");

    //   View      
    GradientDrawable unSelected = DrawableUtils.createShape(1, 4, color1, color2);
    GradientDrawable selected = DrawableUtils.createShape(1, 4, color2, color1);
    StateListDrawable stateListDrawable = DrawableUtils.createStateListDrawable(unSelected, selected);
    btn3.setBackground(stateListDrawable);

ツールクラス:
public class DrawableUtils {

    /**
     *     shape
     *
     * @param strokeWidth     (px)
     * @param roundRadius     (px)
     * @param strokeColor     
     * @param fillColor         
     * @return GradientDrawable
     */
    public static GradientDrawable createShape(int strokeWidth, int roundRadius, int strokeColor, int fillColor) {
        GradientDrawable gd = new GradientDrawable();
        gd.setColor(fillColor);
        gd.setCornerRadius(roundRadius);
        gd.setStroke(strokeWidth, strokeColor);
        return gd;
    }

    /**
     *           
     *
     * @param normal      
     * @param pressed     
     * @param focused     
     * @param unable       
     * @return ColorStateList
     */
    public static ColorStateList createColorStateList(int normal, int pressed, int focused, int unable) {
        int[] colors = new int[]{pressed, focused, normal, focused, unable, normal};
        int[][] states = new int[6][];
        states[0] = new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled};
        states[1] = new int[]{android.R.attr.state_enabled, android.R.attr.state_focused};
        states[2] = new int[]{android.R.attr.state_enabled};
        states[3] = new int[]{android.R.attr.state_focused};
        states[4] = new int[]{android.R.attr.state_window_focused};
        states[5] = new int[]{};
        return new ColorStateList(states, colors);
    }

    /**
     *         
     *
     * @param unSelected      
     * @param selected     
     * @return StateListDrawable
     */
    public static StateListDrawable createStateListDrawable(GradientDrawable unSelected, GradientDrawable selected) {
        StateListDrawable drawable = new StateListDrawable();
        drawable.addState(new int[]{android.R.attr.state_pressed}, selected);
        drawable.addState(new int[]{-android.R.attr.state_pressed}, unSelected);
        return drawable;
    }

}