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

The following examples show how to use android.graphics.drawable.Drawable#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: ProgressBar.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Should only be called if we've already verified that mProgressDrawable
 * and mProgressTintInfo are non-null.
 */
private void applyPrimaryProgressTint() {
    if (mProgressTintInfo.mHasProgressTint
            || mProgressTintInfo.mHasProgressTintMode) {
        final Drawable target = getTintTarget(R.id.progress, true);
        if (target != null) {
            if (mProgressTintInfo.mHasProgressTint) {
                target.setTintList(mProgressTintInfo.mProgressTintList);
            }
            if (mProgressTintInfo.mHasProgressTintMode) {
                target.setTintMode(mProgressTintInfo.mProgressTintMode);
            }

            // The drawable (or one of its children) may not have been
            // stateful before applying the tint, so let's try again.
            if (target.isStateful()) {
                target.setState(getDrawableState());
            }
        }
    }
}
 
Example 2
Source File: ProgressBar.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Should only be called if we've already verified that mProgressDrawable
 * and mProgressTintInfo are non-null.
 */
private void applyProgressBackgroundTint() {
    if (mProgressTintInfo.mHasProgressBackgroundTint
            || mProgressTintInfo.mHasProgressBackgroundTintMode) {
        final Drawable target = getTintTarget(R.id.background, false);
        if (target != null) {
            if (mProgressTintInfo.mHasProgressBackgroundTint) {
                target.setTintList(mProgressTintInfo.mProgressBackgroundTintList);
            }
            if (mProgressTintInfo.mHasProgressBackgroundTintMode) {
                target.setTintMode(mProgressTintInfo.mProgressBackgroundTintMode);
            }

            // The drawable (or one of its children) may not have been
            // stateful before applying the tint, so let's try again.
            if (target.isStateful()) {
                target.setState(getDrawableState());
            }
        }
    }
}
 
Example 3
Source File: ProgressBar.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Should only be called if we've already verified that mProgressDrawable
 * and mProgressTintInfo are non-null.
 */
private void applySecondaryProgressTint() {
    if (mProgressTintInfo.mHasSecondaryProgressTint
            || mProgressTintInfo.mHasSecondaryProgressTintMode) {
        final Drawable target = getTintTarget(R.id.secondaryProgress, false);
        if (target != null) {
            if (mProgressTintInfo.mHasSecondaryProgressTint) {
                target.setTintList(mProgressTintInfo.mSecondaryProgressTintList);
            }
            if (mProgressTintInfo.mHasSecondaryProgressTintMode) {
                target.setTintMode(mProgressTintInfo.mSecondaryProgressTintMode);
            }

            // The drawable (or one of its children) may not have been
            // stateful before applying the tint, so let's try again.
            if (target.isStateful()) {
                target.setState(getDrawableState());
            }
        }
    }
}
 
Example 4
Source File: IncDecCircular.java    From IncDec with Apache License 2.0 6 votes vote down vote up
/** Setting up the right button */
private void setupRightButton(FloatingActionButton rightButton, Drawable rightSrc,
                              int rightButtonTint, int rightDrawableTint) {
    if(rightSrc!=null) {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            rightSrc.setTintList(new ColorStateList(new int[][]{new int[]{0}},
                    new int[]{rightDrawableTint}));
        }
        else
        {
            final Drawable wrappedDrawable = DrawableCompat.wrap(rightSrc);
            DrawableCompat.setTintList(wrappedDrawable, ColorStateList.valueOf(rightDrawableTint));
        }
        rightButton.setImageDrawable(rightSrc);
    }
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        rightButton.setBackgroundTintList(new ColorStateList(new int[][]{new int[]{0}},
                new int[]{rightButtonTint}));
    }
    else
    {
        ViewCompat.setBackgroundTintList(rightButton, ColorStateList.valueOf(rightButtonTint));
    }
}
 
Example 5
Source File: IncDecCircular.java    From IncDec with Apache License 2.0 6 votes vote down vote up
/** Setting up the left button */
private void setupLeftButton(FloatingActionButton leftButton, Drawable leftSrc,
                             int leftButtonTint, int leftDrawableTint) {
    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));
    }

}
 
