Example usage for android.graphics.drawable Drawable mutate

List of usage examples for android.graphics.drawable Drawable mutate

Introduction

In this page you can find the example usage for android.graphics.drawable Drawable mutate.

Prototype

public @NonNull Drawable mutate() 

Source Link

Document

Make this drawable mutable.

Usage

From source file:com.max.library.view.v7.AppCompatDrawableManager.java

public Drawable getDrawable(@NonNull Context context, @DrawableRes int resId, boolean failIfNotKnown) {
    // Let the InflateDelegates have a go first
    if (mDelegates != null) {
        for (int i = 0, count = mDelegates.size(); i < count; i++) {
            final InflateDelegate delegate = mDelegates.get(i);
            final Drawable result = delegate.onInflateDrawable(context, resId);
            if (result != null) {
                return result;
            }/* www . j a va 2  s. com*/
        }
    }

    // The delegates failed so we'll carry on
    Drawable drawable = ContextCompat.getDrawable(context, resId);

    if (drawable != null) {
        if (Build.VERSION.SDK_INT >= 8) {
            // Mutate can cause NPEs on 2.1
            drawable = drawable.mutate();
        }

        final ColorStateList tintList = getTintList(context, resId);
        if (tintList != null) {
            // First wrap the Drawable and set the tint list
            drawable = DrawableCompat.wrap(drawable);
            DrawableCompat.setTintList(drawable, tintList);

            // If there is a blending mode specified for the drawable, use it
            final PorterDuff.Mode tintMode = getTintMode(resId);
            if (tintMode != null) {
                DrawableCompat.setTintMode(drawable, tintMode);
            }
        } else if (resId == R.drawable.abc_cab_background_top_material) {
            return new LayerDrawable(
                    new Drawable[] { getDrawable(context, R.drawable.abc_cab_background_internal_bg),
                            getDrawable(context, R.drawable.abc_cab_background_top_mtrl_alpha) });
        } else if (resId == com.max.library.R.drawable.abc_seekbar_track_material) {
            LayerDrawable ld = (LayerDrawable) drawable;
            setPorterDuffColorFilter(ld.findDrawableByLayerId(android.R.id.background),
                    getThemeAttrColor(context, R.attr.colorControlNormal), DEFAULT_MODE);
            setPorterDuffColorFilter(ld.findDrawableByLayerId(android.R.id.secondaryProgress),
                    getThemeAttrColor(context, R.attr.colorControlNormal), DEFAULT_MODE);
            setPorterDuffColorFilter(ld.findDrawableByLayerId(android.R.id.progress),
                    getThemeAttrColor(context, R.attr.colorControlActivated), DEFAULT_MODE);
        } else {
            final boolean tinted = tintDrawableUsingColorFilter(context, resId, drawable);
            if (!tinted && failIfNotKnown) {
                // If we didn't tint using a ColorFilter, and we're set to fail if we don't
                // know the id, return null
                drawable = null;
            }
        }
    }
    return drawable;
}

From source file:com.dwdesign.tweetings.fragment.UserListDetailsFragment.java

