Java Code Examples for android.graphics.drawable.Drawable#invalidateSelf()

The following examples show how to use android.graphics.drawable.Drawable#invalidateSelf() . 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: TintManager.java    From timecat with Apache License 2.0 6 votes vote down vote up
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();
    }
}
 
Example 2
Source File: TintManager.java    From timecat with Apache License 2.0 6 votes vote down vote up
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();
    }
}
 
Example 3
Source File: AppCompatSwitchHelper.java    From timecat with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 4
Source File: DrawableHelper.java    From GetApk with MIT License 6 votes vote down vote up
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();
    }
}
 
Example 5
Source File: TintManager.java    From MagicaSakura with Apache License 2.0 6 votes vote down vote up
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();
    }
}
 
Example 6
Source File: TintManager.java    From MagicaSakura with Apache License 2.0 6 votes vote down vote up
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();
    }
}
 
Example 7
Source File: AppCompatSwitchHelper.java    From MagicaSakura with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 8
Source File: AppCompatImageHelper.java    From MagicaSakura with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 9
Source File: EmTintManager.java    From AndroidTint with Apache License 2.0 6 votes vote down vote up
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();
    }
}
 
Example 10
Source File: Images.java    From WhereYouGo with GNU General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 11
Source File: DynamicDrawableUtils.java    From dynamic-utils with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 12
Source File: GenericDraweeHierarchyTest.java    From fresco with MIT License 4 votes vote down vote up
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));
}
 
Example 13
Source File: DynamicDrawableUtils.java    From dynamic-utils with Apache License 2.0 3 votes vote down vote up
/**
 * 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;
}
 
Example 14
Source File: ThemeUtils.java    From TwistyTimer with GNU General Public License v3.0 3 votes vote down vote up
/**
 * 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;
}