下面列出了android.graphics.drawable.Drawable#invalidateSelf() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void tintViewBackground(View view, TintInfo tint) {
Drawable background;
if (view == null || (background = view.getBackground()) == null) return;
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();
}
}
public static void tintViewDrawable(View view, Drawable drawable, TintInfo tint) {
if (view == null || drawable == null) return;
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();
}
}
private boolean applySupportDrawableTint() {
Drawable drawable = mDrawableCallback.getDrawable();
if (drawable != null && mTintInfo != null && mTintInfo.mHasTintList) {
Drawable tintDrawable = drawable.mutate();
tintDrawable = DrawableCompat.wrap(tintDrawable);
if (mTintInfo.mHasTintList) {
DrawableCompat.setTintList(tintDrawable, mTintInfo.mTintList);
}
if (mTintInfo.mHasTintMode) {
DrawableCompat.setTintMode(tintDrawable, mTintInfo.mTintMode);
}
if (tintDrawable.isStateful()) {
tintDrawable.setState(mSwitchCompat.getDrawableState());
}
setDrawable(tintDrawable);
if (drawable == tintDrawable) {
tintDrawable.invalidateSelf();
}
return true;
}
return false;
}
public static void tintDrawable(@NonNull Drawable drawable, ColorStateList tintList, PorterDuff.Mode mode, int[] state) {
if (DrawableUtils.canSafelyMutateDrawable(drawable)
&& drawable.mutate() != drawable) {
Log.e(TAG, "Mutated drawable is not the same instance as the input.");
return;
}
if (tintList != null || mode != null) {
drawable.setColorFilter(createTintFilter(tintList, mode == null ? PorterDuff.Mode.SRC_IN : mode, state));
} else {
drawable.clearColorFilter();
}
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {
// Pre-v23 there is no guarantee that a state change will invoke an invalidation,
// so we force it ourselves
drawable.invalidateSelf();
}
}
public static void tintViewBackground(View view, TintInfo tint) {
Drawable background;
if (view == null || (background = view.getBackground()) == null) return;
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();
}
}
public static void tintViewDrawable(View view, Drawable drawable, TintInfo tint) {
if (view == null || drawable == null) return;
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();
}
}
private boolean applySupportDrawableTint() {
Drawable drawable = mDrawableCallback.getDrawable();
if (drawable != null && mTintInfo != null && mTintInfo.mHasTintList) {
Drawable tintDrawable = drawable.mutate();
tintDrawable = DrawableCompat.wrap(tintDrawable);
if (mTintInfo.mHasTintList) {
DrawableCompat.setTintList(tintDrawable, mTintInfo.mTintList);
}
if (mTintInfo.mHasTintMode) {
DrawableCompat.setTintMode(tintDrawable, mTintInfo.mTintMode);
}
if (tintDrawable.isStateful()) {
tintDrawable.setState(mSwitchCompat.getDrawableState());
}
setDrawable(tintDrawable);
if (drawable == tintDrawable) {
tintDrawable.invalidateSelf();
}
return true;
}
return false;
}
private boolean applySupportImageTint() {
Drawable image = mView.getDrawable();
if (image != null && mImageTintInfo != null && mImageTintInfo.mHasTintList) {
Drawable tintDrawable = image.mutate();
tintDrawable = DrawableCompat.wrap(tintDrawable);
if (mImageTintInfo.mHasTintList) {
DrawableCompat.setTintList(tintDrawable, mImageTintInfo.mTintList);
}
if (mImageTintInfo.mHasTintMode) {
DrawableCompat.setTintMode(tintDrawable, mImageTintInfo.mTintMode);
}
if (tintDrawable.isStateful()) {
tintDrawable.setState(mView.getDrawableState());
}
setImageDrawable(tintDrawable);
if (image == tintDrawable) {
tintDrawable.invalidateSelf();
}
return true;
}
return false;
}
public static void tintDrawable(Drawable drawable, TintInfo tint, int[] state) {
if (shouldMutateBackground(drawable) && drawable.mutate() != drawable) {
Log.d(TAG, "Mutated drawable is not the same instance as the input.");
return;
}
if (tint.mHasTintList || tint.mHasTintMode) {
drawable.setColorFilter(createTintFilter(
tint.mHasTintList ? tint.mTintList : null,
tint.mHasTintMode ? tint.mTintMode : DEFAULT_MODE,
state));
} else {
drawable.clearColorFilter();
}
if (Build.VERSION.SDK_INT <= 10) {
// On Gingerbread, GradientDrawable does not invalidate itself when it's
// ColorFilter has changed, so we need to force an invalidation
drawable.invalidateSelf();
}
}
public static Drawable getSizeOptimizedIcon(Drawable draw, int newSize) {
if (draw == null)
return getImageD(R.drawable.var_empty);
draw.setBounds(0, 0, newSize, newSize);
draw.invalidateSelf();
return draw;
}
/**
* Colorize and return the mutated drawable so that, all other references do not change.
*
* @param drawable The drawable to be colorized.
* @param color The color to colorize the drawable.
* @param wrap {@code true} to {@code wrap} the drawable so that it may be used for
* tinting across the different API levels.
* @param mode The porter duff mode.
*
* @return The drawable after applying the color filter.
*
* @see Drawable#setColorFilter(int, PorterDuff.Mode)
* @see PorterDuff.Mode
*/
public static @Nullable Drawable colorizeDrawable(@Nullable Drawable drawable,
boolean wrap, @ColorInt int color, @Nullable PorterDuff.Mode mode) {
if (drawable != null) {
if (mode == null) {
mode = PorterDuff.Mode.SRC_IN;
}
// Handle issue with layer drawables.
if (DynamicSdkUtils.is21(true)) {
if (wrap) {
drawable = drawable.mutate();
drawable.setColorFilter(color, mode);
}
} else {
if (wrap) {
drawable = DrawableCompat.wrap(drawable.mutate());
}
DrawableCompat.setTintMode(drawable, mode);
DrawableCompat.setTint(drawable, color);
}
drawable.invalidateSelf();
}
return drawable;
}
private void verifyCallback(Drawable parent, Drawable child) {
Drawable.Callback callback = mock(Drawable.Callback.class);
parent.setCallback(callback);
child.invalidateSelf();
verify(callback).invalidateDrawable(any(Drawable.class));
}
/**
* Apply color filter and return the mutated drawable so that, all other references
* do not change.
*
* @param drawable The drawable to be colorized.
* @param wrap {@code true} to {@code wrap} the drawable so that it may be used for
* tinting across the different API levels.
* @param colorFilter The color filter to be applied on the drawable.
*
* @return The drawable after applying the color filter.
*
* @see Drawable#setColorFilter(ColorFilter)
* @see ColorFilter
*/
public static @Nullable Drawable colorizeDrawable(@Nullable Drawable drawable,
boolean wrap, @NonNull ColorFilter colorFilter) {
if (drawable != null) {
if (wrap) {
drawable = drawable.mutate();
}
drawable.setColorFilter(colorFilter);
drawable.invalidateSelf();
}
return drawable;
}
/**
* Fetches a drawable from a resource id (can be a vector drawable),
* tints with {@param colorAttrRes} and returns it.
* @param context {@link Context}
* @param drawableRes resource id for the drawable
* @param colorAttrRes attr res id for the tint
* @return a tinted drawable
*/
public static Drawable fetchTintedDrawable(Context context, @DrawableRes int drawableRes, @AttrRes int colorAttrRes) {
Drawable drawable = DrawableCompat.wrap(ContextCompat.getDrawable(context, drawableRes)).mutate();
DrawableCompat.setTint(drawable, ThemeUtils.fetchAttrColor(context, colorAttrRes));
drawable.invalidateSelf();
return drawable;
}