android.graphics.drawable.Drawable#invalidateSelf()源码实例Demo

下面列出了android.graphics.drawable.Drawable#invalidateSelf() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: timecat   文件: TintManager.java
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();
    }
}
 
源代码2 项目: timecat   文件: TintManager.java
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();
    }
}
 
源代码3 项目: timecat   文件: AppCompatSwitchHelper.java
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;
}
 
源代码4 项目: GetApk   文件: DrawableHelper.java
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();
    }
}
 
源代码5 项目: MagicaSakura   文件: TintManager.java
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();
    }
}
 
源代码6 项目: MagicaSakura   文件: TintManager.java
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();
    }
}
 
源代码7 项目: MagicaSakura   文件: AppCompatSwitchHelper.java
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;
}
 
源代码8 项目: MagicaSakura   文件: AppCompatImageHelper.java
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;
}
 
源代码9 项目: AndroidTint   文件: EmTintManager.java
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();
    }
}
 
源代码10 项目: WhereYouGo   文件: Images.java
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;
}
 
源代码11 项目: dynamic-utils   文件: DynamicDrawableUtils.java
/**
 * 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;
}
 
源代码12 项目: fresco   文件: GenericDraweeHierarchyTest.java
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));
}
 
源代码13 项目: dynamic-utils   文件: DynamicDrawableUtils.java
/**
 * 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;
}
 
源代码14 项目: TwistyTimer   文件: ThemeUtils.java
/**
 * 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;
}