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:de.vanita5.twittnuker.fragment.support.UserProfileFragment.java

private void setMenu(final Menu menu) {
    final AsyncTwitterWrapper twitter = getTwitterWrapper();
    final ParcelableUser user = mUser;
    final Relationship relationship = mRelationship;
    if (twitter == null || user == null)
        return;/*from  ww w. ja v  a 2 s  .co  m*/
    final int activatedColor = ThemeUtils.getUserThemeColor(getActivity());
    final boolean isMyself = user.account_id == user.id;
    final boolean isFollowing = relationship != null && relationship.isSourceFollowingTarget();
    final boolean isProtected = user.is_protected;
    final boolean creatingFriendship = twitter.isCreatingFriendship(user.account_id, user.id);
    final boolean destroyingFriendship = twitter.isDestroyingFriendship(user.account_id, user.id);
    setMenuItemAvailability(menu, MENU_EDIT, isMyself);
    final MenuItem followItem = menu.findItem(MENU_FOLLOW);
    followItem.setVisible(!isMyself);
    final boolean shouldShowFollowItem = !creatingFriendship && !destroyingFriendship && !isMyself
            && relationship != null;
    followItem.setEnabled(shouldShowFollowItem);
    if (shouldShowFollowItem) {
        followItem.setTitle(
                isFollowing ? R.string.unfollow : isProtected ? R.string.send_follow_request : R.string.follow);
        followItem.setIcon(isFollowing ? R.drawable.ic_iconic_action_cancel : R.drawable.ic_iconic_action_add);
    } else {
        followItem.setTitle(null);
        followItem.setIcon(null);
    }
    if (user.id != user.account_id && relationship != null) {
        setMenuItemAvailability(menu, MENU_SEND_DIRECT_MESSAGE, relationship.canSourceDMTarget());
        setMenuItemAvailability(menu, MENU_BLOCK, true);
        final MenuItem blockItem = menu.findItem(MENU_BLOCK);
        if (blockItem != null) {
            final Drawable blockIcon = blockItem.getIcon();
            if (relationship.isSourceBlockingTarget()) {
                blockItem.setTitle(R.string.unblock);
                blockIcon.mutate();
                blockIcon.setColorFilter(activatedColor, PorterDuff.Mode.SRC_ATOP);
            } else {
                blockItem.setTitle(R.string.block);
                blockIcon.clearColorFilter();
            }
        }
    } else {
        setMenuItemAvailability(menu, MENU_SEND_DIRECT_MESSAGE, false);
        setMenuItemAvailability(menu, MENU_BLOCK, false);
        setMenuItemAvailability(menu, MENU_REPORT_SPAM, false);
    }
}

From source file:com.tr4android.support.extension.widget.FlexibleToolbarLayout.java

/**
 * Set the drawable to use for the status bar scrim from resources.
 * Providing null will disable the scrim functionality.
 * <p/>//from   w w  w . ja  v  a2  s .c o  m
 * <p>This scrim is only shown when we have been given a top system inset.</p>
 *
 * @param drawable the drawable to display
 * @attr ref R.styleable#FlexibleToolbarLayout_statusBarScrimColor
 * @see #getStatusBarScrim()
 */
public void setStatusBarScrim(@Nullable Drawable drawable) {
    if (mStatusBarScrim != drawable) {
        if (mStatusBarScrim != null) {
            mStatusBarScrim.setCallback(null);
        }

        mStatusBarScrim = drawable;
        drawable.setCallback(this);
        drawable.mutate().setAlpha(mScrimAlpha);
        ViewCompat.postInvalidateOnAnimation(this);
    }
}

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

private void drawRingerWidgets() {
    //the ringer type description
    TextView ringerDescriptionTV = (TextView) findViewById(id.ringerTypeDescription);
    TextView ringerTV = (TextView) findViewById(id.settings_ringerTitle);

    Drawable icon;
    switch (prefs.getRingerType()) {
    case SimpleCalendarEvent.RINGER.NORMAL:
        ringerDescriptionTV.setText(R.string.pref_ringer_type_normal);
        icon = getResources().getDrawable(R.drawable.ic_state_normal);
        break;/*from w w w  . j  av a2 s  . co m*/
    case SimpleCalendarEvent.RINGER.VIBRATE:
        ringerDescriptionTV.setText(R.string.pref_ringer_type_vibrate);
        icon = getResources().getDrawable(R.drawable.ic_state_vibrate);
        break;
    case SimpleCalendarEvent.RINGER.SILENT:
        ringerDescriptionTV.setText(R.string.pref_ringer_type_silent);
        icon = getResources().getDrawable(R.drawable.ic_state_silent);
        break;
    default:
        throw new IllegalStateException("Saved default ringer is of unknown type: " + prefs.getRingerType());
    }

    if (prefs.getIsServiceActivated()) {
        int iconColor = getResources().getColor(R.color.primary);
        icon.mutate().setColorFilter(iconColor, PorterDuff.Mode.MULTIPLY);
        ringerDescriptionTV.setTextColor(getResources().getColor(R.color.textcolor));
        ringerTV.setTextColor(getResources().getColor(R.color.textcolor));
    } else {
        int iconColor = getResources().getColor(android.R.color.darker_gray);
        icon.mutate().setColorFilter(iconColor, PorterDuff.Mode.MULTIPLY);
        ringerDescriptionTV.setTextColor(getResources().getColor(R.color.textColorDisabled));
        ringerTV.setTextColor(getResources().getColor(R.color.textColorDisabled));
    }

    ImageButton ringerIcon = (ImageButton) findViewById(id.settings_ringerIcon);
    setImageButtonIcon(ringerIcon, icon);
    findViewById(id.settings_ringerBox).setEnabled(prefs.getIsServiceActivated());
}

From source file:org.ciasaboark.tacere.activity.fragment.MainSettingsFragment.java

private void drawRingerWidgets() {
    RelativeLayout ringerBox = (RelativeLayout) rootView.findViewById(R.id.settings_ringerBox);
    ringerBox.setOnClickListener(new View.OnClickListener() {
        @Override/*from w  w  w  .j  ava  2 s  .  c om*/
        public void onClick(View v) {
            String[] ringerTypes = RingerType.names();
            //we don't want ringer type UNDEFINED to be an option
            ArrayList<String> ringerList = new ArrayList<String>();
            for (String type : ringerTypes) {
                if (!type.equalsIgnoreCase(RingerType.UNDEFINED.toString())) {
                    type = type.charAt(0) + type.substring(1).toLowerCase();
                    ringerList.add(type);
                }
            }
            final String[] filteredRingerTypes = ringerList.toArray(new String[] {});
            int previouslySelectedRinger = ringerList.indexOf(prefs.getRingerType().toString());

            final AlertDialog alert = new AlertDialog.Builder(new ContextThemeWrapper(context, R.style.Dialog))
                    .setTitle(R.string.settings_section_general_ringer)
                    .setSingleChoiceItems(filteredRingerTypes, previouslySelectedRinger,
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    String selectedRingerString = filteredRingerTypes[which];
                                    int selectedRingerValue = RingerType
                                            .getIntForStringValue(selectedRingerString);
                                    RingerType selectedRinger = RingerType.getTypeForInt(selectedRingerValue);
                                    prefs.setRingerType(selectedRinger);
                                    drawRingerWidgets();
                                    dialog.dismiss();
                                }
                            })
                    .create();

            alert.show();
        }
    });

    //the ringer type description
    TextView ringerDescriptionTV = (TextView) rootView.findViewById(R.id.ringerTypeDescription);
    TextView ringerTV = (TextView) rootView.findViewById(R.id.settings_ringerTitle);

    Drawable icon;
    switch (prefs.getRingerType()) {
    case NORMAL:
        ringerDescriptionTV.setText(R.string.settings_section_general_ringer_normal);
        icon = getResources().getDrawable(R.drawable.ic_state_normal);
        break;
    case VIBRATE:
        ringerDescriptionTV.setText(R.string.settings_section_general_ringer_vibrate);
        icon = getResources().getDrawable(R.drawable.ic_state_vibrate);
        break;
    case SILENT:
        ringerDescriptionTV.setText(R.string.settings_section_general_ringer_silent);
        icon = getResources().getDrawable(R.drawable.ic_state_silent);
        break;
    case IGNORE:
        ringerDescriptionTV.setText(R.string.settings_section_general_ringer_ignore);
        icon = getResources().getDrawable(R.drawable.ic_state_ignore);
        break;
    default:
        throw new IllegalStateException("Saved default ringer is of unknown type: " + prefs.getRingerType());
    }

    int iconColor = getResources().getColor(R.color.icon_tint);
    icon.mutate().setColorFilter(iconColor, PorterDuff.Mode.MULTIPLY);
    ringerDescriptionTV.setTextColor(getResources().getColor(R.color.text_color));
    ringerTV.setTextColor(getResources().getColor(R.color.text_color));

    ImageView ringerIcon = (ImageView) rootView.findViewById(R.id.settings_ringerIcon);
    ringerIcon.setImageDrawable(icon);
}

