Java Code Examples for androidx.core.graphics.drawable.DrawableCompat#setTintMode()

The following examples show how to use androidx.core.graphics.drawable.DrawableCompat#setTintMode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TextInputLayout.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
private void applyIconTint(
    @NonNull CheckableImageButton iconView,
    boolean hasIconTintList,
    ColorStateList iconTintList,
    boolean hasIconTintMode,
    PorterDuff.Mode iconTintMode) {
  Drawable icon = iconView.getDrawable();
  if (icon != null && (hasIconTintList || hasIconTintMode)) {
    icon = DrawableCompat.wrap(icon).mutate();

    if (hasIconTintList) {
      DrawableCompat.setTintList(icon, iconTintList);
    }
    if (hasIconTintMode) {
      DrawableCompat.setTintMode(icon, iconTintMode);
    }
  }

  if (iconView.getDrawable() != icon) {
    iconView.setImageDrawable(icon);
  }
}
 
Example 2
Source File: RangeSeekBar.java    From RangeSeekBar with MIT License 6 votes vote down vote up
private void applyThumbTintInternal(final WhichThumb which) {
    Drawable thumb = which == WhichThumb.Start ? mThumbStart : mThumbEnd;

    if (thumb != null && (mHasThumbTint || mHasThumbTintMode)) {

        if (which == WhichThumb.Start) {
            mThumbStart = thumb.mutate();
            thumb = mThumbStart;
        } else {
            mThumbEnd = thumb.mutate();
            thumb = mThumbEnd;
        }

        if (mHasThumbTint) {
            DrawableCompat.setTintList(thumb, mThumbTintList);
        }

        if (mHasThumbTintMode) {
            DrawableCompat.setTintMode(thumb, mThumbTintMode);
        }

        if (thumb.isStateful()) {
            thumb.setState(getDrawableState());
        }
    }
}
 
Example 3
Source File: ThemeUtils.java    From TwistyTimer with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Tints a drawable with {@param colorAttrRes} and returns it.
 * @param context {@link Context}
 * @param drawableRes drawableRes to be tinted
 * @param colorAttrRes attr res id for the tint
 * @return a tinted drawable
 */
public static Drawable tintDrawable(Context context, @DrawableRes int drawableRes, @AttrRes int colorAttrRes) {
    Drawable drawable = AppCompatResources.getDrawable(context, drawableRes).mutate();
    Drawable wrap = DrawableCompat.wrap(drawable).mutate();
    DrawableCompat.setTint(wrap, ThemeUtils.fetchAttrColor(context, colorAttrRes));
    DrawableCompat.setTintMode(wrap, PorterDuff.Mode.SRC_IN);

    return wrap;
}
 
Example 4
Source File: CrossHintFaceSelectDialog.java    From TwistyTimer with GNU General Public License v3.0 5 votes vote down vote up
private void setColor(View view, int color) {
    Drawable drawable = ContextCompat.getDrawable(getContext(), R.drawable.square);
    Drawable wrap = DrawableCompat.wrap(drawable);
    DrawableCompat.setTint(wrap, color);
    DrawableCompat.setTintMode(wrap, PorterDuff.Mode.MULTIPLY);
    wrap = wrap.mutate();
    view.setBackground(wrap);
}
 
Example 5
Source File: MaterialButtonHelper.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
void setSupportBackgroundTintMode(@Nullable Mode mode) {
  if (backgroundTintMode != mode) {
    backgroundTintMode = mode;
    if (getMaterialShapeDrawable() != null && backgroundTintMode != null) {
      DrawableCompat.setTintMode(getMaterialShapeDrawable(), backgroundTintMode);
    }
  }
}
 
Example 6
Source File: BaseTransientBottomBar.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
@Override
public void setBackgroundTintList(@Nullable ColorStateList backgroundTint) {
  this.backgroundTint = backgroundTint;
  if (getBackground() != null) {
    Drawable wrappedBackground = DrawableCompat.wrap(getBackground().mutate());
    DrawableCompat.setTintList(wrappedBackground, backgroundTint);
    DrawableCompat.setTintMode(wrappedBackground, backgroundTintMode);
    if (wrappedBackground != getBackground()) {
      super.setBackgroundDrawable(wrappedBackground);
    }
  }
}
 
Example 7
Source File: BaseTransientBottomBar.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
@Override
public void setBackgroundDrawable(@Nullable Drawable drawable) {
  if (drawable != null && backgroundTint != null) {
    drawable = DrawableCompat.wrap(drawable.mutate());
    DrawableCompat.setTintList(drawable, backgroundTint);
    DrawableCompat.setTintMode(drawable, backgroundTintMode);
  }
  super.setBackgroundDrawable(drawable);
}
 
Example 8
Source File: Preference.java    From AndroidMaterialPreferences with Apache License 2.0 5 votes vote down vote up
/**
 * Adapts the tint of the preference's icon.
 */
private void adaptIconTint() {
    Drawable icon = getIcon();

    if (icon != null) {
        DrawableCompat.setTintList(icon, tintList);
        DrawableCompat.setTintMode(icon, tintMode);
    }
}
 
Example 9
Source File: DrawableUtil.java    From UIWidget with Apache License 2.0 5 votes vote down vote up
public static Drawable setTintMode(Drawable drawable, @NonNull PorterDuff.Mode tintMode, int color) {
    if (drawable != null && tintMode != null) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            try {
                DrawableCompat.setTintMode(drawable, tintMode);
            } catch (Exception e) {
                drawable.setColorFilter(color, tintMode);
            }
        } else {
            drawable.setColorFilter(color, tintMode);
        }
    }
    return drawable;
}
 
Example 10
Source File: ArrayRecyclerViewAdapter.java    From AndroidMaterialDialog with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the icon of a list item.
 *
 * @param context  The context, which should be used, as an instance of the class
 *                 {@link Context}. The context may not be null
 * @param position The position of the list item as an {@link Integer} value
 * @return The icon of the list item as an instance of the class {@link Drawable} or null, if
 * the list item does not have an icon
 */
@Nullable
private Drawable getIcon(@NonNull final Context context, final int position) {
    Drawable icon = ActivityCompat.getDrawable(context, iconResourceIds[position]);

    if (icon != null) {
        DrawableCompat.setTintList(icon, itemTintList);
        DrawableCompat.setTintMode(icon, itemTintMode);
    }

    return icon;
}
 
Example 11
Source File: TintHelper.java    From a with GNU General Public License v3.0 5 votes vote down vote up
@CheckResult
@Nullable
public static Drawable createTintedDrawable(@Nullable Drawable drawable, @ColorInt int color) {
    if (drawable == null) return null;
    drawable = DrawableCompat.wrap(drawable.mutate());
    DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_IN);
    DrawableCompat.setTint(drawable, color);
    return drawable;
}
 
Example 12
Source File: BaseTransientBottomBar.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
@Override
public void setBackgroundTintMode(@Nullable PorterDuff.Mode backgroundTintMode) {
  this.backgroundTintMode = backgroundTintMode;
  if (getBackground() != null) {
    Drawable wrappedBackground = DrawableCompat.wrap(getBackground().mutate());
    DrawableCompat.setTintMode(wrappedBackground, backgroundTintMode);
    if (wrappedBackground != getBackground()) {
      super.setBackgroundDrawable(wrappedBackground);
    }
  }
}
 
Example 13
Source File: AppCompat.java    From HaoReader with GNU General Public License v3.0 5 votes vote down vote up
public static void setTint(Drawable drawable, @ColorInt int tint, @NonNull PorterDuff.Mode tintMode) {
    if (drawable == null) return;
    final Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
    wrappedDrawable.mutate();
    DrawableCompat.setTint(wrappedDrawable, tint);
    DrawableCompat.setTintMode(wrappedDrawable, tintMode);
}
 
Example 14
Source File: AppCompat.java    From HaoReader with GNU General Public License v3.0 5 votes vote down vote up
public static void setTintList(Drawable drawable, ColorStateList tint, @NonNull PorterDuff.Mode tintMode) {
    if (drawable == null) return;
    final Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
    wrappedDrawable.mutate();
    DrawableCompat.setTintList(wrappedDrawable, tint);
    DrawableCompat.setTintMode(wrappedDrawable, tintMode);
}
 
Example 15
Source File: DrawableHelper.java    From GetApk with MIT License 5 votes vote down vote up
public static Drawable tintDrawable(@NonNull Drawable drawable, ColorStateList tintList, PorterDuff.Mode tintMode) {
    if (tintList != null) {
        // First mutate the Drawable, then wrap it and set the tint list
        if (DrawableUtils.canSafelyMutateDrawable(drawable)) {
            drawable = drawable.mutate();
        }
        drawable = DrawableCompat.wrap(drawable);
        DrawableCompat.setTintList(drawable, tintList);

        if (tintMode != null) {
            DrawableCompat.setTintMode(drawable, tintMode);
        }
    }
    return drawable;
}
 
Example 16
Source File: MaterialButton.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
/**
 * Updates the icon, icon tint, and icon tint mode for this button.
 * @param needsIconReset Whether to force the drawable to be set
 */
private void updateIcon(boolean needsIconReset) {
  if (icon != null) {
    icon = DrawableCompat.wrap(icon).mutate();
    DrawableCompat.setTintList(icon, iconTint);
    if (iconTintMode != null) {
      DrawableCompat.setTintMode(icon, iconTintMode);
    }

    int width = iconSize != 0 ? iconSize : icon.getIntrinsicWidth();
    int height = iconSize != 0 ? iconSize : icon.getIntrinsicHeight();
    icon.setBounds(iconLeft, iconTop, iconLeft + width, iconTop + height);
  }

  // Forced icon update
  if (needsIconReset) {
    resetIconDrawable();
    return;
  }

  // Otherwise only update if the icon or the position has changed
  Drawable[] existingDrawables = TextViewCompat.getCompoundDrawablesRelative(this);
  Drawable drawableStart = existingDrawables[0];
  Drawable drawableTop = existingDrawables[1];
  Drawable drawableEnd = existingDrawables[2];
  boolean hasIconChanged =
      (isIconStart() && drawableStart != icon)
          || (isIconEnd() && drawableEnd != icon)
          || (isIconTop() && drawableTop != icon);

  if (hasIconChanged) {
    resetIconDrawable();
  }
}
 
Example 17
Source File: SchemeSelectDialogMain.java    From TwistyTimer with GNU General Public License v3.0 5 votes vote down vote up
private void setColor(View view, int color) {
    Drawable drawable = ContextCompat.getDrawable(getContext(), R.drawable.square);
    Drawable wrap = DrawableCompat.wrap(drawable);
    DrawableCompat.setTint(wrap, color);
    DrawableCompat.setTintMode(wrap, PorterDuff.Mode.MULTIPLY);
    wrap = wrap.mutate();
    view.setBackground(wrap);
}
 
Example 18
Source File: PaddingDrawable.java    From material with Apache License 2.0 4 votes vote down vote up
@Override
public void setTintMode(PorterDuff.Mode tintMode) {
    if(mDrawable != null)
        DrawableCompat.setTintMode(mDrawable, tintMode);
}
 
Example 19
Source File: DrawableContainerCompat.java    From MaterialProgressBar with Apache License 2.0 4 votes vote down vote up
/**
 * Initializes a drawable for display in this container.
 *
 * @param d The drawable to initialize.
 */
private void initializeDrawableForDisplay(Drawable d) {
    if (mBlockInvalidateCallback == null) {
        mBlockInvalidateCallback = new BlockInvalidateCallback();
    }
    // Temporary fix for suspending callbacks during initialization. We
    // don't want any of these setters causing an invalidate() since that
    // may call back into DrawableContainer.
    d.setCallback(mBlockInvalidateCallback.wrap(d.getCallback()));
    try {
        if (mDrawableContainerState.mEnterFadeDuration <= 0 && mHasAlpha) {
            d.setAlpha(mAlpha);
        }
        if (mDrawableContainerState.mHasColorFilter) {
            // Color filter always overrides tint.
            d.setColorFilter(mDrawableContainerState.mColorFilter);
        } else {
            if (mDrawableContainerState.mHasTintList) {
                DrawableCompat.setTintList(d, mDrawableContainerState.mTintList);
            }
            if (mDrawableContainerState.mHasTintMode) {
                DrawableCompat.setTintMode(d, mDrawableContainerState.mTintMode);
            }
        }
        d.setVisible(isVisible(), true);
        d.setDither(mDrawableContainerState.mDither);
        d.setState(getState());
        d.setLevel(getLevel());
        d.setBounds(getBounds());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            d.setLayoutDirection(getLayoutDirection());
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            d.setAutoMirrored(mDrawableContainerState.mAutoMirrored);
        }
        final Rect hotspotBounds = mHotspotBounds;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && hotspotBounds != null) {
            d.setHotspotBounds(hotspotBounds.left, hotspotBounds.top,
                    hotspotBounds.right, hotspotBounds.bottom);
        }
    } finally {
        d.setCallback(mBlockInvalidateCallback.unwrap());
    }
}
 
Example 20
Source File: DrawableWrapper.java    From MHViewer with Apache License 2.0 4 votes vote down vote up
public void setTintMode(@NonNull PorterDuff.Mode tintMode) {
  DrawableCompat.setTintMode(this.mDrawable, tintMode);
}