Java Code Examples for android.support.v4.graphics.drawable.DrawableCompat#setTintList()

The following examples show how to use android.support.v4.graphics.drawable.DrawableCompat#setTintList() . 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: TintableImageView.java    From ImageLetterIcon with Apache License 2.0 6 votes vote down vote up
@Override
protected void drawableStateChanged() {
    super.drawableStateChanged();
    if (mTint != null) {
        if (mTint.isStateful())
            setColorFilter(mTint.getColorForState(getDrawableState(), 0));
        else setColorFilter(mTint);
    }
    Drawable drawable = getBackground();
    if (mBackgroundTint != null && drawable != null) {
        Drawable wrap = DrawableCompat.wrap(drawable);
        wrap = wrap.mutate();

        if (mBackgroundTint.isStateful())
            DrawableCompat.setTint(wrap, ContextCompat.getColor(getContext(), mBackgroundTint.getColorForState(getDrawableState(), 0)));
        else DrawableCompat.setTintList(wrap, mBackgroundTint);

        DrawableCompat.setTintMode(wrap, PorterDuff.Mode.SRC_IN);
    }
}
 
Example 2
Source File: MDTintHelper.java    From NewsMe with Apache License 2.0 6 votes vote down vote up
public static void setTint(@NonNull CheckedTextView textView, @ColorInt int color) {
    ColorStateList sl = new ColorStateList(new int[][]{
            new int[]{-android.R.attr.state_checked},
            new int[]{android.R.attr.state_checked}
    }, new int[]{
            ThemeHelper.resolveColor(textView.getContext(), R.attr.colorControlNormal),
            color
    });
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        textView.setCheckMarkTintList(sl);
    } else {
        Drawable d = DrawableCompat.wrap(ContextCompat.getDrawable(textView.getContext(), R.drawable.abc_btn_radio_material));
        DrawableCompat.setTintList(d, sl);
        textView.setCheckMarkDrawable(d);
    }
}
 
Example 3
Source File: TintUtil.java    From android-material-stepper with Apache License 2.0 6 votes vote down vote up
/**
 * Tints a drawable with the provided color state list
 * @param drawable drawable to tint
 * @param color tint color state list
 * @return tinted drawable
 */
public static Drawable tintDrawable(@Nullable Drawable drawable, ColorStateList color) {
    if (drawable != null) {
        drawable = DrawableCompat.unwrap(drawable);
        Rect bounds = drawable.getBounds();
        drawable = DrawableCompat.wrap(drawable);
        // bounds can be all set to zeros when inflating vector drawables in Android Support Library 23.3.0...
        if (bounds.right == 0 || bounds.bottom == 0) {
            if (drawable.getIntrinsicHeight() != -1 && drawable.getIntrinsicWidth() != -1) {
                bounds.right = drawable.getIntrinsicWidth();
                bounds.bottom = drawable.getIntrinsicHeight();
            } else {
                Log.w(TAG, "Cannot tint drawable because its bounds cannot be determined!");
                return DrawableCompat.unwrap(drawable);
            }
        }
        DrawableCompat.setTintList(drawable, color);
        drawable.setBounds(bounds);
    }
    return drawable;
}
 
Example 4
Source File: IncDecImageButton.java    From IncDec with Apache License 2.0 6 votes vote down vote up
private void setupLeftButton(ImageButton leftButton, Drawable leftSrc,
                             int leftButtonTint, int leftDrawableTint,Drawable background) {
    if(leftSrc!=null) {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            leftSrc.setTintList(new ColorStateList(new int[][]{new int[]{0}},
                    new int[]{leftDrawableTint}));
        }
        else
        {
            final Drawable wrappedDrawable = DrawableCompat.wrap(leftSrc);
            DrawableCompat.setTintList(wrappedDrawable, ColorStateList.valueOf(leftDrawableTint));
        }
        leftButton.setImageDrawable(leftSrc);
    }
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        leftButton.setBackgroundTintList(new ColorStateList(new int[][]{new int[]{0}},
                new int[]{leftButtonTint}));
    }
    else
    {
        ViewCompat.setBackgroundTintList(leftButton, ColorStateList.valueOf(leftButtonTint));
    }
    leftButton.setBackground(background);

}
 