From source file:android.support.design.widget.CollapsingToolbarLayout.java

/**
 * Set the drawable to use for the content scrim from resources. Providing null will disable
 * the scrim functionality./*from w w w . j  a v  a2  s.  com*/
 *
 * @param drawable the drawable to display
 *
 * @attr ref R.styleable#CollapsingToolbarLayout_contentScrim
 * @see #getContentScrim()
 */
public void setContentScrim(@Nullable Drawable drawable) {
    if (mContentScrim != drawable) {
        if (mContentScrim != null) {
            mContentScrim.setCallback(null);
        }
        mContentScrim = drawable != null ? drawable.mutate() : null;
        if (mContentScrim != null) {
            mContentScrim.setBounds(0, 0, getWidth(), getHeight());
            mContentScrim.setCallback(this);
            mContentScrim.setAlpha(mScrimAlpha);
        }
        ViewCompat.postInvalidateOnAnimation(this);
    }
}

From source file:android.support.design.widget.CollapsingToolbarLayout.java

/**
 * Set the drawable to use for the status bar scrim from resources.
 * Providing null will disable the scrim functionality.
 *
 * <p>This scrim is only shown when we have been given a top system inset.</p>
 *
 * @param drawable the drawable to display
 *
 * @attr ref R.styleable#CollapsingToolbarLayout_statusBarScrim
 * @see #getStatusBarScrim()//from w w  w  .  j av a 2s.  c  o  m
 */
public void setStatusBarScrim(@Nullable Drawable drawable) {
    if (mStatusBarScrim != drawable) {
        if (mStatusBarScrim != null) {
            mStatusBarScrim.setCallback(null);
        }
        mStatusBarScrim = drawable != null ? drawable.mutate() : null;
        if (mStatusBarScrim != null) {
            if (mStatusBarScrim.isStateful()) {
                mStatusBarScrim.setState(getDrawableState());
            }
            DrawableCompat.setLayoutDirection(mStatusBarScrim, ViewCompat.getLayoutDirection(this));
            mStatusBarScrim.setVisible(getVisibility() == VISIBLE, false);
            mStatusBarScrim.setCallback(this);
            mStatusBarScrim.setAlpha(mScrimAlpha);
        }
        ViewCompat.postInvalidateOnAnimation(this);
    }
}

From source file:de.vanita5.twittnuker.activity.support.ComposeActivity.java

private void setCommonMenu(final Menu menu) {
    final boolean hasMedia = hasMedia();
    final int activatedColor = getUserThemeColor(this);
    final MenuItem itemAddImageSubmenu = menu.findItem(R.id.add_image_submenu);
    if (itemAddImageSubmenu != null) {
        final Drawable iconAddImage = itemAddImageSubmenu.getIcon();
        iconAddImage.mutate();
        if (hasMedia) {
            iconAddImage.setColorFilter(activatedColor, Mode.SRC_ATOP);
        } else {/*w ww .j  av a2s . c  o m*/
            iconAddImage.clearColorFilter();
        }
    }
    final MenuItem itemAttachLocation = menu.findItem(MENU_ADD_LOCATION);
    if (itemAttachLocation != null) {
        final Drawable iconAttachLocation = itemAttachLocation.getIcon().mutate();
        final boolean attachLocation = mPreferences.getBoolean(KEY_ATTACH_LOCATION, false);
        if (attachLocation && getLocation()) {
            iconAttachLocation.setColorFilter(activatedColor, Mode.SRC_ATOP);
            itemAttachLocation.setTitle(R.string.remove_location);
            itemAttachLocation.setChecked(true);
        } else {
            setProgressVisibility(false);
            mPreferences.edit().putBoolean(KEY_ATTACH_LOCATION, false).commit();
            iconAttachLocation.clearColorFilter();
            itemAttachLocation.setTitle(R.string.add_location);
            itemAttachLocation.setChecked(false);
        }
    }
    final MenuItem viewItem = menu.findItem(MENU_VIEW);
    if (viewItem != null) {
        viewItem.setVisible(mInReplyToStatus != null);
    }
    final MenuItem itemToggleSensitive = menu.findItem(MENU_TOGGLE_SENSITIVE);
    if (itemToggleSensitive != null) {
        itemToggleSensitive.setVisible(hasMedia);
        itemToggleSensitive.setEnabled(hasMedia);
        itemToggleSensitive.setChecked(hasMedia && mIsPossiblySensitive);
        if (hasMedia) {
            final Drawable iconToggleSensitive = itemToggleSensitive.getIcon().mutate();
            if (mIsPossiblySensitive) {
                itemToggleSensitive.setTitle(R.string.remove_sensitive_mark);
                iconToggleSensitive.setColorFilter(activatedColor, Mode.SRC_ATOP);
            } else {
                itemToggleSensitive.setTitle(R.string.mark_as_sensitive);
                iconToggleSensitive.clearColorFilter();
            }
        }
    }
}

