Skip to content

Instantly share code, notes, and snippets.

@watabee
Created August 2, 2017 14:48
Show Gist options
  • Save watabee/a505c9dbd946a97dd8a5e87672547674 to your computer and use it in GitHub Desktop.
Save watabee/a505c9dbd946a97dd8a5e87672547674 to your computer and use it in GitHub Desktop.
An ImageView which can highlight.
import android.content.Context;
import android.graphics.ColorFilter;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
public class HighlightableImageView extends AppCompatImageView {
private static final ColorFilter COLOR_FILTER = new PorterDuffColorFilter(0x40000000, PorterDuff.Mode.SRC_ATOP);
public HighlightableImageView(final Context context) {
this(context, null);
}
public HighlightableImageView(final Context context, final AttributeSet attrs) {
this(context, attrs, 0);
}
public HighlightableImageView(final Context context, final AttributeSet attrs,
final int defStyleAttr) {
super(context, attrs, defStyleAttr);
setClickable(true);
}
@Override
public void setPressed(final boolean pressed) {
super.setPressed(pressed);
if (pressed) {
setColorFilter(COLOR_FILTER);
} else {
clearColorFilter();
}
invalidate();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment