Skip to content

Instantly share code, notes, and snippets.

@johnybot
Created March 9, 2015 21:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save johnybot/61bf48309d7e288691bc to your computer and use it in GitHub Desktop.
Save johnybot/61bf48309d7e288691bc to your computer and use it in GitHub Desktop.
TintableImageView
public class TintableImageView extends ImageView {
private ColorStateList tint;
public TintableImageView(Context context) {
super(context);
}
public TintableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, 0);
}
public TintableImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs, defStyle);
}
private void init(Context context, AttributeSet attrs, int defStyle) {
TypedArray typedArray = context.obtainStyledAttributes(
attrs,
R.styleable.TintableImageView,
defStyle,
0
);
tint = typedArray.getColorStateList(R.styleable.TintableImageView_tint);
typedArray.recycle();
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
if (tint != null) {
setColorFilter(tint.getColorForState(getDrawableState(), tint.getDefaultColor()), PorterDuff.Mode.SRC_ATOP);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment