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

@Nullable
public static Drawable getTintedDrawable(@NonNull Context context, @DrawableRes int res, @ColorInt int color) {
    try {/*from  ww  w .ja va 2 s .c  om*/
        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:com.dm.material.dashboard.candybar.helpers.DrawableHelper.java

@Nullable
public static Drawable getDefaultImage(@NonNull Context context, @DrawableRes int res) {
    try {/*from w w  w . j a v  a2s.  com*/
        int color = ColorHelper.getAttributeColor(context, android.R.attr.textColorSecondary);
        int padding = context.getResources().getDimensionPixelSize(R.dimen.default_image_padding);

        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.popdeem.sdk.uikit.utils.PDUIColorUtils.java

public static Drawable getTintedDrawable(Context context, @DrawableRes int drawableRes, @ColorRes int colorRes,
        boolean mutate) {
    Drawable drawable = ContextCompat.getDrawable(context, drawableRes);
    if (drawable == null) {
        return null;
    }//  ww  w . j  a va2 s  .  c  o m

    if (mutate) {
        drawable = drawable.mutate();
    }
    drawable.setColorFilter(ContextCompat.getColor(context, colorRes), PorterDuff.Mode.SRC_IN);
    return drawable;
}

From source file:io.mpos.ui.shared.util.UiHelper.java

public static void setActionbarWithCustomColors(AppCompatActivity activity, Toolbar toolbar) {
    if (toolbar != null) {
        setupUpNavigation(activity, toolbar);
        activity.setSupportActionBar(toolbar);

        int color = MposUi.getInitializedInstance().getConfiguration().getAppearance().getTextColorPrimary();
        toolbar.setTitleTextColor(color);

        final Drawable navigationDrawable = toolbar.getNavigationIcon();
        if (navigationDrawable != null) {
            navigationDrawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
            toolbar.setNavigationIcon(navigationDrawable);
        }//from w  w  w. ja  v  a  2  s .com

        color = MposUi.getInitializedInstance().getConfiguration().getAppearance().getColorPrimary();
        toolbar.setBackgroundColor(color);
    }

    if (Build.VERSION.SDK_INT >= 21) {
        int color = MposUi.getInitializedInstance().getConfiguration().getAppearance().getColorPrimaryDark();
        activity.getWindow().setStatusBarColor(color);
    }
}

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

@CheckResult
@Nullable//  w w  w  .ja  v  a  2  s .  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:Main.java

public static int PutImageTargetHeightColor(Canvas canvas, Drawable image, int x, int y, int height, int color,
        Mode porterDuff) {/*from ww  w  . j  a v a  2  s  .  c o  m*/

    float scale = (float) height / (float) image.getIntrinsicHeight();
    int width = (int) Math.round(image.getIntrinsicWidth() * scale);

    Rect oldBounds = image.getBounds();
    image.setBounds(x, y, x + width, y + height);
    image.setColorFilter(color, porterDuff);
    image.draw(canvas);
    image.setBounds(oldBounds);
    image.clearColorFilter();
    return width;
}

From source file:com.owncloud.android.utils.ThemeUtils.java

public static Drawable tintDrawable(Drawable drawable, int color) {
    if (drawable != null) {
        Drawable wrap = DrawableCompat.wrap(drawable);
        wrap.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);

        return wrap;
    } else {/*w  w  w.ja v  a2s  .  c  om*/
        return drawable;
    }
}

From source file:Main.java

public static Bitmap flattenExtensionIcon(Drawable baseIcon, int color) {
    if (baseIcon == null) {
        return null;
    }//from w w w .ja v a2s. com

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

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

@CheckResult
@Nullable/* w  w  w.  j a  v a 2  s .  com*/
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:Main.java

private static Drawable recolor(Resources res, Drawable drawable, int color) {
    if (drawable == null) {
        return null;
    }/*from w  w  w .  ja  va 2  s.  c o m*/

    Bitmap outBitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
            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 new BitmapDrawable(res, outBitmap);
}