Example 6
Source File: IncDecImageButton.java    From IncDec with Apache License 2.0 6 votes vote down vote up
private void setupRightButton(ImageButton rightButton, Drawable rightSrc,
                              int rightButtonTint, int rightDrawableTint, Drawable background) {
    if(rightSrc!=null) {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            rightSrc.setTintList(new ColorStateList(new int[][]{new int[]{0}},
                    new int[]{rightDrawableTint}));
        }
        else
        {
            final Drawable wrappedDrawable = DrawableCompat.wrap(rightSrc);
            DrawableCompat.setTintList(wrappedDrawable, ColorStateList.valueOf(rightDrawableTint));
        }
        rightButton.setImageDrawable(rightSrc);
    }
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        rightButton.setBackgroundTintList(new ColorStateList(new int[][]{new int[]{0}},
                new int[]{rightButtonTint}));
    }
    else
    {
        ViewCompat.setBackgroundTintList(rightButton, ColorStateList.valueOf(rightButtonTint));
    }
    rightButton.setBackground(background);
}
 
Example 7
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 8
Source File: NotificationInfo.java    From LaunchEnr with GNU General Public License v3.0 5 votes vote down vote up
public Drawable getIconForBackground(Context context, int background) {
    if (mIsIconLarge) {
        // Only small icons should be tinted.
        return mIconDrawable;
    }
    mIconColor = IconPalette.resolveContrastColor(context, mIconColor, background);
    Drawable icon = mIconDrawable.mutate();
    // DrawableContainer ignores the color filter if it's already set, so clear it first to
    // get it set and invalidated properly.
    icon.setTintList(null);
    icon.setTint(mIconColor);
    return icon;
}
 
Example 9
Source File: Slider.java    From appinventor-extensions with Apache License 2.0 5 votes vote down vote up
private void setSliderColors() {
  if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
    seekbar.setProgressTintList(ColorStateList.valueOf(leftColor));
    if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP_MR1 ||
        !(seekbar.getProgressDrawable() instanceof StateListDrawable)) {
      seekbar.setProgressBackgroundTintList(ColorStateList.valueOf(rightColor));
      seekbar.setProgressBackgroundTintMode(Mode.MULTIPLY);
    } else {
      // Looking at the AOSP code, the previous calls should effectively accomplish what the
      // following code does... except it doesn't on Android 5.0. Instead, the result is that the
      // right side color is 50% opacity of leftColor. The following code works on Android 5.0,
      // but assumes a Drawable hierarchy that may not be true if the device manufacturer deviates
      // from the AOSP design. If that is the case, then the right hand side will not change.
      StateListDrawable drawable = (StateListDrawable) seekbar.getProgressDrawable();
      if (drawable.getCurrent() instanceof LayerDrawable) {
        LayerDrawable layerDrawable = (LayerDrawable) drawable.getCurrent();
        Drawable background = layerDrawable.findDrawableByLayerId(R.id.background);
        background.setTintList(ColorStateList.valueOf(rightColor));
        background.setTintMode(Mode.MULTIPLY);
      }
    }
  } else {
    LayerDrawable fullBar = (LayerDrawable) seekbar.getProgressDrawable();
    fullBar.setColorFilter(rightColor,PorterDuff.Mode.SRC);
    fullBar.findDrawableByLayerId(R.id.progress).setColorFilter(leftColor, PorterDuff.Mode.SRC);
  }
}
 
Example 10
Source File: MorphButton.java    From CanDialog with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void applyTint(Drawable d, TintInfo t) {
    if (d != null && t != null) {
        if (ResourcesCompat.LOLLIPOP) {
            if (t.mHasTintList || t.mHasTintMode) {
                d = d.mutate();
                if (t.mHasTintList) {
                    d.setTintList(t.mTintList);
                }
                if (t.mHasTintMode) {
                    d.setTintMode(t.mTintMode);
                }
            }
        } else if (d instanceof Tintable) {
            // Our VectorDrawable and AnimatedVectorDrawable implementation
            if (t.mHasTintList || t.mHasTintMode) {
                d = d.mutate();
                Tintable tintable = (Tintable) d;
                if (t.mHasTintList) {
                    tintable.setTintList(t.mTintList);
                }
                if (t.mHasTintMode) {
                    tintable.setTintMode(t.mTintMode);
                }
            }
        } else {
            //TODO: Should I attempt to make "stateful" ColorFilters from mBackgroundTint?
            if (t.mHasTintList) {
                int color = t.mTintList.getColorForState(getDrawableState(), Color.TRANSPARENT);
                setDrawableColorFilter(d, color, PorterDuff.Mode.SRC_IN);
            }

        }
    }
}
 
Example 11
Source File: MaterialProgressBar.java    From MaterialProgressBar with Apache License 2.0 5 votes vote down vote up
@SuppressLint("NewApi")
private void applyTintForDrawable(@NonNull Drawable drawable, @Nullable ColorStateList tint,
                                  boolean hasTint, @Nullable PorterDuff.Mode tintMode,
                                  boolean hasTintMode) {

    if (hasTint || hasTintMode) {

        if (hasTint) {
            if (drawable instanceof TintableDrawable) {
                //noinspection RedundantCast
                ((TintableDrawable) drawable).setTintList(tint);
            } else {
                logDrawableTintWarning();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    drawable.setTintList(tint);
                }
            }
        }

        if (hasTintMode) {
            if (drawable instanceof TintableDrawable) {
                //noinspection RedundantCast
                ((TintableDrawable) drawable).setTintMode(tintMode);
            } else {
                logDrawableTintWarning();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    drawable.setTintMode(tintMode);
                }
            }
        }

        // The drawable (or one of its children) may not have been
        // stateful before applying the tint, so let's try again.
        if (drawable.isStateful()) {
            drawable.setState(getDrawableState());
        }
    }
}
 
Example 12
Source File: LayerDrawable.java    From Carbon with Apache License 2.0 5 votes vote down vote up
@Override
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void setTintList(ColorStateList tint) {
    final ChildDrawable[] array = mLayerState.mChildren;
    final int N = mLayerState.mNum;
    for (int i = 0; i < N; i++) {
        final Drawable dr = array[i].mDrawable;
        if (dr != null) {
            dr.setTintList(tint);
        }
    }
}
 
Example 13
Source File: Carbon.java    From Carbon with Apache License 2.0 5 votes vote down vote up
public static void setTintListMode(Drawable drawable, ColorStateList tint, PorterDuff.Mode mode) {
    if (Carbon.IS_LOLLIPOP_OR_HIGHER) {
        drawable.setTintList(tint);
        drawable.setTintMode(mode);
    } else if (drawable instanceof TintAwareDrawable) {
        ((TintAwareDrawable) drawable).setTintList(tint);
        ((TintAwareDrawable) drawable).setTintMode(mode);
    } else {
        drawable.setColorFilter(tint == null ? null : new PorterDuffColorFilter(tint.getColorForState(drawable.getState(), tint.getDefaultColor()), mode));
    }
}
 
Example 14
Source File: Carbon.java    From Carbon with Apache License 2.0 5 votes vote down vote up
public static void clearTint(Drawable drawable) {
    if (Carbon.IS_LOLLIPOP_OR_HIGHER) {
        drawable.setTintList(null);
    } else if (drawable instanceof TintAwareDrawable) {
        ((TintAwareDrawable) drawable).setTintList(null);
    } else {
        drawable.setColorFilter(null);
    }
}
 
Example 15
Source File: DrawableCompatL.java    From adt-leanback-support with Apache License 2.0 4 votes vote down vote up
public static void setTintList(Drawable drawable, ColorStateList tint) {
    drawable.setTintList(tint);
}
 
Example 16
Source File: CanDialog.java    From CanDialog with Apache License 2.0 3 votes vote down vote up
/**
 * 设置svg图标颜色
 *
 * @param d     Drawable
 * @param color int
 */
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void applyTint(Drawable d, int color) {

    ColorStateList colorList = ColorStateList.valueOf(color);
    if (ResourcesCompat.LOLLIPOP) {

        d = d.mutate();

        d.setTintList(colorList);


        d.setTintMode(PorterDuff.Mode.SRC_IN);


    } else if (d instanceof Tintable) {

        d = d.mutate();
        Tintable tintable = (Tintable) d;

        tintable.setTintList(colorList);


        tintable.setTintMode(PorterDuff.Mode.SRC_IN);

    } else {

        int colorF = colorList.getColorForState(getDrawableState(), Color.TRANSPARENT);
        d.setColorFilter(colorF, PorterDuff.Mode.SRC_IN);

    }

}