Example usage for android.graphics.drawable Drawable mutate

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

Introduction

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

Prototype

public @NonNull Drawable mutate() 

Source Link

Document

Make this drawable mutable.

Usage

From source file:jahirfiquitiva.iconshowcase.utilities.color.ColorUtils.java

@CheckResult
@Nullable/* ww  w  .j  a  va2 s  .co  m*/
public static Drawable getTintedIcon(Drawable drawable, int color) {
    if (drawable != null) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            if (drawable instanceof VectorDrawable) {
                drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
            }
            drawable = DrawableCompat.wrap(drawable.mutate());
        } else {
            drawable = DrawableCompat.wrap(drawable);
        }

        DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_IN);
        DrawableCompat.setTint(drawable, color);
        return drawable;
    } else {
        return null;
    }
}

From source file:com.abhinavjhanwar.android.egg.neko.Cat.java

private static void tint(int color, Drawable... ds) {
    for (Drawable d : ds) {
        if (d != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                d.mutate().setTint(color);
            } else {
                DrawableCompat.setTint(DrawableCompat.wrap(d).mutate(), color);
            }/*from w w  w .  j a va2  s. c  o m*/
        }
    }
}

From source file:com.silentcircle.common.util.ViewUtil.java

public static void tintMenuIcons(@NonNull Menu menu, final int color) {
    for (int i = 0; i < menu.size(); ++i) {
        final MenuItem item = menu.getItem(i);
        Drawable icon = item.getIcon();
        if (icon != null) {
            icon.mutate().setColorFilter(color, PorterDuff.Mode.SRC_IN);
            item.setIcon(icon);//from   w  ww.j  a v  a2 s.  co  m
        }
    }
}

From source file:com.tafayor.selfcamerashot.taflib.helpers.GraphicsHelper.java

public static StateListDrawable createSelector(Drawable normal, Drawable pressed) {
    StateListDrawable selector = new StateListDrawable();

    selector.addState(new int[] { android.R.attr.state_pressed }, pressed.mutate());

    selector.addState(new int[0], normal.mutate());

    return selector;
}

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

public static Drawable tintDrawable(Drawable drawable, ColorStateList cls, PorterDuff.Mode mode) {
    if (drawable == null)
        return null;
    Drawable wrapper = DrawableCompat.wrap(drawable.mutate());
    DrawableCompat.setTintList(wrapper, cls);
    DrawableCompat.setTintMode(drawable, mode);
    return wrapper;
}

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

public static Drawable tintDrawable(Drawable drawable, @ColorInt int color, PorterDuff.Mode mode) {
    if (drawable == null)
        return null;
    Drawable wrapper = DrawableCompat.wrap(drawable.mutate());
    DrawableCompat.setTint(wrapper, color);
    DrawableCompat.setTintMode(drawable, mode);
    return wrapper;
}

From source file:xyz.klinker.android.article.Utils.java

/**
 * Changes the text selection handle colors.
 *//*from   w  w  w .j a va 2 s  .c  o  m*/
static void changeTextSelectionHandleColors(TextView textView, int color) {
    textView.setHighlightColor(Color.argb(40, Color.red(color), Color.green(color), Color.blue(color)));

    try {
        Field editorField = TextView.class.getDeclaredField("mEditor");
        if (!editorField.isAccessible()) {
            editorField.setAccessible(true);
        }

        Object editor = editorField.get(textView);
        Class<?> editorClass = editor.getClass();

        String[] handleNames = { "mSelectHandleLeft", "mSelectHandleRight", "mSelectHandleCenter" };
        String[] resNames = { "mTextSelectHandleLeftRes", "mTextSelectHandleRightRes", "mTextSelectHandleRes" };

        for (int i = 0; i < handleNames.length; i++) {
            Field handleField = editorClass.getDeclaredField(handleNames[i]);
            if (!handleField.isAccessible()) {
                handleField.setAccessible(true);
            }

            Drawable handleDrawable = (Drawable) handleField.get(editor);

            if (handleDrawable == null) {
                Field resField = TextView.class.getDeclaredField(resNames[i]);
                if (!resField.isAccessible()) {
                    resField.setAccessible(true);
                }
                int resId = resField.getInt(textView);
                handleDrawable = ContextCompat.getDrawable(textView.getContext(), resId);
            }

            if (handleDrawable != null) {
                Drawable drawable = handleDrawable.mutate();
                drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
                handleField.set(editor, drawable);
            }
        }
    } catch (Exception ignored) {
    }
}

From source file:arun.com.chromer.browsing.article.util.ArticleUtil.java

/**
 * Changes the text selection handle colors.
 *///  w w  w  .  j  a va2s .  c  o  m
public static void changeTextSelectionHandleColors(TextView textView, int color) {
    textView.setHighlightColor(Color.argb(40, Color.red(color), Color.green(color), Color.blue(color)));

    try {
        Field editorField = TextView.class.getDeclaredField("mEditor");
        if (!editorField.isAccessible()) {
            editorField.setAccessible(true);
        }

        Object editor = editorField.get(textView);
        Class<?> editorClass = editor.getClass();

        String[] handleNames = { "mSelectHandleLeft", "mSelectHandleRight", "mSelectHandleCenter" };
        String[] resNames = { "mTextSelectHandleLeftRes", "mTextSelectHandleRightRes", "mTextSelectHandleRes" };

        for (int i = 0; i < handleNames.length; i++) {
            Field handleField = editorClass.getDeclaredField(handleNames[i]);
            if (!handleField.isAccessible()) {
                handleField.setAccessible(true);
            }

            Drawable handleDrawable = (Drawable) handleField.get(editor);

            if (handleDrawable == null) {
                Field resField = TextView.class.getDeclaredField(resNames[i]);
                if (!resField.isAccessible()) {
                    resField.setAccessible(true);
                }
                int resId = resField.getInt(textView);
                handleDrawable = ContextCompat.getDrawable(textView.getContext(), resId);
            }

            if (handleDrawable != null) {
                Drawable drawable = handleDrawable.mutate();
                drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
                handleField.set(editor, drawable);
            }
        }
    } catch (Exception ignored) {
    }
}

From source file:com.max.library.view.v7.AppCompatDrawableManager.java

public static void tintDrawable(Drawable drawable, TintInfo tint, int[] state) {
    if (shouldMutateBackground(drawable) && drawable.mutate() != drawable) {
        Log.d(TAG, "Mutated drawable is not the same instance as the input.");
        return;//  w w  w. ja  v  a 2 s. c om
    }

    if (tint.mHasTintList || tint.mHasTintMode) {
        drawable.setColorFilter(createTintFilter(tint.mHasTintList ? tint.mTintList : null,
                tint.mHasTintMode ? tint.mTintMode : DEFAULT_MODE, state));
    } else {
        drawable.clearColorFilter();
    }

    if (Build.VERSION.SDK_INT <= 23) {
        // Pre-v23 there is no guarantee that a state change will invoke an invalidation,
        // so we force it ourselves
        drawable.invalidateSelf();
    }
}

From source file:jahirfiquitiva.iconshowcase.utilities.color.ToolbarTinter.java

/**
 * Sets the color filter and/or the alpha transparency on a {@link MenuItem}'s icon.
 *
 * @param menuItem The {@link MenuItem} to theme.
 * @param color    The color to set for the color filter or {@code null} for no changes.
 *///from w ww.java2  s .  c  o m
public static void colorMenuItem(MenuItem menuItem, Integer color, Integer alpha) {
    if (color == null) {
        return; // nothing to do.
    }
    Drawable drawable = menuItem.getIcon();
    if (drawable != null) {
        // If we don't mutate the drawable, then all drawables with this id will have the ColorFilter
        drawable.mutate();
        drawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
        if (alpha != null) {
            drawable.setAlpha(alpha);
        } else {
            drawable.setAlpha(255);
        }
    }
}