Example 5
Source File: MDTintHelper.java    From NewsMe with Apache License 2.0 6 votes vote down vote up
public static void setTint(@NonNull SeekBar seekBar, @ColorInt int color) {
    ColorStateList s1 = ColorStateList.valueOf(color);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        seekBar.setThumbTintList(s1);
        seekBar.setProgressTintList(s1);
    } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1) {
        Drawable progressDrawable = DrawableCompat.wrap(seekBar.getProgressDrawable());
        seekBar.setProgressDrawable(progressDrawable);
        DrawableCompat.setTintList(progressDrawable, s1);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            Drawable thumbDrawable = DrawableCompat.wrap(seekBar.getThumb());
            DrawableCompat.setTintList(thumbDrawable, s1);
            seekBar.setThumb(thumbDrawable);
        }
    } else {
        PorterDuff.Mode mode = PorterDuff.Mode.SRC_IN;
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
            mode = PorterDuff.Mode.MULTIPLY;
        }
        if (seekBar.getIndeterminateDrawable() != null)
            seekBar.getIndeterminateDrawable().setColorFilter(color, mode);
        if (seekBar.getProgressDrawable() != null)
            seekBar.getProgressDrawable().setColorFilter(color, mode);
    }
}
 
Example 6
Source File: DrawableUtils.java    From PainlessMusicPlayer with Apache License 2.0 5 votes vote down vote up
@Nullable
public static Drawable getTintedDrawable(@Nullable Drawable drawable,
        @Nullable final ColorStateList tint) {
    if (drawable != null) {
        drawable = DrawableCompat.wrap(drawable);
        DrawableCompat.setTintList(drawable, tint);
    }
    return drawable;
}
 
Example 7
Source File: TintHelper.java    From APlayer with GNU General Public License v3.0 5 votes vote down vote up
@CheckResult
@Nullable
public static Drawable createTintedDrawable(@Nullable Drawable drawable,
    @NonNull ColorStateList sl) {
  if (drawable == null) {
    return null;
  }
  drawable = DrawableCompat.wrap(drawable.mutate());
  DrawableCompat.setTintList(drawable, sl);
  return drawable;
}
 
Example 8
Source File: Util.java    From RetroMusicPlayer with GNU General Public License v3.0 5 votes vote down vote up
@CheckResult
@Nullable
public static Drawable createTintedDrawable(@Nullable Drawable drawable, @NonNull ColorStateList colorStateList) {
    if (drawable == null) {
        return null;
    }
    Drawable wrap = DrawableCompat.wrap(drawable.mutate());
    DrawableCompat.setTintList(wrap, colorStateList);
    return wrap;
}
 
Example 9
Source File: Easel.java    From andela-crypto-app with Apache License 2.0 5 votes vote down vote up
public static void tint(@NonNull SwitchCompat switchCompat, @ColorInt int color, @ColorInt int unpressedColor) {
    ColorStateList sl = new ColorStateList(new int[][]{
            new int[]{-android.R.attr.state_checked},
            new int[]{android.R.attr.state_checked}
    }, new int[]{
            unpressedColor,
            color
    });

    DrawableCompat.setTintList(switchCompat.getThumbDrawable(), sl);
    DrawableCompat.setTintList(switchCompat.getTrackDrawable(), createSwitchTrackColorStateList(switchCompat.getContext(), color));
}
 
Example 10
Source File: VectorDrawableInflateImpl.java    From MagicaSakura with Apache License 2.0 5 votes vote down vote up
@Override
public Drawable inflateDrawable(Context context, XmlPullParser parser, AttributeSet attrs) throws IOException, XmlPullParserException {
    ColorStateList colorFilter = getTintColorList(context, attrs, android.R.attr.tint);
    Drawable d;
    if (resId == 0) {
        d = VectorDrawableCompat.createFromXmlInner(context.getResources(), parser, attrs);
    } else {
        d = VectorDrawableCompat.create(context.getResources(), resId, context.getTheme());
    }
    if (d != null && colorFilter != null) {
        DrawableCompat.setTintList(d, colorFilter);
    }
    return d;
}
 
