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:com.nttec.everychan.ui.theme.ThemeUtils.java

/**
 *   (drawable) ? Action Bar.  ? Android 5.0    ??   android:attr/textColorPrimary
 * @param theme /* w  w  w.  j a  v  a 2  s. co  m*/
 * @param resources ??
 * @param attrId id  (R.attr.[...])
 * @return  Drawable ? ,  null, ?  ? 
 */
public static Drawable getActionbarIcon(Theme theme, Resources resources, int attrId) {
    try {
        int id = getThemeResId(theme, attrId);
        Drawable drawable = ResourcesCompat.getDrawable(resources, id, theme);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            int color = getThemeColor(theme, android.R.attr.textColorPrimary, Color.TRANSPARENT);
            if (color != Color.TRANSPARENT) {
                drawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
            }
        }
        return drawable;
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static Bitmap recolorBitmap(Drawable drawable, int color) {
    if (drawable == null) {
        return null;
    }/*from w ww. ja v  a2 s  .  c  om*/

    int width = drawable.getIntrinsicWidth();
    int height = drawable.getIntrinsicHeight();
    if (width <= 0 || height <= 0) {
        return Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
    }

    Bitmap outBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(outBitmap);
    drawable.setBounds(0, 0, outBitmap.getWidth(), outBitmap.getHeight());
    drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
    drawable.draw(canvas);
    drawable.setColorFilter(null);
    drawable.setCallback(null); // free up any references
    return outBitmap;
}

From source file:de.grobox.liberario.utils.TransportrUtils.java

static public Drawable getTintedDrawable(Context context, boolean dark, Drawable drawable) {
    if (dark) {//from   w w w. j a  v  a2 s  .c  om
        drawable.setColorFilter(ContextCompat.getColor(context, R.color.drawableTintDark),
                PorterDuff.Mode.SRC_IN);
    } else {
        drawable.setColorFilter(ContextCompat.getColor(context, R.color.drawableTintLight),
                PorterDuff.Mode.SRC_IN);
    }
    return drawable.mutate();
}

From source file:de.grobox.liberario.utils.TransportrUtils.java

static private Drawable getToolbarDrawable(Context context, Drawable drawable) {
    if (drawable != null) {
        drawable.setColorFilter(ContextCompat.getColor(context, R.color.drawableTintDark),
                PorterDuff.Mode.SRC_IN);
        drawable.setAlpha(255);//from  w w  w .  j  a  v  a2 s . c o m
        drawable.mutate();
    }
    return drawable;
}

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.
 *//* w  w w  .j  a v a 2 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);
        }
    }
}

From source file:com.weebly.opus1269.copyeverywhere.ui.shared.MenuTint.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.
 * @param alpha    The alpha value (0...255) to set on the icon or {@code null} for no changes.
 *//*from   w ww . j a v a 2 s  .  com*/
public static void colorMenuItem(MenuItem menuItem, Integer color, Integer alpha) {
    if (color == null && alpha == 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();
        if (color != null) {
            drawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
        }
        if (alpha != null) {
            drawable.setAlpha(alpha);
        }
    }
}

From source file:com.mobicage.rogerthat.util.ui.UIUtils.java

private static void colorEditText(EditText view, String fieldNameRes, String fieldNameDrawable, int color,
        boolean isArray) {
    try {//from  w w  w.j  av a  2 s.  c  o  m
        // Get the cursor resource id
        final Field field = TextView.class.getDeclaredField(fieldNameRes);
        field.setAccessible(true);
        int drawableCursorResId = field.getInt(view);

        // Get the editor
        final Field editor = TextView.class.getDeclaredField("mEditor");
        editor.setAccessible(true);
        Object cursorEditor = editor.get(view);

        // Get the drawable and set a color filter
        Drawable drawable = ContextCompat.getDrawable(view.getContext(), drawableCursorResId);
        drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);

        // Set the drawables
        final Field drawableField = cursorEditor.getClass().getDeclaredField(fieldNameDrawable);
        drawableField.setAccessible(true);
        if (isArray) {
            Drawable[] drawables = { drawable, drawable };
            drawableField.set(cursorEditor, drawables);
        } else {
            drawableField.set(cursorEditor, drawable);
        }
    } catch (IllegalAccessException e) {
        L.bug(e);
    } catch (NoSuchFieldException e) {
        L.e("NoSuchFieldException for " + fieldNameRes, e);
    }
}

From source file:com.mobicage.rogerthat.util.ui.UIUtils.java

public static void setColors(int color, View... views) {
    if (Build.VERSION.SDK_INT >= 27) {
        return;/* w w w.  j a va2  s. c o  m*/
    }
    for (View view : views) {
        if (view instanceof CheckBox) {
            CheckBox checkbox = (CheckBox) view;
            CompoundButtonCompat.setButtonTintList(checkbox, ColorStateList.valueOf(color));
        } else if (view instanceof FloatingActionButton) {
            //noinspection RedundantCast
            ((FloatingActionButton) view).setBackgroundTintList(ColorStateList.valueOf(color));
        } else if (view instanceof Button || view instanceof ImageButton) {
            Drawable background = view.getBackground();
            if (background != null) {
                background.setColorFilter(color, PorterDuff.Mode.SRC_IN);
            }
        } else if (view instanceof TextInputLayout) {
            UIUtils.colorTextInputLayout((TextInputLayout) view, color);
            // TODO: EditText's who are a child of TextInputLayout their line isn't colored correctly
        } else if (view instanceof EditText) {
            EditText editText = (EditText) view;
            editText.setHighlightColor(color); // When selecting text
            editText.setHintTextColor(color); // Text for the  hint message
            // Line under the textfield
            Drawable background = editText.getBackground();
            if (background != null) {
                DrawableCompat.setTint(background, color);
                editText.setBackground(background);
            }
            UIUtils.setCursorColor(editText, color);
        } else if (view instanceof CheckedTextView) {
            CheckedTextView ctv = (CheckedTextView) view;
            Drawable d = ctv.getCheckMarkDrawable();
            d.setColorFilter(color, PorterDuff.Mode.SRC_IN);
        } else if (view instanceof TextView) {
            ((TextView) view).setLinkTextColor(color);
        } else if (view instanceof SeekBar) {
            SeekBar sb = (SeekBar) view;
            Drawable progress = sb.getProgressDrawable();
            if (progress != null) {
                progress.setColorFilter(color, PorterDuff.Mode.SRC_IN);
            }
            Drawable thumb = sb.getThumb();
            if (thumb != null) {
                thumb.setColorFilter(color, PorterDuff.Mode.SRC_IN);
            }
        } else if (view instanceof ProgressBar) {
            ((ProgressBar) view).getIndeterminateDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
        } else {
            L.d("Not coloring view: " + view.toString());
        }
    }
}

From source file:org.catrobat.paintroid.dialog.IndeterminateProgressDialog.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.custom_progress_dialog);

    // Remove this section once AppCompat supports tinting Progressbars
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
        if (progressBar != null) {
            Drawable drawable = progressBar.getIndeterminateDrawable();
            drawable.setColorFilter(ContextCompat.getColor(getContext(), R.color.tools_text_color),
                    PorterDuff.Mode.SRC_IN);
        }/*  w  w w .  j a v  a 2  s  . com*/
    }

    setCancelable(false);
}

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

/**
 * Sets the color on the {@code view}'s {@code selectableItemBackground} or the
 * borderless variant, whichever was set as the background.
 * @param view the view that should have its highlight color changed
 *///from   ww w . j a  va2 s. c  o  m
public static void setColorControlHighlight(@NonNull View view, @ColorInt int color) {
    Drawable selectableItemBackground = view.getBackground();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
            && selectableItemBackground instanceof RippleDrawable) {
        ((RippleDrawable) selectableItemBackground).setColor(ColorStateList.valueOf(color));
    } else {
        // Draws the color (src) onto the background (dest) *in the same plane*.
        // That means the color is not overlapping (i.e. on a higher z-plane, covering)
        // the background. That would be done with SRC_OVER.
        // The DrawableCompat tinting APIs *could* be a viable alternative, if you
        // call setTintMode(). Previous attempts using those APIs failed without
        // the tint mode. However, those APIs have the overhead of mutating and wrapping
        // the drawable.
        selectableItemBackground.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
    }
}