From source file:org.buffer.android.buffertextinputlayout.BufferTextInputLayout.java

private void updateEditTextBackground() {
    if (editText == null) {
        return;//from   w  w  w.j  av  a 2s.c om
    }
    Drawable editTextBackground = editText.getBackground();
    if (editTextBackground == null) {
        return;
    }
    ensureBackgroundDrawableStateWorkaround();
    if (android.support.v7.widget.DrawableUtils.canSafelyMutateDrawable(editTextBackground)) {
        editTextBackground = editTextBackground.mutate();
    }
    if (errorShown && errorView != null) {
        // Set a color filter of the error color
        editTextBackground.setColorFilter(AppCompatDrawableManager
                .getPorterDuffColorFilter(errorView.getCurrentTextColor(), 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();
    }
}

From source file:android.support.design.widget.TextInputLayout.java

private void updateEditTextBackground() {
    if (mEditText == null) {
        return;/*from  w w w. ja  v  a 2 s  .  com*/
    }

    Drawable editTextBackground = mEditText.getBackground();
    if (editTextBackground == null) {
        return;
    }

    ensureBackgroundDrawableStateWorkaround();

    if (android.support.v7.widget.DrawableUtils.canSafelyMutateDrawable(editTextBackground)) {
        editTextBackground = editTextBackground.mutate();
    }

    if (mErrorShown && mErrorView != null) {
        // Set a color filter of the error color
        editTextBackground.setColorFilter(AppCompatDrawableManager
                .getPorterDuffColorFilter(mErrorView.getCurrentTextColor(), PorterDuff.Mode.SRC_IN));
    } else if (mCounterOverflowed && mCounterView != null) {
        // Set a color filter of the counter color
        editTextBackground.setColorFilter(AppCompatDrawableManager
                .getPorterDuffColorFilter(mCounterView.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);
        mEditText.refreshDrawableState();
    }
}

From source file:com.guoxiaoxing.cloud.music.magicasakura.widgets.AppCompatImageHelper.java

private boolean applySupportImageTint() {
    Drawable image = ((ImageView) mView).getDrawable();
    AnimationDrawable animationDrawable = null;
    if (image instanceof AnimationDrawable) {
        Log.e("drawable", "is animationdrawable");
        animationDrawable = ((AnimationDrawable) image);
        //image = animationDrawable;
    }//from  w ww  . j  av  a  2s  .c o  m

    if (image != null && mImageTintInfo != null && mImageTintInfo.mHasTintList) {

        if (animationDrawable != null) {
            Log.e("drawable", "is animationdrawable not null");
            Drawable tintDrawable = animationDrawable;
            Log.e("drawable", "start0");
            tintDrawable = DrawableCompat.wrap(tintDrawable);
            Log.e("drawable", "start1");
            if (mImageTintInfo.mHasTintList) {
                DrawableCompat.setTintList(tintDrawable, mImageTintInfo.mTintList);
            }
            if (mImageTintInfo.mHasTintMode) {
                DrawableCompat.setTintMode(tintDrawable, mImageTintInfo.mTintMode);
            }
            if (tintDrawable.isStateful()) {
                tintDrawable.setState(mView.getDrawableState());
            }
            tintDrawable = DrawableCompat.unwrap(tintDrawable);
            setImageDrawable(tintDrawable);
            if (image == tintDrawable) {
                Log.e("drawable", "invalidateself");
                // tintDrawable.invalidateSelf();
            }
            return true;
        } else {
            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;
}