Example usage for android.graphics.drawable Drawable getConstantState

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

Introduction

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

Prototype

public @Nullable ConstantState getConstantState() 

Source Link

Document

Return a ConstantState instance that holds the shared state of this Drawable.

Usage

From source file:Main.java

@TargetApi(Build.VERSION_CODES.KITKAT)
private static void forceStateChangeOnChildren(Drawable drawable) {
    Drawable.ConstantState drawableState = drawable.getConstantState();
    if (drawableState instanceof DrawableContainer.DrawableContainerState) {
        DrawableContainer.DrawableContainerState drawableContainerState = (DrawableContainer.DrawableContainerState) drawableState;

        for (int i = 0; i < drawableContainerState.getChildCount(); i++) {
            Drawable child = drawableContainerState.getChild(i);
            forceStateChange(child, true);
        }/*from   w  ww  .  ja  v  a  2s  .  c om*/
    }
}

From source file:Main.java

private static void forceStateChangeOnChildrenCompat(Drawable drawable) {
    Drawable.ConstantState drawableState = drawable.getConstantState();
    if (drawableState instanceof DrawableContainer.DrawableContainerState) {
        DrawableContainer.DrawableContainerState drawableContainerState = (DrawableContainer.DrawableContainerState) drawableState;

        Drawable[] children = drawableContainerState.getChildren();
        if (children != null) {
            for (Drawable child : children) {
                forceStateChange(child, true);
            }//  w ww  . j  a v  a 2s.c om
        }
    }
}

From source file:Main.java

/**
 * Extract an underlying bitmap from a drawable
 *
 * @param sourceDrawable The source drawable
 * @return The underlying bitmap/*from  ww  w. j a  va  2s  .com*/
 */
public static Bitmap getBitmapFromDrawable(Drawable sourceDrawable) {
    if (sourceDrawable == null) {
        return null;
    }

    if (sourceDrawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) sourceDrawable).getBitmap();
    } else {
        //copying drawable object to not manipulate on the same reference
        Drawable.ConstantState constantState = sourceDrawable.getConstantState();
        if (constantState == null) {
            return null;
        }
        Drawable drawable = constantState.newDrawable().mutate();

        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
        return bitmap;
    }
}

From source file:com.alainesp.fan.sanderson.MainActivity.java

/**
 * Set the badge number to all menu items visible
 *//*  w w w .  ja v a  2  s .  co m*/
private static void setBadgesNumberUI() {
    if (navigationView != null) {
        NavigationMenuView v = (NavigationMenuView) navigationView.getChildAt(0);
        Menu drawerMenu = navigationView.getMenu();

        if (v != null)
            // Iterate all children
            for (int childIndex = 0; childIndex < v.getChildCount(); childIndex++) {
                View v1 = v.getChildAt(childIndex);
                if (v1 instanceof NavigationMenuItemView) {
                    TextView mTextView = (TextView) ((NavigationMenuItemView) v1).getChildAt(0);
                    if (mTextView != null) {
                        // Get the menu index
                        Integer menuIndex = reverseMenuText.get(mTextView.getText().toString());
                        if (menuIndex != null && menuIndex < badgeNumbers.length) {
                            Drawable numberText = null;
                            if (badgeNumbers[menuIndex] > 0) {
                                int height = mTextView.getHeight();
                                numberText = new TextDrawable(badgeNumbers[menuIndex], mTextView.getTextSize(),
                                        mTextView.getCurrentTextColor(), height);
                                numberText.setBounds(0, 0, height, height);
                            }

                            // Similar to NavigationMenuItemView.setIcon
                            Drawable icon = drawerMenu.getItem(menuIndex).getIcon();
                            Drawable.ConstantState state = icon.getConstantState();
                            icon = DrawableCompat.wrap(state == null ? icon : state.newDrawable()).mutate();
                            int mIconSize = navigationView.getContext().getResources().getDimensionPixelSize(
                                    android.support.design.R.dimen.design_navigation_icon_size);
                            icon.setBounds(0, 0, mIconSize, mIconSize);
                            DrawableCompat.setTintList(icon, navigationView.getItemIconTintList());
                            TextViewCompat.setCompoundDrawablesRelative(mTextView, icon, null, numberText,
                                    null);
                        }
                    }
                }
            }
    }
}

From source file:org.deviceconnect.android.manager.core.util.DConnectUtil.java

/**
 * ??Drawable??.//from   ww w  . j a v  a  2  s . c o  m
 *
 * @param drawable ??Drawable
 * @return ??Drawable
 */
public static Drawable convertToGrayScale(final Drawable drawable) {
    Drawable clone = drawable.getConstantState().newDrawable().mutate();
    ColorMatrix matrix = new ColorMatrix();
    matrix.setSaturation(0.2f);
    ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
    clone.setColorFilter(filter);
    return clone;
}

From source file:com.commonsware.cwac.crossport.v7.widget.DrawableUtils.java

/**
 * Some drawable implementations have problems with mutation. This method returns false if
 * there is a known issue in the given drawable's implementation.
 *//*  www  .j a  v a 2  s  .c  o  m*/
public static boolean canSafelyMutateDrawable(@NonNull Drawable drawable) {
    if (Build.VERSION.SDK_INT < 15 && drawable instanceof InsetDrawable) {
        return false;
    } else if (Build.VERSION.SDK_INT < 15 && drawable instanceof GradientDrawable) {
        // GradientDrawable has a bug pre-ICS which results in mutate() resulting
        // in loss of color
        return false;
    } else if (Build.VERSION.SDK_INT < 17 && drawable instanceof LayerDrawable) {
        return false;
    }

    if (drawable instanceof DrawableContainer) {
        // If we have a DrawableContainer, let's traverse its child array
        final Drawable.ConstantState state = drawable.getConstantState();
        if (state instanceof DrawableContainer.DrawableContainerState) {
            final DrawableContainer.DrawableContainerState containerState = (DrawableContainer.DrawableContainerState) state;
            for (final Drawable child : containerState.getChildren()) {
                if (!canSafelyMutateDrawable(child)) {
                    return false;
                }
            }
        }
    } /*else if (drawable instanceof android.support.v4.graphics.drawable.DrawableWrapper) {
      return canSafelyMutateDrawable(
              ((android.support.v4.graphics.drawable.DrawableWrapper) drawable)
                      .getWrappedDrawable());
      }*/ /*else if (drawable instanceof android.support.v7.graphics.drawable.DrawableWrapper) {
           return canSafelyMutateDrawable(
                   ((android.support.v7.graphics.drawable.DrawableWrapper) drawable)
                           .getWrappedDrawable());
           }*/ else if (drawable instanceof ScaleDrawable) {
        return canSafelyMutateDrawable(((ScaleDrawable) drawable).getDrawable());
    }

    return true;
}

From source file:com.bilibili.magicasakura.utils.ThemeUtils.java

public static boolean containsNinePatch(Drawable drawable) {
    drawable = getWrapperDrawable(drawable);
    if (drawable instanceof NinePatchDrawable || drawable instanceof InsetDrawable
            || drawable instanceof LayerDrawable) {
        return true;
    } else if (drawable instanceof StateListDrawable) {
        final DrawableContainer.DrawableContainerState containerState = ((DrawableContainer.DrawableContainerState) drawable
                .getConstantState());/* ww  w  . j  a  v  a  2  s  . c  o  m*/
        //can't get containState from drawable which is containing DrawableWrapperDonut
        //https://code.google.com/p/android/issues/detail?id=169920
        if (containerState == null) {
            return true;
        }
        for (Drawable dr : containerState.getChildren()) {
            dr = getWrapperDrawable(dr);
            if (dr instanceof NinePatchDrawable || dr instanceof InsetDrawable || dr instanceof LayerDrawable) {
                return true;
            }
        }
    }
    return false;
}

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

/**
 * Some drawable implementations have problems with mutation. This method returns false if
 * there is a known issue in the given drawable's implementation.
 *///  w w w.j av  a  2s .c  o m
