下面列出了android.graphics.drawable.Drawable#setTintMode() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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());
}
}
}
}
/**
* 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());
}
}
}
}
/**
* 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());
}
}
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.list_item, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
ListItem listItem = getItem(position);
Drawable drawable = mContext.getDrawable(R.drawable.person);
drawable.setTintMode(PorterDuff.Mode.SRC_ATOP);
drawable.setTint(listItem.getColor());
viewHolder.imageView.setImageDrawable(drawable);
viewHolder.count.setText(listItem.getCount());
viewHolder.title.setText(listItem.getTitle());
viewHolder.subtitle.setText(listItem.getSubtitle());
return convertView;
}
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);
}
}
@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);
}
}
}
}
@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());
}
}
}
@Override
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void setTintMode(PorterDuff.Mode tintMode) {
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.setTintMode(tintMode);
}
}
}
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));
}
}
private void setContactImage(boolean tint) {
if (!(contactImageString == null)) {
Uri contactImageUri = Uri.parse(contactImageString);
try {
InputStream contactImageStream = contentResolver.openInputStream(contactImageUri);
Drawable contactImage = Drawable.createFromStream(contactImageStream, contactImageUri.toString());
if(tint) {
contactImage.setTint(getResources().getColor(R.color.contact_photo_tint));
contactImage.setTintMode(PorterDuff.Mode.DARKEN);
}
contactPhoto.setImageDrawable(contactImage);
} catch (Exception e) {
}
}
}
public static void setTintMode(Drawable drawable, PorterDuff.Mode tintMode) {
drawable.setTintMode(tintMode);
}
/**
* 设置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);
}
}