Example usage for android.graphics.drawable StateListDrawable StateListDrawable

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

Introduction

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

Prototype

public StateListDrawable() 

Source Link

Usage

From source file:Main.java

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

    BitmapDrawable nomalBitmap = new BitmapDrawable(nomal);
    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

/**
 * set btn selector with corner drawable for special position
 *//*  www.  j  a v a 2 s. co m*/
public static StateListDrawable btnSelector(float radius, int normalColor, int pressColor, int postion) {
    StateListDrawable bg = new StateListDrawable();
    Drawable normal = null;
    Drawable pressed = null;

    if (postion == 0) {// left btn
        normal = cornerDrawable(normalColor, new float[] { 0, 0, 0, 0, 0, 0, radius, radius });
        pressed = cornerDrawable(pressColor, new float[] { 0, 0, 0, 0, 0, 0, radius, radius });
    } else if (postion == 1) {// right btn
        normal = cornerDrawable(normalColor, new float[] { 0, 0, 0, 0, radius, radius, 0, 0 });
        pressed = cornerDrawable(pressColor, new float[] { 0, 0, 0, 0, radius, radius, 0, 0 });
    } else if (postion == -1) {// only one btn
        normal = cornerDrawable(normalColor, new float[] { 0, 0, 0, 0, radius, radius, radius, radius });
        pressed = cornerDrawable(pressColor, new float[] { 0, 0, 0, 0, radius, radius, radius, radius });
    } else if (postion == -2) {// for material dialog
        normal = cornerDrawable(normalColor, radius);
        pressed = cornerDrawable(pressColor, radius);
    }

    bg.addState(new int[] { -android.R.attr.state_pressed }, normal);
    bg.addState(new int[] { android.R.attr.state_pressed }, pressed);
    return bg;
}

From source file:Main.java

/**
 * helper to create a StateListDrawable for the drawer item background
 *
 * @param selected_color//from  w  ww  .  j  av a 2 s .c o m
 * @return
 */
public static StateListDrawable getDrawerItemBackground(int selected_color) {
    ColorDrawable clrActive = new ColorDrawable(selected_color);
    StateListDrawable states = new StateListDrawable();
    states.addState(new int[] { android.R.attr.state_selected }, clrActive);
    return states;
}

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

/**
 * To state list drawable state list drawable.
 *
 * @param normal  the normal//from   w w  w  .  j a v a2 s.com
 * @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: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(int normalColor, int pressedColor, int focusedColor,
        int unableColor) {
    StateListDrawable drawable = new StateListDrawable();
    Drawable normal = new ColorDrawable(normalColor);
    Drawable pressed = new ColorDrawable(pressedColor);
    Drawable focused = new ColorDrawable(focusedColor);
    Drawable unable = new ColorDrawable(unableColor);
    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

public static StateListDrawable makeStateDrawable(Drawable drawable, Drawable pressedDrawable,
        Drawable disabledDrawable) {/*from  ww w. j  av a 2s  .  co  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:Main.java

public static void traverseAndRecolor(View root, int color, boolean withStates,
        boolean setClickableItemBackgrounds) {
    Context context = root.getContext();

    if (setClickableItemBackgrounds && root.isClickable()) {
        StateListDrawable selectableItemBackground = new StateListDrawable();
        selectableItemBackground.addState(new int[] { android.R.attr.state_pressed },
                new ColorDrawable((color & 0xffffff) | 0x33000000));
        selectableItemBackground.addState(new int[] { android.R.attr.state_focused },
                new ColorDrawable((color & 0xffffff) | 0x44000000));
        selectableItemBackground.addState(new int[] {}, null);
        root.setBackground(selectableItemBackground);
    }// w ww  . j  a  v  a 2s.  com

    if (root instanceof ViewGroup) {
        ViewGroup parent = (ViewGroup) root;
        for (int i = 0; i < parent.getChildCount(); i++) {
            traverseAndRecolor(parent.getChildAt(i), color, withStates, setClickableItemBackgrounds);
        }

    } else if (root instanceof ImageView) {
        ImageView imageView = (ImageView) root;
        Drawable sourceDrawable = imageView.getDrawable();
        if (withStates && sourceDrawable != null && sourceDrawable instanceof BitmapDrawable) {
            imageView.setImageDrawable(
                    makeRecoloredDrawable(context, (BitmapDrawable) sourceDrawable, color, true));
        } else {
            imageView.setColorFilter(color, PorterDuff.Mode.MULTIPLY);
        }

    } else if (root instanceof TextView) {
        TextView textView = (TextView) root;
        if (withStates) {
            int sourceColor = textView.getCurrentTextColor();
            ColorStateList colorStateList = new ColorStateList(
                    new int[][] { new int[] { android.R.attr.state_pressed },
                            new int[] { android.R.attr.state_focused }, new int[] {} },
                    new int[] { sourceColor, sourceColor, color });
            textView.setTextColor(colorStateList);
        } else {
            textView.setTextColor(color);
        }

    } else if (root instanceof AnalogClock) {
        AnalogClock analogClock = (AnalogClock) root;
        try {
            Field hourHandField = AnalogClock.class.getDeclaredField("mHourHand");
            hourHandField.setAccessible(true);
            Field minuteHandField = AnalogClock.class.getDeclaredField("mMinuteHand");
            minuteHandField.setAccessible(true);
            Field dialField = AnalogClock.class.getDeclaredField("mDial");
            dialField.setAccessible(true);
            BitmapDrawable hourHand = (BitmapDrawable) hourHandField.get(analogClock);
            if (hourHand != null) {
                Drawable d = makeRecoloredDrawable(context, hourHand, color, withStates);
                d.setCallback(analogClock);
                hourHandField.set(analogClock, d);
            }
            BitmapDrawable minuteHand = (BitmapDrawable) minuteHandField.get(analogClock);
            if (minuteHand != null) {
                Drawable d = makeRecoloredDrawable(context, minuteHand, color, withStates);
                d.setCallback(analogClock);
                minuteHandField.set(analogClock, d);
            }
            BitmapDrawable dial = (BitmapDrawable) dialField.get(analogClock);
            if (dial != null) {
                Drawable d = makeRecoloredDrawable(context, dial, color, withStates);
                d.setCallback(analogClock);
                dialField.set(analogClock, d);
            }
        } catch (NoSuchFieldException ignored) {
        } catch (IllegalAccessException ignored) {
        } catch (ClassCastException ignored) {
        } // TODO: catch all exceptions?
    }
}

From source file:Main.java

/**
 * To state list drawable state list drawable.
 *
 * @param normalColor  the normal color//from ww  w. j a v a  2s.  com
 * @param pressedColor the pressed color
 * @param focusedColor the focused color
 * @param unableColor  the unable color
 * @return the state list drawable
 */
public static StateListDrawable toStateListDrawable(int normalColor, int pressedColor, int focusedColor,
        int unableColor) {
    StateListDrawable drawable = new StateListDrawable();
    Drawable normal = new ColorDrawable(normalColor);
    Drawable pressed = new ColorDrawable(pressedColor);
    Drawable focused = new ColorDrawable(focusedColor);
    Drawable unable = new ColorDrawable(unableColor);
    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;
}