Last active
August 29, 2015 14:23
-
-
Save jakubkinst/2f677de94c15b87c54ed to your computer and use it in GitHub Desktop.
Android TintableImageView compat
This file contains hidden or 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
<?xml version="1.0" encoding="UTF-8"?> | |
<resources> | |
<declare-styleable name="TintableImageView"> | |
<attr name="tint" format="color"/> | |
</declare-styleable> | |
</resources> |
This file contains hidden or 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
package cz.kinst.jakub.tintableimageview; | |
import android.content.Context; | |
import android.content.res.TypedArray; | |
import android.graphics.Color; | |
import android.graphics.PorterDuff; | |
import android.support.annotation.ColorInt; | |
import android.support.annotation.ColorRes; | |
import android.util.AttributeSet; | |
import android.widget.ImageView; | |
public class TintableImageView extends ImageView { | |
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 a = context.obtainStyledAttributes(attrs, R.styleable.TintableImageView, defStyle, 0); | |
int tintColor = a.getColor(R.styleable.TintableImageView_tint, Color.BLACK); | |
a.recycle(); | |
setTintColor(tintColor); | |
} | |
public void setTintColor(@ColorInt int color) { | |
super.setColorFilter(color, PorterDuff.Mode.SRC_IN); | |
} | |
public void setTintColorResource(@ColorRes int colorResource) { | |
super.setColorFilter(getContext().getResources().getColor(colorResource), PorterDuff.Mode.SRC_IN); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment