Example usage for android.graphics.drawable StateListDrawable addState

List of usage examples for android.graphics.drawable StateListDrawable addState

Introduction

In this page you can find the example usage for android.graphics.drawable StateListDrawable addState.

Prototype

public void addState(int[] stateSet, Drawable drawable) 

Source Link

Document

Add a new image/string ID to the set of images.

Usage

From source file:Main.java

public static StateListDrawable bgColorDrawableSelector(Bitmap nomal, Bitmap focus) {

    @SuppressWarnings("deprecation")
    BitmapDrawable nomalBitmap = new BitmapDrawable(nomal);
    @SuppressWarnings("deprecation")
    BitmapDrawable focusBitmap = new BitmapDrawable(focus);
    StateListDrawable selector = new StateListDrawable();
    selector.addState(new int[] { android.R.attr.state_pressed }, focusBitmap);
    selector.addState(new int[] { android.R.attr.state_selected }, focusBitmap);
    selector.addState(new int[] { android.R.attr.state_focused }, focusBitmap);
    selector.addState(new int[] {}, nomalBitmap);
    return selector;
}

From source file:Main.java

private static StateListDrawable getStateListDrawable(int pressedColor, Integer normalColor) {
    StateListDrawable states = new StateListDrawable();
    states.addState(new int[] { android.R.attr.state_pressed }, new ColorDrawable(pressedColor));
    states.addState(new int[] { android.R.attr.state_focused }, new ColorDrawable(pressedColor));
    states.addState(new int[] { android.R.attr.state_activated }, new ColorDrawable(pressedColor));
    if (null != normalColor)
        states.addState(new int[] {}, new ColorDrawable(normalColor));
    return states;
}

From source file:Main.java

public static StateListDrawable toStateListDrawable(Drawable normal, Drawable pressed, Drawable focused,
        Drawable unable) {/*w w  w. j ava 2 s  .  c  o m*/
    StateListDrawable drawable = new StateListDrawable();
    drawable.addState(new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled }, pressed);
    drawable.addState(new int[] { android.R.attr.state_enabled, android.R.attr.state_focused }, focused);
    drawable.addState(new int[] { android.R.attr.state_enabled }, normal);
    drawable.addState(new int[] { android.R.attr.state_focused }, focused);
    drawable.addState(new int[] { android.R.attr.state_window_focused }, unable);
    drawable.addState(new int[] {}, normal);
    return drawable;
}

From source file:Main.java

@NonNull
private static StateListDrawable getStateListDrawable(int normalColor, int pressedColor) {
    StateListDrawable states = new StateListDrawable();
    states.addState(new int[] { android.R.attr.state_pressed }, new ColorDrawable(pressedColor));
    states.addState(new int[] { android.R.attr.state_focused }, new ColorDrawable(pressedColor));
    states.addState(new int[] { android.R.attr.state_activated }, new ColorDrawable(pressedColor));
    states.addState(new int[] {}, new ColorDrawable(normalColor));
    states.addState(StateSet.WILD_CARD, new ColorDrawable(normalColor));
    return states;
}

From source file:Main.java

/**
 * To state list drawable state list drawable.
 *
 * @param normal  the normal//from   ww w . ja v  a2s.  c  o  m
 * @param pressed the pressed
 * @param focused the focused
 * @param unable  the unable
 * @return the state list drawable
 */
public static StateListDrawable toStateListDrawable(Drawable normal, Drawable pressed, Drawable focused,
        Drawable unable) {
    StateListDrawable drawable = new StateListDrawable();
    drawable.addState(new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled }, pressed);
    drawable.addState(new int[] { android.R.attr.state_enabled, android.R.attr.state_focused }, focused);
    drawable.addState(new int[] { android.R.attr.state_enabled }, normal);
    drawable.addState(new int[] { android.R.attr.state_focused }, focused);
    drawable.addState(new int[] { android.R.attr.state_window_focused }, unable);
    drawable.addState(new int[] {}, normal);
    return drawable;
}

From source file:com.waz.zclient.ui.utils.ColorUtils.java

public static Drawable getButtonBackground(int borderColor, int fillColor, int strokeWidth, int cornerRadius) {
    int fillColorPressed = getPressColor(PRESSED_ALPHA, fillColor);
    int borderColorPressed = getPressColor(PRESSED_ALPHA, borderColor);

    GradientDrawable gradientDrawablePressed = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
            new int[] { fillColorPressed, fillColorPressed });
    gradientDrawablePressed.setStroke(strokeWidth, borderColorPressed);
    gradientDrawablePressed.setCornerRadius(cornerRadius);

    GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
            new int[] { fillColor, fillColor });
    gradientDrawable.setStroke(strokeWidth, borderColor);
    gradientDrawable.setCornerRadius(cornerRadius);

    StateListDrawable states = new StateListDrawable();
    states.addState(new int[] { android.R.attr.state_pressed }, gradientDrawablePressed);
    states.addState(new int[] { android.R.attr.state_focused }, gradientDrawablePressed);
    states.addState(new int[] {}, gradientDrawable);

    return states;
}

From source file:Main.java

public static Drawable createRippleDrawable(final View v, final int color, Drawable pressed) {

    Drawable drawable = v.getBackground();

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

        if (drawable instanceof RippleDrawable) {
            drawable = ((RippleDrawable) drawable).getDrawable(0);

            RippleDrawable rippleDrawable = new RippleDrawable(new ColorStateList(
                    new int[][] { new int[] { android.R.attr.state_pressed }, new int[] { 0 } },
                    new int[] { color, 0 }), drawable, null);

            return rippleDrawable;
        } else {//from  w  w  w. j  a v  a  2 s . c  o m
            if (drawable == null) {
                drawable = new ColorDrawable(0);
                v.setBackground(drawable);
                RippleDrawable rippleDrawable = new RippleDrawable(new ColorStateList(
                        new int[][] { new int[] { android.R.attr.state_pressed }, new int[] { 0 } },
                        new int[] { color, 0 }), drawable, new ColorDrawable(0xffffffff));
                return rippleDrawable;
            } else {
                RippleDrawable rippleDrawable = new RippleDrawable(new ColorStateList(
                        new int[][] { new int[] { android.R.attr.state_pressed }, new int[] { 0 } },
                        new int[] { color, 0 }), drawable, null);
                return rippleDrawable;
            }
        }
    } else {

        if (drawable == null) {
            drawable = new ColorDrawable(color);
        }

        if (pressed == null) {
            pressed = drawable;
        }

        StateListDrawable sld = new StateListDrawable();
        sld.addState(new int[] { android.R.attr.state_pressed, }, pressed);

        if (v.getBackground() != null) {
            sld.addState(new int[] { 0, }, drawable);
        }

        return sld;
    }
}

From source file:Main.java

public static StateListDrawable makeStateDrawable(Drawable drawable, Drawable pressedDrawable,
        Drawable disabledDrawable) {// w  w w. ja v  a2s.c  o m
    boolean set = false;
    StateListDrawable stateDrawable = new StateListDrawable();
    if (disabledDrawable != null) {
        set = true;
        stateDrawable.addState(new int[] { -android.R.attr.state_enabled }, disabledDrawable);
    }
    if (pressedDrawable != null) {
        set = true;
        stateDrawable.addState(new int[] { android.R.attr.state_pressed }, pressedDrawable);
    }
    if (drawable != null) {
        set = true;
        stateDrawable.addState(new int[0], drawable);
    }
    return set ? stateDrawable : null;
}

From source file:de.dala.simplenews.utilities.UIUtils.java

public static StateListDrawable getStateListDrawableByColorDrawable(Drawable colorDrawable) {
    StateListDrawable stateList = new StateListDrawable();
    stateList.addState(new int[] { android.R.attr.state_pressed }, colorDrawable);
    stateList.addState(new int[] { android.R.attr.state_focused }, colorDrawable);
    stateList.addState(new int[] { android.R.attr.state_checked }, colorDrawable);
    stateList.addState(new int[] { android.R.attr.state_selected }, colorDrawable);
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {
        stateList.addState(new int[] { android.R.attr.state_activated }, colorDrawable);
    }/*w  ww. j a  v  a 2s .c  o m*/
    stateList.addState(new int[] {}, new ColorDrawable(Color.WHITE));
    return stateList;
}

From source file:Main.java

public static Drawable makeRecoloredDrawable(Context context, BitmapDrawable drawable, int color,
        boolean withStates) {
    Bitmap recoloredBitmap = recolorBitmap(drawable, color);
    BitmapDrawable recoloredDrawable = new BitmapDrawable(context.getResources(), recoloredBitmap);

    if (!withStates) {
        return recoloredDrawable;
    }//w  w  w. jav a  2s. c  o m

    StateListDrawable stateDrawable = new StateListDrawable();
    stateDrawable.addState(new int[] { android.R.attr.state_pressed }, drawable);
    stateDrawable.addState(new int[] { android.R.attr.state_focused }, drawable);
    stateDrawable.addState(new int[] {}, recoloredDrawable);
    return stateDrawable;
}