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

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

源代码1 项目: giffun   文件: CropImageActivity.java
/** 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);
      }
    }
  }
}
 
源代码2 项目: 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();
    }
}
 
源代码3 项目: SVG-Android   文件: SVGEditText.java
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);
        }
    }
}
 
源代码4 项目: AcDisplay   文件: NotificationActions.java
@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;
}
 
源代码5 项目: 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;
}
 
源代码6 项目: SuntimesWidget   文件: ColorChooser.java
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;
}
 
源代码8 项目: MyBookshelf   文件: MBaseActivity.java
@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);
}
 
源代码9 项目: litho   文件: EditTextSpec.java
@Override
public void setBackground(Drawable background) {
  if (background != null) {
    background.mutate();
  }
  super.setBackground(background);
}
 
源代码10 项目: a   文件: MBaseActivity.java
@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);
}
 
源代码11 项目: Header2ActionBar   文件: FadingActionBarHelper.java
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);
    }
}
 
源代码12 项目: adt-leanback-support   文件: HeadersFragment.java
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();
  }
}
 
源代码17 项目: TwistyTimer   文件: SchemeSelectDialogMain.java
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);
}
 
源代码18 项目: AndroidChromium   文件: ContentSettingsResources.java
/**
 * 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);
}
 
源代码20 项目: AndroidBase   文件: AppCompatTextViewHelper.java
private static Drawable tintDrawable(Drawable drawable, ColorStateList colors) {
    drawable.mutate();
    final Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
    DrawableCompat.setTintList(wrappedDrawable, colors);
    return wrappedDrawable;
}