Skip to content

Instantly share code, notes, and snippets.

@Krishan14sharma
Created December 25, 2014 04:31
Show Gist options
  • Save Krishan14sharma/8ae76d6f97da38b9eb04 to your computer and use it in GitHub Desktop.
Save Krishan14sharma/8ae76d6f97da38b9eb04 to your computer and use it in GitHub Desktop.
<declare-styleable name="TintableImageView">
<attr name="tint" format="reference|color" />
</declare-styleable>
package com.dnn.zapbuild.sports.view;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.ImageView;
import com.dnn.zapbuild.sports.R;
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 a = context.obtainStyledAttributes(attrs, R.styleable.TintableImageView, defStyle, 0);
tint = a.getColorStateList(R.styleable.TintableImageView_tint);
a.recycle();
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
if (tint != null && tint.isStateful())
updateTintColor();
}
public void setColorFilter(ColorStateList tint) {
this.tint = tint;
super.setColorFilter(tint.getColorForState(getDrawableState(), 0));
}
private void updateTintColor() {
int color = tint.getColorForState(getDrawableState(), 0);
setColorFilter(color);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment