Example usage for android.graphics.drawable Drawable getState

List of usage examples for android.graphics.drawable Drawable getState

Introduction

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

Prototype

public @NonNull int[] getState() 

Source Link

Document

Describes the current state, as a union of primitve states, such as android.R.attr#state_focused , android.R.attr#state_selected , etc.

Usage

From source file:Main.java

public static boolean isCacheDrawableExpired(Drawable drawable) {
    if (drawable != null && drawable.getState() == EXPIRED) {
        return true;
    }/*from w w w  . j a v a 2s.c  om*/
    return false;
}

From source file:Main.java

public static void configureColor(View view, int newColor) {
    Drawable drawable = view.getBackground();
    if (drawable instanceof GradientDrawable) {
        GradientDrawable gDrawable = (GradientDrawable) drawable;
        gDrawable.setColor(newColor);//from  w  w  w . j  a  v  a  2  s. c  om
    } else if (drawable instanceof StateListDrawable) {
        StateListDrawable listDrawable = (StateListDrawable) drawable;

        DrawableContainer.DrawableContainerState drawableContainerState = (DrawableContainer.DrawableContainerState) listDrawable
                .getConstantState();

        int c = drawableContainerState.getChildCount();
        Drawable[] children = drawableContainerState.getChildren();

        for (int i = 0; i < c; ++i) {
            Drawable child = children[i];
            int[] states = child.getState();

            if (child instanceof GradientDrawable) {
                GradientDrawable gChild = (GradientDrawable) child;

                if (containsState(states, android.R.attr.state_pressed)) {
                    gChild.setColor(darkenColor(newColor));
                } else {
                    gChild.setColor(newColor);
                }
            } else if (child instanceof ColorDrawable) {
            }
        }
    }
}

From source file:Main.java

public static void forceStateChange(Drawable drawable, boolean forceOnChildren) {
    drawable.setState(getForceState(drawable.getState()));

    if (forceOnChildren) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            forceStateChangeOnChildrenCompat(drawable);
        } else {//  w ww .j a va  2 s .  co  m
            forceStateChangeOnChildren(drawable);
        }
    }
}

From source file:android.support.v7.widget.DrawableUtils.java

/**
 * VectorDrawable has an issue on API 21 where it sometimes doesn't create its tint filter.
 * Fixed by toggling it's state to force a filter creation.
 *///from   ww  w .  j  a  v a 2  s. co m
private static void fixVectorDrawableTinting(final Drawable drawable) {
    final int[] originalState = drawable.getState();
    if (originalState == null || originalState.length == 0) {
        // The drawable doesn't have a state, so set it to be checked
        drawable.setState(ThemeUtils.CHECKED_STATE_SET);
    } else {
        // Else the drawable does have a state, so clear it
        drawable.setState(ThemeUtils.EMPTY_STATE_SET);
    }
    // Now set the original state
    drawable.setState(originalState);
}

From source file:Main.java

/**
 * Ensures the tint filter is consistent with the current tint color and
 * mode.//ww  w.  j av a  2 s  .  c om
 */
public static PorterDuffColorFilter updateTintFilter(Drawable drawable, PorterDuffColorFilter tintFilter,
        ColorStateList tint, PorterDuff.Mode tintMode) {
    if (tint == null || tintMode == null) {
        return null;
    }

    final int color = tint.getColorForState(drawable.getState(), Color.TRANSPARENT);
    return new PorterDuffColorFilter(color, tintMode);
}

From source file:Main.java

/**
 * Copies various properties from one drawable to the other.
 * @param to drawable to copy properties to
 * @param from drawable to copy properties from
 *//*  w w  w . j a  va 2 s . co m*/
public static void copyProperties(Drawable to, Drawable from) {
    if (from == null || to == null || to == from) {
        return;
    }

    to.setBounds(from.getBounds());
    to.setChangingConfigurations(from.getChangingConfigurations());
    to.setLevel(from.getLevel());
    to.setVisible(from.isVisible(), /* restart */false);
    to.setState(from.getState());
}

From source file:com.telly.mrvector.Utils.java

/**
 * Ensures the tint filter is consistent with the current tint color and
 * mode./*from w  ww.j a  v a2  s. c  o  m*/
 */
static PorterDuffColorFilter updateTintFilter(Drawable drawable, PorterDuffColorFilter tintFilter,
        ColorStateList tint, PorterDuff.Mode tintMode) {
    if (tint == null || tintMode == null) {
        return null;
    }

    final int color = tint.getColorForState(drawable.getState(), Color.TRANSPARENT);

    if (tintFilter == null || !LOLLIPOP_PLUS) { // TODO worth caching them?
        return new PorterDuffColorFilter(color, tintMode);
    }

    tryInvoke(tintFilter, "setColor", INT_ARG, color);
    tryInvoke(tintFilter, "setMode", MODE_ARG, tintMode);
    return tintFilter;
}

From source file:com.john.main.Utils.java

/**
 * Ensures the tint filter is consistent with the current tint color and
 * mode./*from  ww w. j av a 2 s  .  co  m*/
 */
static PorterDuffColorFilter updateTintFilter(Drawable drawable, PorterDuffColorFilter tintFilter,
        ColorStateList tint, Mode tintMode) {
    if (tint == null || tintMode == null) {
        return null;
    }

    final int color = tint.getColorForState(drawable.getState(), Color.TRANSPARENT);

    if (tintFilter == null || !LOLLIPOP_PLUS) { // TODO worth caching them?
        return new PorterDuffColorFilter(color, tintMode);
    }

    tryInvoke(tintFilter, "setColor", INT_ARG, color);
    tryInvoke(tintFilter, "setMode", MODE_ARG, tintMode);
    return tintFilter;
}

From source file:com.albedinsky.android.ui.graphics.drawable.DrawableWrapper.java

/**
 * Creates a new instance of DrawableWrapper which wraps the given <var>drawable</var>.
 *
 * @param drawable The drawable to wrap.
 *///from w  w  w. j  a v  a 2  s. c o m
public DrawableWrapper(@NonNull Drawable drawable) {
    this.mDrawable = drawable;

    /**
     * Copy current state of the wrapped drawable.
     */
    setState(drawable.getState());
    setBounds(drawable.getBounds());
    setLevel(drawable.getLevel());

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        setCallback(drawable.getCallback());
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        setAlpha(drawable.getAlpha());
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        setColorFilter(drawable.getColorFilter());
    }
    mDrawable.setCallback(this);
}

From source file:com.bilibili.magicasakura.widgets.AppCompatCompoundDrawableHelper.java

private Drawable applySupportCompoundDrawableTint(int position) {
    Drawable originDrawable = ((TextView) mView).getCompoundDrawables()[position];
    Drawable compoundDrawable = originDrawable;
    TintInfo tintInfo = mCompoundDrawableTintInfos[position];
    if (compoundDrawable != null && tintInfo != null && tintInfo.mHasTintList) {
        compoundDrawable = DrawableCompat.wrap(compoundDrawable);
        compoundDrawable.mutate();//from   w  w w  . j a  va 2s  . co  m
        if (tintInfo.mHasTintList) {
            DrawableCompat.setTintList(compoundDrawable, tintInfo.mTintList);
        }
        if (tintInfo.mHasTintMode) {
            DrawableCompat.setTintMode(compoundDrawable, tintInfo.mTintMode);
        }
        if (compoundDrawable.isStateful()) {
            compoundDrawable.setState(originDrawable.getState());
        }
        return compoundDrawable;
    }
    return originDrawable;
}