static boolean canSafelyMutateDrawable(@NonNull Drawable drawable) {
    if (drawable instanceof LayerDrawable) {
        return Build.VERSION.SDK_INT >= 16;
    } else if (drawable instanceof InsetDrawable) {
        return Build.VERSION.SDK_INT >= 14;
    } else if (drawable instanceof StateListDrawable) {
        // StateListDrawable has a bug in mutate() on API 7
        return Build.VERSION.SDK_INT >= 8;
    } else if (drawable instanceof GradientDrawable) {
        // GradientDrawable has a bug pre-ICS which results in mutate() resulting
        // in loss of color
        return Build.VERSION.SDK_INT >= 14;
    } else if (drawable instanceof DrawableContainer) {
        // If we have a DrawableContainer, let's traverse it's child array
        final Drawable.ConstantState state = drawable.getConstantState();
        if (state instanceof DrawableContainer.DrawableContainerState) {
            final DrawableContainer.DrawableContainerState containerState = (DrawableContainer.DrawableContainerState) state;
            for (Drawable child : containerState.getChildren()) {
                if (!canSafelyMutateDrawable(child)) {
                    return false;
                }
            }
        }
    } else if (drawable instanceof android.support.v4ox.graphics.drawable.DrawableWrapper) {
        return canSafelyMutateDrawable(
                ((android.support.v4ox.graphics.drawable.DrawableWrapper) drawable).getWrappedDrawable());
    } else if (drawable instanceof android.support.v7ox.graphics.drawable.DrawableWrapper) {
        return canSafelyMutateDrawable(
                ((android.support.v7ox.graphics.drawable.DrawableWrapper) drawable).getWrappedDrawable());
    }
    return true;
}

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

/**
 * Some drawable implementations have problems with mutation. This method returns false if
 * there is a known issue in the given drawable's implementation.
 *//*from   ww w.j av a2s  .c o  m*/
public static boolean canSafelyMutateDrawable(@NonNull Drawable drawable) {
    if (Build.VERSION.SDK_INT < 8 && drawable instanceof StateListDrawable) {
        // StateListDrawable has a bug in mutate() on API 7
        return false;
    } else if (Build.VERSION.SDK_INT < 15 && drawable instanceof InsetDrawable) {
        return false;
    } else if (Build.VERSION.SDK_INT < 15 && drawable instanceof GradientDrawable) {
        // GradientDrawable has a bug pre-ICS which results in mutate() resulting
        // in loss of color
        return false;
    } else if (Build.VERSION.SDK_INT < 17 && drawable instanceof LayerDrawable) {
        return false;
    }

    if (drawable instanceof DrawableContainer) {
        // If we have a DrawableContainer, let's traverse it's child array
        final Drawable.ConstantState state = drawable.getConstantState();
        if (state instanceof DrawableContainer.DrawableContainerState) {
            final DrawableContainer.DrawableContainerState containerState = (DrawableContainer.DrawableContainerState) state;
            for (final Drawable child : containerState.getChildren()) {
                if (!canSafelyMutateDrawable(child)) {
                    return false;
                }
            }
        }
    } else if (drawable instanceof android.support.v4.graphics.drawable.DrawableWrapper) {
        return canSafelyMutateDrawable(
                ((android.support.v4.graphics.drawable.DrawableWrapper) drawable).getWrappedDrawable());
    } else if (drawable instanceof android.support.v7.graphics.drawable.DrawableWrapper) {
        return canSafelyMutateDrawable(
                ((android.support.v7.graphics.drawable.DrawableWrapper) drawable).getWrappedDrawable());
    } else if (drawable instanceof ScaleDrawable) {
        return canSafelyMutateDrawable(((ScaleDrawable) drawable).getDrawable());
    }

    return true;
}

From source file:com.example.domiter.fileexplorer.dialog.BaseDialogFragment.java

private Drawable tintDrawable(Drawable drawable, int tint) {
    Drawable d = drawable.getConstantState().newDrawable(getActivity().getResources()).mutate();
    d.setColorFilter(tint, PorterDuff.Mode.SRC_IN);
    return d;/*from w w w.  j a  v  a  2  s  .  co m*/
}