public void changeUserList(final long account_id, final UserList user_list) {
    if (user_list == null || getActivity() == null || !isMyActivatedAccount(getActivity(), account_id))
        return;/*from   ww w  .  j a va  2s.  c om*/
    getLoaderManager().destroyLoader(0);
    final User user = user_list.getUser();
    if (user == null)
        return;
    final boolean is_my_activated_account = isMyActivatedAccount(getActivity(), user_list.getId());
    mErrorRetryContainer.setVisibility(View.GONE);
    mAccountId = account_id;
    mUserListId = user_list.getId();
    mUserName = user.getName();
    mUserId = user.getId();
    mUserScreenName = user.getScreenName();
    mListName = user_list.getName();
    mListSlug = user_list.getSlug();

    final boolean is_multiple_account_enabled = getActivatedAccountIds(getActivity()).length > 1;

    mListView.setBackgroundResource(is_multiple_account_enabled ? R.drawable.ic_label_account_nopadding : 0);
    if (is_multiple_account_enabled) {
        final Drawable d = mListView.getBackground();
        if (d != null) {
            d.mutate().setColorFilter(getAccountColor(getActivity(), account_id), PorterDuff.Mode.MULTIPLY);
            mListView.invalidate();
        }
    }

    mListNameView.setText(mListName);
    mUserNameView.setText(mDisplayName ? mUserName : mUserScreenName);
    final String description = user_list.getDescription();
    mDescriptionContainer
            .setVisibility(is_my_activated_account || !isNullOrEmpty(description) ? View.VISIBLE : View.GONE);
    mDescriptionContainer.setOnLongClickListener(this);
    mDescriptionView.setText(description);
    final TwidereLinkify linkify = new TwidereLinkify(mDescriptionView);
    linkify.setOnLinkClickListener(this);
    linkify.addAllLinks();
    mDescriptionView.setMovementMethod(LinkMovementMethod.getInstance());
    final String profile_image_url_string = parseString(user.getProfileImageURL());
    final boolean hires_profile_image = getResources().getBoolean(R.bool.hires_profile_image);
    mProfileImageLoader.displayProfileImage(mProfileImageView,
            hires_profile_image ? getBiggerTwitterProfileImage(profile_image_url_string)
                    : profile_image_url_string);
    mUserList = user_list;
    //if (mUserId == mAccountId) {
    mFollowMoreButton.setText(R.string.more);
    mFollowMoreButton.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.expander_open_holo, 0);
    //} else {
    //   mFollowMoreButton.setText(user_list.isFollowing() ? R.string.unfollow : R.string.follow);
    //   mFollowMoreButton.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
    //}
    mAdapter.notifyDataSetChanged();
}

From source file:android.support.v7.widget.AppCompatDrawableManager.java

static boolean tintDrawableUsingColorFilter(@NonNull Context context, @DrawableRes final int resId,
        @NonNull Drawable drawable) {
    PorterDuff.Mode tintMode = DEFAULT_MODE;
    boolean colorAttrSet = false;
    int colorAttr = 0;
    int alpha = -1;

    if (arrayContains(COLORFILTER_TINT_COLOR_CONTROL_NORMAL, resId)) {
        colorAttr = R.attr.colorControlNormal;
        colorAttrSet = true;// w  w  w .j av a2 s. c  om
    } else if (arrayContains(COLORFILTER_COLOR_CONTROL_ACTIVATED, resId)) {
        colorAttr = R.attr.colorControlActivated;
        colorAttrSet = true;
    } else if (arrayContains(COLORFILTER_COLOR_BACKGROUND_MULTIPLY, resId)) {
        colorAttr = android.R.attr.colorBackground;
        colorAttrSet = true;
        tintMode = PorterDuff.Mode.MULTIPLY;
    } else if (resId == R.drawable.abc_list_divider_mtrl_alpha) {
        colorAttr = android.R.attr.colorForeground;
        colorAttrSet = true;
        alpha = Math.round(0.16f * 255);
    } else if (resId == R.drawable.abc_dialog_material_background) {
        colorAttr = android.R.attr.colorBackground;
        colorAttrSet = true;
    }

    if (colorAttrSet) {
        if (DrawableUtils.canSafelyMutateDrawable(drawable)) {
            drawable = drawable.mutate();
        }

        final int color = getThemeAttrColor(context, colorAttr);
        drawable.setColorFilter(getPorterDuffColorFilter(color, tintMode));

        if (alpha != -1) {
            drawable.setAlpha(alpha);
        }

        if (DEBUG) {
            Log.d(TAG, "[tintDrawableUsingColorFilter] Tinted " + context.getResources().getResourceName(resId)
                    + " with color: #" + Integer.toHexString(color));
        }
        return true;
    }
    return false;
}

From source file:org.ciasaboark.tacere.activity.SettingsActivity.java