Example 11
Source File: BottomNavigationItemView.java    From MiPushFramework with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void setIcon(Drawable icon) {
    if (icon != null) {
        Drawable.ConstantState state = icon.getConstantState();
        icon = DrawableCompat.wrap(state == null ? icon : state.newDrawable()).mutate();
        DrawableCompat.setTintList(icon, mIconTint);
    }
    mIcon.setImageDrawable(icon);
}
 
Example 12
Source File: MediaActionSwitchView.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private void initializeView() {
        Context context = getContext();

        photoDrawable = ContextCompat.getDrawable(context, R.drawable.phoenix_photo_camera_white);
        photoDrawable = DrawableCompat.wrap(photoDrawable);
        DrawableCompat.setTintList(photoDrawable.mutate(), ContextCompat.getColorStateList(context, R.drawable.phoenix_selector_switch_camera_mode));

        videoDrawable = ContextCompat.getDrawable(context, R.drawable.phoenix_videocam_white);
        videoDrawable = DrawableCompat.wrap(videoDrawable);
        DrawableCompat.setTintList(videoDrawable.mutate(), ContextCompat.getColorStateList(context, R.drawable.phoenix_selector_switch_camera_mode));

        setBackgroundResource(R.drawable.phoenix_circle_frame_background_dark);
//        setBackgroundResource(R.drawable.phoenix_circle_frame_background);

        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if(onMediaActionStateChangeListener != null) {
                    onMediaActionStateChangeListener.switchAction();
                }
            }
        });

        padding = DeviceUtils.convertDipToPixels(context, padding);
        setPadding(padding, padding, padding, padding);

        displayActionWillSwitchVideo();
    }
 
Example 13
Source File: IconUtils.java    From FireFiles with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static Drawable applyTintList(Context context, int drawableId, int tintColorId) {
    final Drawable icon = getDrawable(context, drawableId);
    icon.mutate();
    DrawableCompat.setTintList(DrawableCompat.wrap(icon), ContextCompat.getColorStateList(context, tintColorId));
    return icon;
}
 
Example 14
Source File: VectorDrawableCompat.java    From VectorChildFinder with Apache License 2.0 5 votes vote down vote up
@Override
public void setTintList(ColorStateList tint) {
    if (mDelegateDrawable != null) {
        DrawableCompat.setTintList(mDelegateDrawable, tint);
        return;
    }

    final VectorDrawableCompatState state = mVectorState;
    if (state.mTint != tint) {
        state.mTint = tint;
        mTintFilter = updateTintFilter(mTintFilter, tint, state.mTintMode);
        invalidateSelf();
    }
}
 
Example 15
Source File: AppUtils.java    From v9porn with MIT License 5 votes vote down vote up
/**
 * drawable 着色
 */
public static void setImageViewColor(ImageView view, int colorResId) {
    //mutate()
    Drawable modeDrawable = view.getDrawable().mutate();
    Drawable temp = DrawableCompat.wrap(modeDrawable);
    ColorStateList colorStateList = ColorStateList.valueOf(view.getResources().getColor(colorResId));
    DrawableCompat.setTintList(temp, colorStateList);
    view.setImageDrawable(temp);
}
 
Example 16
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 17
Source File: ScanView.java    From MeetMusic with Apache License 2.0 4 votes vote down vote up
public Drawable tintDrawable(Drawable drawable, ColorStateList colors) {
    Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
    DrawableCompat.setTintList(wrappedDrawable, colors);
    return wrappedDrawable;
}
 
Example 18
Source File: Tints.java    From AndroidCommons with Apache License 2.0 4 votes vote down vote up
private static Drawable tint(Drawable drawable, ColorStateList color) {
    Drawable wrapped = DrawableCompat.wrap(drawable);
    wrapped.mutate();
    DrawableCompat.setTintList(wrapped, color);
    return wrapped;
}
 
Example 19
Source File: ScanView.java    From AndroidDemo with MIT License 4 votes vote down vote up
public Drawable tintDrawable(Drawable drawable, ColorStateList colors) {
    Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
    DrawableCompat.setTintList(wrappedDrawable, colors);
    return wrappedDrawable;
}
 
Example 20
Source File: TintHelper.java    From nono-android with GNU General Public License v3.0 4 votes vote down vote up
public static Drawable tintDrawable(Drawable drawable, ColorStateList colors) {
    final Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
    DrawableCompat.setTintList(wrappedDrawable, colors);
    return wrappedDrawable;
}