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:com.javielinux.tweettopics2.TweetTopicsActivity.java

public void refreshTheme() {

    layoutBackgroundApp.setBackgroundColor(
            Color.parseColor("#" + themeManager.getStringColor("color_background_new_status")));

    themeManager.setColors();//from w w w. j  ava2s  .c o m

    layoutBackgroundBar.setBackgroundDrawable(
            ImageUtils.createBackgroundDrawable(this, themeManager.getColor("color_top_bar"), false, 0));
    layoutBackgroundColumnsBarContainer.setBackgroundDrawable(
            ImageUtils.createBackgroundDrawable(this, themeManager.getColor("color_top_bar"), false, 0));

    StateListDrawable statesButtonBg = new StateListDrawable();
    statesButtonBg.addState(new int[] { android.R.attr.state_pressed }, ImageUtils
            .createBackgroundDrawable(this, themeManager.getColor("color_button_press_default"), false, 0));
    statesButtonBg.addState(new int[] { -android.R.attr.state_pressed },
            ImageUtils.createBackgroundDrawable(this, themeManager.getColor("color_top_bar"), false, 0));

    imgBarAvatarBg.setBackgroundDrawable(statesButtonBg);

    StateListDrawable statesButton = new StateListDrawable();
    statesButton.addState(new int[] { android.R.attr.state_pressed }, ImageUtils.createBackgroundDrawable(this,
            themeManager.getColor("color_button_press_default"), false, 0));
    statesButton.addState(new int[] { -android.R.attr.state_pressed },
            ImageUtils.createBackgroundDrawable(this, themeManager.getColor("color_top_bar"), false, 0));

    imgNewStatus.setBackgroundDrawable(statesButton);

    float size = getResources().getDimension(R.dimen.actionbar_height) - Utils.dip2px(this, 12);
    imgBarCounter.setBackgroundDrawable(new BitmapDrawable(getResources(),
            ImageUtils.getBackgroundBitmapInBubble(this, Color.RED, Utils.TYPE_RECTANGLE, size, size)));

    //(findViewById(R.id.tweettopics_bar_divider1)).setBackgroundDrawable(ImageUtils.createBackgroundDrawable(this, themeManager.getColor("color_top_bar"), false, 0));
    //(findViewById(R.id.tweettopics_bar_divider2)).setBackgroundDrawable(ImageUtils.createBackgroundDrawable(this, themeManager.getColor("color_top_bar"), false, 0));

    Drawable d = new ColorDrawable(android.R.color.transparent);

    StateListDrawable states = new StateListDrawable();
    states.addState(new int[] { android.R.attr.state_pressed }, d);
    states.addState(new int[] { android.R.attr.state_window_focused }, d);
    states.addState(new int[] { android.R.attr.state_pressed }, d);
    states.addState(new int[] { android.R.attr.state_selected }, d);
    states.addState(new int[] { android.R.attr.color },
            new ColorDrawable(themeManager.getColor("color_indicator_text")));
    indicator.setBackgroundDrawable(states);
    indicator.setTextSize(getResources().getDimension(R.dimen.text_size_title_page_indicator));
}

From source file:com.winneredge.stockly.wcommons.floatingactionwidget.FloatingActionButton.java

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private Drawable createFillDrawable() {
    StateListDrawable drawable = new StateListDrawable();
    drawable.addState(new int[] { -android.R.attr.state_enabled }, createCircleDrawable(mColorDisabled));
    drawable.addState(new int[] { android.R.attr.state_pressed }, createCircleDrawable(mColorPressed));
    drawable.addState(new int[] {}, createCircleDrawable(mColorNormal));

    if (Util.hasLollipop()) {
        RippleDrawable ripple = new RippleDrawable(
                new ColorStateList(new int[][] { {} }, new int[] { mColorRipple }), drawable, null);
        setOutlineProvider(new ViewOutlineProvider() {
            @Override/*  ww  w  .j  a  v a 2 s  .co m*/
            public void getOutline(View view, Outline outline) {
                outline.setOval(0, 0, view.getWidth(), view.getHeight());
            }
        });
        setClipToOutline(true);
        mBackgroundDrawable = ripple;
        return ripple;
    }

    mBackgroundDrawable = drawable;
    return drawable;
}

