Skip to content

Instantly share code, notes, and snippets.

@rakuishi
Created March 16, 2015 10:11
Show Gist options
  • Save rakuishi/02fc20a3f1826c5b2950 to your computer and use it in GitHub Desktop.
Save rakuishi/02fc20a3f1826c5b2950 to your computer and use it in GitHub Desktop.
import android.content.Context;
import android.graphics.PorterDuff;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageButton;
public class TouchImageButton extends ImageButton {
public TouchImageButton(Context context) {
super(context);
}
public TouchImageButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TouchImageButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void setEnabled(boolean enabled) {
if (this.getDrawable() == null) {
return;
}
if (enabled) {
this.getDrawable().clearColorFilter();
} else {
this.getDrawable().setColorFilter(0x55FFFFFF, PorterDuff.Mode.MULTIPLY);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (this.getDrawable() == null || !this.isEnabled()) {
return super.onTouchEvent(event);
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
this.getDrawable().setColorFilter(0x55FFFFFF, PorterDuff.Mode.MULTIPLY);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
this.getDrawable().clearColorFilter();
break;
default:
break;
}
return super.onTouchEvent(event);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment