下面列出了android.graphics.drawable.Drawable#mutate() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/** Update the color of a specific menu item to the given color. */
private void updateMenuItemIconColor(Menu menu, int itemId, int color) {
MenuItem menuItem = menu.findItem(itemId);
if (menuItem != null) {
Drawable menuItemIcon = menuItem.getIcon();
if (menuItemIcon != null) {
try {
menuItemIcon.mutate();
menuItemIcon.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
menuItem.setIcon(menuItemIcon);
} catch (Exception e) {
Log.w("AIC", "Failed to update menu item color", e);
}
}
}
}
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();
}
}
private void resetDrawable(Drawable drawable, CompoundSVGParameter svgParameter) {
if (drawable != null && drawable instanceof SVGDrawable) {
drawable.mutate();
((SVGDrawable)drawable).setTintList(svgParameter.svgColor);
if (svgParameter.svgAlpha > 0 && svgParameter.svgAlpha <= 1.0f) {
((SVGDrawable)drawable).setAlpha((int) (svgParameter.svgAlpha * 0xFF));
}
if (svgParameter.svgWidth > 0) {
((SVGDrawable)drawable).setWidth(svgParameter.svgWidth);
}
if (svgParameter.svgHeight > 0) {
((SVGDrawable)drawable).setHeight(svgParameter.svgHeight);
}
if (svgParameter.svgRotation != 0) {
((SVGDrawable)drawable).setRotation(svgParameter.svgRotation);
}
}
}
@Nullable
protected Drawable onCreateActionIcon(@NonNull Drawable icon) {
int size = getResources().getDimensionPixelSize(R.dimen.notification_action_icon_size);
icon = icon.mutate();
icon.setBounds(0, 0, size, size);
// The matrix is stored in a single array, and its treated as follows:
// [ a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t ]
// When applied to a color [r, g, b, a], the resulting color is computed as (after clamping)
// R' = a*R + b*G + c*B + d*A + e;
// G' = f*R + g*G + h*B + i*A + j;
// B' = k*R + l*G + m*B + n*A + o;
// A' = p*R + q*G + r*B + s*A + t;
ColorFilter colorFilter = new ColorMatrixColorFilter(new float[]{
0, 0, 0, 0, 255, // Red
0, 0, 0, 0, 255, // Green
0, 0, 0, 0, 255, // Blue
0, 0, 0, 1, 0 // Alpha
});
icon.setColorFilter(colorFilter); // force white color
return icon;
}
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 void updateViews()
{
if (edit != null)
{
edit.setText( String.format("#%08X", color) );
edit.setVisibility((isCollapsed ? View.GONE : View.VISIBLE));
}
if (button != null)
{
Drawable d = button.getDrawable();
if (d != null)
{
GradientDrawable g = (GradientDrawable) d.mutate();
g.setColor(color);
g.invalidateSelf();
}
}
}
/**
* Adds the drawable to the end of the list of contained drawables.
*
* @param dr the drawable to add
* @return the position of the drawable within the container
*/
public final int addChild(Drawable dr) {
final int pos = mNumChildren;
if (pos >= mDrawables.length) {
growArray(pos, pos + 10);
}
dr.mutate();
dr.setVisible(false, true);
dr.setCallback(mOwner);
mDrawables[pos] = dr;
mNumChildren++;
mChildrenChangingConfigurations |= dr.getChangingConfigurations();
invalidateCache();
mConstantPadding = null;
mCheckedPadding = false;
mCheckedConstantSize = false;
mCheckedConstantState = false;
return pos;
}
@SuppressLint("PrivateApi")
@SuppressWarnings("unchecked")
@Override
public boolean onMenuOpened(int featureId, Menu menu) {
if (menu != null) {
//展开菜单显示图标
if (menu.getClass().getSimpleName().equalsIgnoreCase("MenuBuilder")) {
try {
Method method = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
method.setAccessible(true);
method.invoke(menu, true);
method = menu.getClass().getDeclaredMethod("getNonActionItems");
ArrayList<MenuItem> menuItems = (ArrayList<MenuItem>) method.invoke(menu);
if (!menuItems.isEmpty()) {
for (MenuItem menuItem : menuItems) {
Drawable drawable = menuItem.getIcon();
if (drawable != null) {
drawable.mutate();
drawable.setColorFilter(getResources().getColor(R.color.tv_text_default), PorterDuff.Mode.SRC_ATOP);
}
}
}
} catch (Exception ignored) {
}
}
}
return super.onMenuOpened(featureId, menu);
}
@Override
public void setBackground(Drawable background) {
if (background != null) {
background.mutate();
}
super.setBackground(background);
}
@SuppressWarnings("unchecked")
//@Override
public boolean onMenuOpened111(int featureId, Menu menu) {
if (menu != null) {
//展开菜单显示图标
if (menu.getClass().getSimpleName().equalsIgnoreCase("MenuBuilder")) {
try {
Method method = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
method.setAccessible(true);
method.invoke(menu, true);
method = menu.getClass().getDeclaredMethod("getNonActionItems");
ArrayList<MenuItem> menuItems = (ArrayList<MenuItem>) method.invoke(menu);
if (!menuItems.isEmpty()) {
for (MenuItem menuItem : menuItems) {
Drawable drawable = menuItem.getIcon();
if (drawable != null) {
drawable.mutate();
drawable.setColorFilter(getResources().getColor(R.color.tv_text_default), PorterDuff.Mode.SRC_ATOP);
}
}
}
} catch (Exception ignored) {
}
}
}
return super.onMenuOpened(featureId, menu);
}
public void setActionBarBackgroundDrawable(Drawable drawable, boolean mutate) {
mDrawable = mutate ? drawable.mutate() : drawable;
mActionBar.setBackgroundDrawable(mDrawable);
if (mAlpha == 255) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
mAlpha = mDrawable.getAlpha();
} else {
setActionBarAlpha(mAlpha);
}
}
private void updateFadingEdgeToBrandColor(int backgroundColor) {
View fadingView = getView().findViewById(R.id.fade_out_edge);
Drawable background = fadingView.getBackground();
if (background instanceof GradientDrawable) {
background.mutate();
((GradientDrawable) background).setColors(
new int[] {Color.TRANSPARENT, backgroundColor});
}
}
private Drawable prepareDrawable(Drawable child) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
child.setLayoutDirection(mLayoutDirection);
}
child = child.mutate();
child.setCallback(mOwner);
return child;
}
@Override
public void setImage(Drawable drawable, boolean immediate, int progress) {
drawable = maybeApplyRounding(mRoundingParams, mResources, drawable);
drawable.mutate();
mActualImageSettableDrawable.setDrawable(drawable);
mFadeDrawable.beginBatchMode();
fadeOutBranches();
fadeInLayer(mActualImageIndex);
setProgress(progress);
if (immediate) {
mFadeDrawable.finishTransitionImmediately();
}
mFadeDrawable.endBatchMode();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_reader_v1, menu);
Drawable drawable = menu.getItem(0).getIcon();
if (drawable != null) {
drawable.mutate();
drawable.setColorFilter(getResources().getColor(R.color.default_white), PorterDuff.Mode.SRC_ATOP);
}
return true;
}
void updateEditTextBackground() {
// Only update the color filter for the legacy text field, since we can directly change the
// Paint colors of the MaterialShapeDrawable box background without having to use color filters.
if (editText == null || boxBackgroundMode != BOX_BACKGROUND_NONE) {
return;
}
Drawable editTextBackground = editText.getBackground();
if (editTextBackground == null) {
return;
}
if (androidx.appcompat.widget.DrawableUtils.canSafelyMutateDrawable(editTextBackground)) {
editTextBackground = editTextBackground.mutate();
}
if (indicatorViewController.errorShouldBeShown()) {
// Set a color filter for the error color
editTextBackground.setColorFilter(
AppCompatDrawableManager.getPorterDuffColorFilter(
indicatorViewController.getErrorViewCurrentTextColor(), PorterDuff.Mode.SRC_IN));
} else if (counterOverflowed && counterView != null) {
// Set a color filter of the counter color
editTextBackground.setColorFilter(
AppCompatDrawableManager.getPorterDuffColorFilter(
counterView.getCurrentTextColor(), PorterDuff.Mode.SRC_IN));
} else {
// Else reset the color filter and refresh the drawable state so that the
// normal tint is used
DrawableCompat.clearColorFilter(editTextBackground);
editText.refreshDrawableState();
}
}
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);
}
/**
* Returns the Drawable object of the icon for a content type with a disabled tint.
*/
public static Drawable getDisabledIcon(int contentType, Resources resources) {
Drawable icon = ApiCompatibilityUtils.getDrawable(resources, getIcon(contentType));
icon.mutate();
int disabledColor = ApiCompatibilityUtils.getColor(resources,
R.color.primary_text_disabled_material_light);
icon.setColorFilter(disabledColor, PorterDuff.Mode.SRC_IN);
return icon;
}
private void setTextViewSortedAttributes(TextView textView) {
textView.setTextColor(mTextColor);
Drawable arrowDrawable = getStartCompoundDrawable(textView);
arrowDrawable.mutate();
arrowDrawable.setAlpha(255);
}
private static Drawable tintDrawable(Drawable drawable, ColorStateList colors) {
drawable.mutate();
final Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTintList(wrappedDrawable, colors);
return wrappedDrawable;
}