Example usage for android.graphics.drawable Drawable setColorFilter

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

Introduction

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

Prototype

public void setColorFilter(@ColorInt int color, @NonNull PorterDuff.Mode mode) 

Source Link

Document

Specify a color and Porter-Duff mode to be the color filter for this drawable.

Usage

From source file:Main.java

public static void changeDrawableColor(Drawable drawable, int color) {
    if (drawable != null) {
        drawable.mutate();/*  w  ww.  ja v  a 2 s  .com*/
        drawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
    }
}

From source file:Main.java

private static void brandGlowDrawableColor(final Resources resources, final String drawable, final int color) {
    final int drawableRes = resources.getIdentifier(drawable, "drawable", "android");
    try {/*from   ww  w. j av a 2s.c  o  m*/
        final Drawable d = resources.getDrawable(drawableRes);
        if (d != null) {
            d.setColorFilter(color, PorterDuff.Mode.SRC_IN);
        }
    } catch (final Resources.NotFoundException rnfe) {
        Log.e(LOG_TAG, "Failed to find drawable for resource " + drawable);
    }
}

From source file:com.mindorks.framework.mvp.utils.ViewUtils.java

public static void changeIconDrawableToGray(Context context, Drawable drawable) {
    if (drawable != null) {
        drawable.mutate();//  w  w w .j a  v a 2  s  .  com
        drawable.setColorFilter(ContextCompat.getColor(context, R.color.dark_gray), PorterDuff.Mode.SRC_ATOP);
    }
}

From source file:com.scrachx.foodfacts.checker.utils.ViewUtils.java

public static void changeIconDrawableToGray(Context context, Drawable drawable) {
    if (drawable != null) {
        drawable.mutate();//ww  w . j  a  v  a2 s. c o  m
        drawable.setColorFilter(
                ContextCompat.getColor(context, com.scrachx.foodfacts.checker.R.color.dark_gray),
                PorterDuff.Mode.SRC_ATOP);
    }
}

From source file:com.stepstone.stepper.internal.util.TintUtil.java

/**
 * Tints a drawable with the provided color
 * @param drawable drawable to tint//  w w  w  .ja  v  a 2  s.c  o m
 * @param color tint color
 * @return tinted drawable
 */
public static Drawable tintDrawable(@Nullable Drawable drawable, @ColorInt int color) {
    if (drawable != null) {
        drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
    }
    return drawable;
}

From source file:org.tasks.ui.MenuColorizer.java

private static Drawable colorDrawable(Drawable drawable, int color) {
    if (drawable != null) {
        drawable.mutate();/*from   w  w w . ja  v a 2  s  .co  m*/
        drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
    }
    return drawable;
}

From source file:com.dm.wallpaper.board.helpers.DrawableHelper.java

@Nullable
public static Drawable getDefaultImage(@NonNull Context context, @DrawableRes int res, @ColorInt int color,
        int padding) {
    try {/*w  w w.  j  av  a  2  s  .  c  o  m*/
        Drawable drawable = AppCompatDrawableManager.get().getDrawable(context, res);
        drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            drawable = (DrawableCompat.wrap(drawable)).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);

        Bitmap tintedBitmap = Bitmap.createBitmap(bitmap.getWidth() + padding, bitmap.getHeight() + padding,
                Bitmap.Config.ARGB_8888);
        Canvas tintedCanvas = new Canvas(tintedBitmap);
        int background = ColorHelper.getAttributeColor(context, R.attr.card_background);
        Paint paint = new Paint();
        paint.setFilterBitmap(true);
        paint.setAntiAlias(true);
        tintedCanvas.drawColor(background, PorterDuff.Mode.ADD);
        tintedCanvas.drawBitmap(bitmap, (tintedCanvas.getWidth() - bitmap.getWidth()) / 2,
                (tintedCanvas.getHeight() - bitmap.getHeight()) / 2, paint);
        return new BitmapDrawable(context.getResources(), tintedBitmap);
    } catch (Exception | OutOfMemoryError e) {
        return null;
    }
}

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

private static Bitmap getDarkIcon(Context context, int drawableId) {
    Drawable drawable = ContextCompat.getDrawable(context, drawableId);
    if (drawable != null) {
        drawable.setColorFilter(0xFF616161, PorterDuff.Mode.MULTIPLY);
        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);//from  w w  w.j av a  2s  .c  om
        return bitmap;
    } else {
        throw new IllegalArgumentException("unsupported drawable type");
    }
}

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

/**
 * Changes the text selection handle colors.
 *//*from   w  w  w  . jav a 2s  .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.
 */// ww w. jav a  2s .co 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) {
    }
}