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:com.dm.material.dashboard.candybar.helpers.DrawableHelper.java

@Nullable
public static Drawable getTintedDrawable(@NonNull Context context, @DrawableRes int res, @ColorInt int color) {
    try {//  w  w w .ja  v a 2 s.  co  m
        Drawable drawable = AppCompatDrawableManager.get().getDrawable(context, res);
        drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
        return drawable.mutate();
    } catch (OutOfMemoryError e) {
        return null;
    }
}

From source file:org.opensilk.common.ui.util.ThemeUtils.java

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static void themeSeekBar2(SeekBar seekBar, int color) {
    if (VersionUtils.hasLollipop()) {
        seekBar.getProgressDrawable().setTint(color);
        seekBar.getThumb().setTint(color);
    } else {/*from   w ww . java2s  . c  o  m*/
        seekBar.getProgressDrawable().mutate().setColorFilter(color, PorterDuff.Mode.SRC_IN);
        if (VersionUtils.hasJellyBean()) {
            seekBar.getThumb().mutate().setColorFilter(color, PorterDuff.Mode.SRC_IN);
        } else if (seekBar instanceof CompatSeekBar) {
            Drawable thumb = ((CompatSeekBar) seekBar).getThumb();
            if (thumb != null) {
                thumb.mutate().setColorFilter(color, PorterDuff.Mode.SRC_IN);
            }
        }
    }
}

From source file:com.finchuk.clock2.timepickers.Utils.java

public static void setTint(Drawable drawable, @ColorInt int color) {
    drawable = DrawableCompat.wrap(drawable.mutate());
    DrawableCompat.setTint(drawable, color);
}

From source file:com.tr4android.support.extension.picker.PickerThemeUtils.java

public static Drawable getNavButtonBackground(Context context) {
    Drawable drawable = ContextCompat.getDrawable(context, R.drawable.date_picker_nav_background);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        // Proxy the highlight color into the background drawable
        drawable = DrawableCompat.wrap(drawable.mutate());
        DrawableCompat.setTintList(drawable, getNavButtonColorStateList(context));
    }/*from   w  w w . j a v a  2  s  . c om*/
    return drawable;
}

From source file:com.ofalvai.bpinfo.util.UiUtils.java

/**
 * Adds a rectangular icon for the affected route.
 *
 * First it creates a TextView, then sets the style properties of the view.
 * The custom colored rounded background is achieved by a Drawable and a ColorFilter on top of that.
 *///from  w ww .ja  v  a2 s.c  om
public static void addRouteIcon(Context context, @NonNull ViewGroup root, @NonNull Route route) {
    ContextThemeWrapper iconContextTheme = new ContextThemeWrapper(context, R.style.RouteIcon);
    TextView iconView = new TextView(iconContextTheme);

    iconView.setText(route.getShortName());
    iconView.setTextColor(route.getTextColor());
    iconView.setContentDescription(Utils.getContentDescriptionForRoute(context, route));
    root.addView(iconView);

    // Layout attributes defined in R.style.RouteIcon were ignored before attaching the view to
    // a parent, so we need to manually set them
    ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) iconView.getLayoutParams();
    params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
    int margin = (int) context.getResources().getDimension(R.dimen.route_icon_margin);
    params.rightMargin = margin;
    params.topMargin = margin;
    // A requestLayout() call is not necessary here because the setBackground() method below
    // will call that anyway.
    //iconView.requestLayout();

    // Setting a custom colored rounded background drawable as background
    Drawable iconBackground = context.getResources().getDrawable(R.drawable.rounded_corner_5dp);
    if (iconBackground != null) {
        ColorFilter colorFilter = new LightingColorFilter(Color.rgb(1, 1, 1), route.getColor());
        iconBackground.mutate().setColorFilter(colorFilter);
        iconView.setBackground(iconBackground);
    }
}

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

public static void tintViewDrawable(View view, Drawable drawable, TintInfo tint) {
    if (view == null || drawable == null)
        return;//from w w w .ja va 2  s . c  om
    if (tint.mHasTintList || tint.mHasTintMode) {
        drawable.mutate();
        if (drawable instanceof ColorDrawable) {
            ((ColorDrawable) drawable).setColor(ThemeUtils.replaceColor(view.getContext(), tint.mTintList
                    .getColorForState(view.getDrawableState(), tint.mTintList.getDefaultColor())));
        } else {
            drawable.setColorFilter(
                    createTintFilter(view.getContext(), tint.mHasTintList ? tint.mTintList : null,
                            tint.mHasTintMode ? tint.mTintMode : DEFAULT_MODE, view.getDrawableState()));
        }
    } else {
        drawable.clearColorFilter();
    }

    if (Build.VERSION.SDK_INT <= 23) {
        // On Gingerbread, GradientDrawable does not invalidate itself when it's ColorFilter
        // has changed, so we need to force an invalidation
        drawable.invalidateSelf();
    }
}

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

public static void tintViewBackground(View view, com.bilibili.magicasakura.utils.TintInfo tint) {
    Drawable background;
    if (view == null || (background = view.getBackground()) == null)
        return;/*from   w  w w  . ja  v a  2s .c  om*/

    if (tint.mHasTintList || tint.mHasTintMode) {
        background.mutate();
        if (background instanceof ColorDrawable) {
            ((ColorDrawable) background).setColor(ThemeUtils.replaceColor(view.getContext(), tint.mTintList
                    .getColorForState(view.getDrawableState(), tint.mTintList.getDefaultColor())));
        } else {
            background.setColorFilter(
                    createTintFilter(view.getContext(), tint.mHasTintList ? tint.mTintList : null,
                            tint.mHasTintMode ? tint.mTintMode : DEFAULT_MODE, view.getDrawableState()));
        }
    } else {
        background.clearColorFilter();
    }

    if (Build.VERSION.SDK_INT <= 23) {
        // On Gingerbread, GradientDrawable does not invalidate itself when it's ColorFilter
        // has changed, so we need to force an invalidation
        background.invalidateSelf();
    }
}

From source file:poche.fm.potunes.utils.TintManager.java

public static void tintViewBackground(View view, TintInfo tint) {
    Drawable background;
    if (view == null || (background = view.getBackground()) == null)
        return;//from   w  w  w  .  j a v a  2s  .  c o m

    if (tint.mHasTintList || tint.mHasTintMode) {
        background.mutate();
        if (background instanceof ColorDrawable) {
            ((ColorDrawable) background).setColor(ThemeUtils.replaceColor(view.getContext(), tint.mTintList
                    .getColorForState(view.getDrawableState(), tint.mTintList.getDefaultColor())));
        } else {
            background.setColorFilter(
                    createTintFilter(view.getContext(), tint.mHasTintList ? tint.mTintList : null,
                            tint.mHasTintMode ? tint.mTintMode : DEFAULT_MODE, view.getDrawableState()));
        }
    } else {
        background.clearColorFilter();
    }

    if (Build.VERSION.SDK_INT <= 23) {
        // On Gingerbread, GradientDrawable does not invalidate itself when it's ColorFilter
        // has changed, so we need to force an invalidation
        background.invalidateSelf();
    }
}

From source file:jahirfiquitiva.iconshowcase.utilities.utils.IconUtils.java

@CheckResult
@Nullable//from   w  ww. j a  va  2s.  c  o 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.nadmm.airports.utils.UiUtils.java

static public void setTextViewDrawable(TextView tv, int resid) {
    String key = String.format(Locale.US, "%d", resid);
    Drawable d = getDrawableFromCache(key);
    if (d == null) {
        Resources res = tv.getResources();
        d = ResourcesCompat.getDrawable(res, resid, null);
        putDrawableIntoCache(key, d);/*from   w  ww.  ja  v  a 2s. co  m*/
    }
    setTextViewDrawable(tv, d.mutate());
}