Skip to content

Instantly share code, notes, and snippets.

@MarkoMilos
Forked from johnybot/TintableImageView.java
Last active July 6, 2017 08:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarkoMilos/5b225832cfd8fe1994e0 to your computer and use it in GitHub Desktop.
Save MarkoMilos/5b225832cfd8fe1994e0 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<declare-styleable name="TintableImageView">
<attr name="tint" format="reference|color" />
</declare-styleable>
</resources>
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