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

The following examples show how to use android.graphics.drawable.Drawable#mutate() . 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: CropImageActivity.java    From giffun with Apache License 2.0 7 votes vote down vote up
/** 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);
      }
    }
  }
}
 
Example 2
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 3
Source File: SVGEditText.java    From SVG-Android with Apache License 2.0 6 votes vote down vote up
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);
        }
    }
}
 
Example 4
Source File: NotificationActions.java    From AcDisplay with GNU General Public License v2.0 6 votes vote down vote up
@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;
}
 
Example 5
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 6
Source File: ColorChooser.java    From SuntimesWidget with GNU General Public License v3.0 6 votes vote down vote up
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();
        }
    }
}
 
Example 7
Source File: DrawableContainerCompat.java    From MaterialProgressBar with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 8
Source File: ContentSettingsResources.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 9
Source File: MBaseActivity.java    From MyBookshelf with GNU General Public License v3.0 5 votes vote down vote up
@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);
}
 
Example 10
Source File: SchemeSelectDialogMain.java    From TwistyTimer with GNU General Public License v3.0 5 votes vote down vote up
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);
}
 
Example 11
Source File: TextInputLayout.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
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();
  }
}
 
Example 12
Source File: Wenku8ReaderActivityV1.java    From light-novel-library_Wenku8_Android with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example 13
Source File: GenericDraweeHierarchy.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
@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();
}
 
Example 14
Source File: DrawableContainerCompat.java    From MaterialProgressBar with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 15
Source File: HeadersFragment.java    From adt-leanback-support with Apache License 2.0 5 votes vote down vote up
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});
    }
}
 
Example 16
Source File: FadingActionBarHelper.java    From Header2ActionBar with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 17
Source File: MBaseActivity.java    From a with GNU General Public License v3.0 5 votes vote down vote up
@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);
}
 
Example 18
Source File: EditTextSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@Override
public void setBackground(Drawable background) {
  if (background != null) {
    background.mutate();
  }
  super.setBackground(background);
}
 
Example 19
Source File: AppCompatTextViewHelper.java    From AndroidBase with Apache License 2.0 4 votes vote down vote up
private static Drawable tintDrawable(Drawable drawable, ColorStateList colors) {
    drawable.mutate();
    final Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
    DrawableCompat.setTintList(wrappedDrawable, colors);
    return wrappedDrawable;
}
 
Example 20
Source File: DataReductionSiteBreakdownView.java    From 365browser with Apache License 2.0 4 votes vote down vote up
private void setTextViewSortedAttributes(TextView textView) {
    textView.setTextColor(mTextColor);
    Drawable arrowDrawable = getStartCompoundDrawable(textView);
    arrowDrawable.mutate();
    arrowDrawable.setAlpha(255);
}