Created
March 9, 2015 21:41
-
-
Save johnybot/61bf48309d7e288691bc to your computer and use it in GitHub Desktop.
TintableImageView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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