下面列出了android.widget.ImageView#setImageTintList ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void setBottomIconOriColor(BottomNavigationView bottomView) {
try {
Field mMenuViewField = BottomNavigationView.class.getDeclaredField("mMenuView");
mMenuViewField.setAccessible(true);
BottomNavigationMenuView mMenuView = (BottomNavigationMenuView) mMenuViewField.get(bottomView);
Field mButtonsField = BottomNavigationMenuView.class.getDeclaredField("mButtons");
mButtonsField.setAccessible(true);
BottomNavigationItemView[] mButtons = (BottomNavigationItemView[]) mButtonsField.get(mMenuView);
Field mIconField = BottomNavigationItemView.class.getDeclaredField("mIcon");
mIconField.setAccessible(true);
for (BottomNavigationItemView item : mButtons) {
ImageView mIcon = (ImageView) mIconField.get(item);
mIcon.setImageTintList(null);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void setImageTint(ImageView imageView, ColorStateList tint, PorterDuff.Mode
tintMode) {
if (imageView.getDrawable() == null) {
return;
}
if (tint == null && tintMode == null) {
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (mActionTint != null) {
imageView.setImageTintList(mActionTint);
}
if (mActionTintMode != null) {
imageView.setImageTintMode(mActionTintMode);
}
} else {
Drawable drawable = imageView.getDrawable();
if (drawable != null && mActionTint != null) {
drawable = drawable.mutate();
drawable.setColorFilter(mActionTint.getDefaultColor(), mActionTintMode != null ? mActionTintMode : PorterDuff.Mode.SRC_ATOP);
}
}
}
public static void setTint(@NonNull ImageView imageView, @ColorInt int color) {
ColorStateList s1 = ColorStateList.valueOf(color);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
imageView.setImageTintList(s1);
} else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1) {
Drawable drawable = DrawableCompat.wrap(imageView.getDrawable());
imageView.setImageDrawable(drawable);
DrawableCompat.setTintList(drawable, s1);
} else {
PorterDuff.Mode mode = PorterDuff.Mode.SRC_IN;
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
mode = PorterDuff.Mode.MULTIPLY;
}
if (imageView.getDrawable() != null)
imageView.getDrawable().setColorFilter(color, mode);
}
}
@NonNull
private ImageView createArrow() {
ImageView arrow = (ImageView) inflater.inflate(R.layout.breadcrumbs_arrow, this, false);
arrow.setImageResource(arrowRes);
arrow.setImageTintList(ColorStateList.valueOf(mColor));
return arrow;
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static void setTint(@NonNull ImageView view, @ColorInt int color) {
int[] colors = new int[]{color, color, color, color, color, color};
int[][] states = new int[6][];
states[0] = new int[]{android.R.attr.state_checked, android.R.attr.state_enabled};
states[1] = new int[]{android.R.attr.state_enabled, android.R.attr.state_focused};
states[2] = new int[]{android.R.attr.state_enabled};
states[3] = new int[]{android.R.attr.state_focused};
states[4] = new int[]{android.R.attr.state_window_focused};
states[5] = new int[]{};
view.setImageTintList(new ColorStateList(states, colors));
}
@Override
public void applyColor(ImageView view, @ColorInt int color) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
view.setImageTintList(Utils.colorToStateList(color));
}else{
view.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
}
}
public static void setImageTintList(ImageView imageView, ColorStateList colorStateList) {
if (isApi_21_OrHigher()) {
imageView.setImageTintList(colorStateList);
} else {
Drawable drawable = imageView.getDrawable();
if (drawable != null) {
Drawable wrapped = DrawableCompat.wrap(drawable);
DrawableCompat.setTintList(wrapped, colorStateList);
imageView.setImageDrawable(wrapped);
}
}
}
public static void setImageTintColor(@NonNull ImageView view, @ColorRes int res) {
view.setImageTintList(ColorStateList.valueOf(ContextCompat.getColor(view.getContext(), res)));
}
private void initView(Context context, AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs,
R.styleable.SwipeTodoView);
Drawable centerIconBg = typedArray.getDrawable(R.styleable.SwipeTodoView_tic_centerBtnBg);
int leftIconResId = typedArray.getResourceId(R.styleable.SwipeTodoView_tic_leftBtnImage, 0);
int rightIconResId = typedArray.getResourceId(R.styleable
.SwipeTodoView_tic_rightBtnImage, 0);
mShowLeftButton = (0 != leftIconResId);
mShowRightButton = (0 != rightIconResId);
int centerIconResId = typedArray.getResourceId(R.styleable
.SwipeTodoView_tic_centerBtnImage, 0);
ColorStateList defaultColorList = ColorStateList.valueOf(Color.BLUE);
ColorStateList leftColorStateList = typedArray.getColorStateList(R.styleable
.SwipeTodoView_tic_leftBtnColor);
if (null == leftColorStateList) {
leftColorStateList = defaultColorList;
}
ColorStateList rightColorStateList = typedArray.getColorStateList(R.styleable
.SwipeTodoView_tic_rightBtnColor);
if (null == rightColorStateList) {
rightColorStateList = defaultColorList;
}
ColorStateList leftBgColor = typedArray.getColorStateList(R.styleable
.SwipeTodoView_tic_leftBtnBgColor);
ColorStateList rightBgColor = typedArray.getColorStateList(R.styleable
.SwipeTodoView_tic_rightBtnBgColor);
mLeftBgDrawable = new ArcDrawable(Color.WHITE);
mLeftBgDrawable.setTintList(leftBgColor);
mLeftBgDrawable.setGravity(Gravity.LEFT);
mLeftBgDrawable.setAlpha(0);
mRightBgDrawable = new ArcDrawable(Color.WHITE);
mRightBgDrawable.setGravity(Gravity.RIGHT);
mRightBgDrawable.setTintList(rightBgColor);
mRightBgDrawable.setAlpha(0);
String content = typedArray.getString(R.styleable.SwipeTodoView_tic_content);
String subContent = typedArray.getString(R.styleable.SwipeTodoView_tic_subContent);
typedArray.recycle();
mOuterCircleIv = (ImageView) findViewById(R.id.outer_circle_iv);
mMiddleCircleIv = (ImageView) findViewById(R.id.middle_circle_iv);
mInnerCircleIv = (ImageView) findViewById(R.id.inner_circle_iv);
mContentTv = (TextView) findViewById(R.id.content_tv);
mSubContentTv = (TextView) findViewById(R.id.sub_content_tv);
mTipTv = (TextView) findViewById(R.id.tip_tv);
mCenterIv = (ImageView) findViewById(R.id.center_iv);
mLeftIv = (ImageView) findViewById(R.id.left_iv);
mRightIv = (ImageView) findViewById(R.id.right_iv);
mContentTv.setText(content);
mSubContentTv.setText(subContent);
mCenterIv.setBackground(centerIconBg);
if (centerIconResId != 0) {
mHasCenterIcon = true;
mCenterIv.setImageResource(centerIconResId);
mCenterIv.getDrawable().setAlpha(255);
}
mLeftIv.setImageResource(leftIconResId);
mLeftIv.setImageTintList(leftColorStateList);
mLeftIv.setBackground(mLeftBgDrawable);
mRightIv.setImageResource(rightIconResId);
mRightIv.setImageTintList(rightColorStateList);
mRightIv.setBackground(mRightBgDrawable);
}
public static void setImageTintList(ImageView imageView, ColorStateList colorStateList) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
imageView.setImageTintList(colorStateList);
}
}
private void handleUserAvatar(ImageView avatarIV, String url) {
avatarIV.setImageTintList(null);
ImageUtils.loadRoundCornerAvatar(avatarIV, url);
}