private void drawCalendarWidgets() {
    ImageView calendarIcon = (ImageView) findViewById(id.calendar_icon);
    TextView calendarTV = (TextView) findViewById(id.calendar_text);
    RelativeLayout calendarBox = (RelativeLayout) findViewById(id.select_calendar_box);

    Drawable d = getResources().getDrawable(R.drawable.calendar_icon);
    d.setColorFilter(getResources().getColor(R.color.primary), PorterDuff.Mode.MULTIPLY);

    calendarBox.setOnClickListener(new View.OnClickListener() {
        @Override//from w  w  w .  j  ava2s .com
        public void onClick(View view) {
            Intent i = new Intent(context, SelectCalendarsActivity.class);
            startActivity(i);
        }
    });

    if (prefs.getIsServiceActivated()) {
        int iconColor = getResources().getColor(R.color.primary);
        d.mutate().setColorFilter(iconColor, PorterDuff.Mode.MULTIPLY);
        calendarTV.setTextColor(getResources().getColor(R.color.textcolor));
        calendarBox.setClickable(true);
    } else {
        int iconColor = getResources().getColor(android.R.color.darker_gray);
        d.mutate().setColorFilter(iconColor, PorterDuff.Mode.MULTIPLY);
        calendarTV.setTextColor(getResources().getColor(R.color.textColorDisabled));
        calendarBox.setClickable(false);
    }
    calendarIcon.setBackgroundDrawable(d);

}

From source file:androidx.mediarouter.app.MediaRouteButton.java

/**
 * Sets a drawable to use as the remote route indicator.
 *//*w w  w .java2s  .c  om*/
public void setRemoteIndicatorDrawable(Drawable d) {
    if (mRemoteIndicatorLoader != null) {
        mRemoteIndicatorLoader.cancel(false);
    }

    if (mRemoteIndicator != null) {
        mRemoteIndicator.setCallback(null);
        unscheduleDrawable(mRemoteIndicator);
    }
    if (d != null) {
        if (mButtonTint != null) {
            d = DrawableCompat.wrap(d.mutate());
            DrawableCompat.setTintList(d, mButtonTint);
        }
        d.setCallback(this);
        d.setState(getDrawableState());
        d.setVisible(getVisibility() == VISIBLE, false);
    }
    mRemoteIndicator = d;

    refreshDrawableState();
    if (mAttachedToWindow && mRemoteIndicator != null
            && mRemoteIndicator.getCurrent() instanceof AnimationDrawable) {
        AnimationDrawable curDrawable = (AnimationDrawable) mRemoteIndicator.getCurrent();
        if (mIsConnecting) {
            if (!curDrawable.isRunning()) {
                curDrawable.start();
            }
        } else if (mRemoteActive) {
            if (curDrawable.isRunning()) {
                curDrawable.stop();
            }
            curDrawable.selectDrawable(curDrawable.getNumberOfFrames() - 1);
        }
    }
}

From source file:net.gsantner.opoc.util.ContextUtils.java

/**
 * Tint a {@link Drawable} with given {@code color}
 *///from w  w w .  java  2 s  . c  o m
public Drawable tintDrawable(@Nullable Drawable drawable, @ColorInt int color) {
    if (drawable != null) {
        drawable = DrawableCompat.wrap(drawable);
        DrawableCompat.setTint(drawable.mutate(), color);
    }
    return drawable;
}

From source file:android.support.v7.widget.AppCompatDrawableManager.java

private Drawable tintDrawable(@NonNull Context context, @DrawableRes int resId, boolean failIfNotKnown,
        @NonNull Drawable drawable) {
    final ColorStateList tintList = getTintList(context, resId);
    if (tintList != null) {
        // First mutate the Drawable, then wrap it and set the tint list
        if (DrawableUtils.canSafelyMutateDrawable(drawable)) {
            drawable = drawable.mutate();
        }//www . ja v a 2 s  . com
        drawable = DrawableCompat.wrap(drawable);
        DrawableCompat.setTintList(drawable, tintList);

        // If there is a blending mode specified for the drawable, use it
        final PorterDuff.Mode tintMode = getTintMode(resId);
        if (tintMode != null) {
            DrawableCompat.setTintMode(drawable, tintMode);
        }
    } else if (resId == R.drawable.abc_seekbar_track_material) {
        LayerDrawable ld = (LayerDrawable) drawable;
        setPorterDuffColorFilter(ld.findDrawableByLayerId(android.R.id.background),
                getThemeAttrColor(context, R.attr.colorControlNormal), DEFAULT_MODE);
        setPorterDuffColorFilter(ld.findDrawableByLayerId(android.R.id.secondaryProgress),
                getThemeAttrColor(context, R.attr.colorControlNormal), DEFAULT_MODE);
        setPorterDuffColorFilter(ld.findDrawableByLayerId(android.R.id.progress),
                getThemeAttrColor(context, R.attr.colorControlActivated), DEFAULT_MODE);
    } else if (resId == R.drawable.abc_ratingbar_material
            || resId == R.drawable.abc_ratingbar_indicator_material
            || resId == R.drawable.abc_ratingbar_small_material) {
        LayerDrawable ld = (LayerDrawable) drawable;
        setPorterDuffColorFilter(ld.findDrawableByLayerId(android.R.id.background),
                getDisabledThemeAttrColor(context, R.attr.colorControlNormal), DEFAULT_MODE);
        setPorterDuffColorFilter(ld.findDrawableByLayerId(android.R.id.secondaryProgress),
                getThemeAttrColor(context, R.attr.colorControlActivated), DEFAULT_MODE);
        setPorterDuffColorFilter(ld.findDrawableByLayerId(android.R.id.progress),
                getThemeAttrColor(context, R.attr.colorControlActivated), DEFAULT_MODE);
    } else {
        final boolean tinted = tintDrawableUsingColorFilter(context, resId, drawable);
        if (!tinted && failIfNotKnown) {
            // If we didn't tint using a ColorFilter, and we're set to fail if we don't
            // know the id, return null
            drawable = null;
        }
    }
    return drawable;
}

From source file:me.albertonicoletti.latex.activities.EditorActivity.java

private void setSaveButtonEnabled(boolean enable) {
    if (saveButton != null) {
        Drawable resIcon = ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_save_white_24dp);
        if (!enable)
            resIcon.mutate().setColorFilter(Color.LTGRAY, PorterDuff.Mode.SRC_IN);
        saveButton.setEnabled(enable);//from w  ww . j  a v a  2s  .  c o  m
        saveButton.setIcon(resIcon);
    }
}

From source file:com.dwdesign.tweetings.fragment.StatusFragment.java

private void updateUserColor() {
    if (mUserColorLabel != null && mStatus != null) {
        final Drawable d = mUserColorLabel.getBackground();
        if (d != null) {
            d.mutate().setColorFilter(getUserColor(getActivity(), mStatus.user_id), Mode.MULTIPLY);
            mUserColorLabel.invalidate();
        }// w w w.j  a  v a2 s  . c o  m
    }
}

From source file:org.ciasaboark.tacere.view.EventListItem.java

private Drawable getRingerIcon() {
    Drawable icon;
    int defaultColor = context.getResources().getColor(R.color.ringer_default);
    int color = defaultColor;

    EventManager eventManager = new EventManager(context, event);
    RingerType ringerType = eventManager.getBestRinger();
    icon = getIconForRinger(ringerType);

    RingerSource ringerSource = eventManager.getRingerSource();
    if (ringerSource != RingerSource.DEFAULT) {
        color = iconTintColor;//w  ww  .j  a  v  a  2 s  . c  o  m
    }

    icon.mutate().setColorFilter(color, PorterDuff.Mode.MULTIPLY);

    if (icon == null) {
        throw new AssertionError(this.getClass().getName() + "Ringer icon should not be null");
    }
    return icon;
}