使用自定义TintableImageView
importandroid.content.Context;
importandroid.content.res.ColorStateList;
importandroid.content.res.TypedArray;
importandroid.util.AttributeSet;
importcom.chianyu.circle.client.R;
/**
*@Description支持tintList着色ImageView
*/
public classTintableImageViewextendsandroid.support.v7.widget.AppCompatImageView {
privateColorStateListtint;
publicTintableImageView(Context context) {
super(context);
}
publicTintableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs,0);
}
private voidinit(Context context, AttributeSet attrs,intdefStyle) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TintableImageView, defStyle,0);
tint= a.getColorStateList(R.styleable.TintableImageView_tint);
a.recycle();
}
publicTintableImageView(Context context, AttributeSet attrs,intdefStyle) {
super(context, attrs, defStyle);
init(context, attrs, defStyle);
}
public voidsetColorFilter(ColorStateList tint) {
this.tint= tint;
super.setColorFilter(tint.getColorForState(getDrawableState(),0));
}
public voidsetTintList(ColorStateList colorList) {
if(colorList !=null) {
tint= colorList;
drawableStateChanged();
}
}
@Override
protected voiddrawableStateChanged() {
super.drawableStateChanged();
if(tint!=null&&tint.isStateful())
updateTintColor();
}
private voidupdateTintColor() {
intcolor =tint.getColorForState(getDrawableState(),0);
setColorFilter(color);
}
}
网友评论