From source file:com.initialxy.cordova.themeablebrowser.ThemeableBrowser.java

private void setButtonImages(View view, BrowserButton buttonProps, int disabledAlpha) {
    Drawable normalDrawable = null;//  w ww . j  a va  2 s.c om
    Drawable disabledDrawable = null;
    Drawable pressedDrawable = null;

    CharSequence description = view.getContentDescription();

    if (buttonProps.image != null || buttonProps.wwwImage != null) {
        try {
            normalDrawable = getImage(buttonProps.image, buttonProps.wwwImage, buttonProps.wwwImageDensity);
            ViewGroup.LayoutParams params = view.getLayoutParams();
            params.width = normalDrawable.getIntrinsicWidth();
            params.height = normalDrawable.getIntrinsicHeight();
        } catch (Resources.NotFoundException e) {
            emitError(ERR_LOADFAIL,
                    String.format("Image for %s, %s, failed to load", description, buttonProps.image));
        } catch (IOException ioe) {
            emitError(ERR_LOADFAIL,
                    String.format("Image for %s, %s, failed to load", description, buttonProps.wwwImage));
        }
    } else {
        emitWarning(WRN_UNDEFINED,
                String.format("Image for %s is not defined. Button will not be shown", description));
    }

    if (buttonProps.imagePressed != null || buttonProps.wwwImagePressed != null) {
        try {
            pressedDrawable = getImage(buttonProps.imagePressed, buttonProps.wwwImagePressed,
                    buttonProps.wwwImageDensity);
        } catch (Resources.NotFoundException e) {
            emitError(ERR_LOADFAIL, String.format("Pressed image for %s, %s, failed to load", description,
                    buttonProps.imagePressed));
        } catch (IOException e) {
            emitError(ERR_LOADFAIL, String.format("Pressed image for %s, %s, failed to load", description,
                    buttonProps.wwwImagePressed));
        }
    } else {
        emitWarning(WRN_UNDEFINED, String.format("Pressed image for %s is not defined.", description));
    }

    if (normalDrawable != null) {
        // Create the disabled state drawable by fading the normal state
        // drawable. Drawable.setAlpha() stopped working above Android 4.4
        // so we gotta bring out some bitmap magic. Credit goes to:
        // http://stackoverflow.com/a/7477572
        Bitmap enabledBitmap = ((BitmapDrawable) normalDrawable).getBitmap();
        Bitmap disabledBitmap = Bitmap.createBitmap(normalDrawable.getIntrinsicWidth(),
                normalDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(disabledBitmap);

        Paint paint = new Paint();
        paint.setAlpha(disabledAlpha);
        canvas.drawBitmap(enabledBitmap, 0, 0, paint);

        Resources activityRes = cordova.getActivity().getResources();
        disabledDrawable = new BitmapDrawable(activityRes, disabledBitmap);
    }

    StateListDrawable states = new StateListDrawable();
    if (pressedDrawable != null) {
        states.addState(new int[] { android.R.attr.state_pressed }, pressedDrawable);
    }
    if (normalDrawable != null) {
        states.addState(new int[] { android.R.attr.state_enabled }, normalDrawable);
    }
    if (disabledDrawable != null) {
        states.addState(new int[] {}, disabledDrawable);
    }

    setBackground(view, states);
}

From source file:edu.mit.viral.shen.DroidFish.java

private final void setButtonData(ImageButton button, int bWidth, int bHeight, int svgResId, SVG touched) {
    SVG svg = SVGParser.getSVGFromResource(getResources(), svgResId);
    button.setBackgroundDrawable(new SVGPictureDrawable(svg));

    StateListDrawable sld = new StateListDrawable();
    sld.addState(new int[] { android.R.attr.state_pressed }, new SVGPictureDrawable(touched));
    button.setImageDrawable(sld);/*w ww .j  av  a  2 s.  com*/

    LayoutParams lp = button.getLayoutParams();
    lp.height = bHeight;
    lp.width = bWidth;
    button.setLayoutParams(lp);
    button.setPadding(0, 0, 0, 0);
    button.setScaleType(ScaleType.FIT_XY);
}