/**
* Created by Huo on 2018/4/23.
*/
public class RhombusImageButton extends ImageButton {
// y = ax + b;
int a = 1;
int b = 106;
public RhombusImageButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
//不是按下事件不管
if(action != MotionEvent.ACTION_DOWN) {
return super.onTouchEvent( event);
}
Point center = new Point((getRight() + getLeft()) / 2, (getTop() + getBottom()) / 2);
float x = event.getRawX() - center.x;
float y = event.getRawY()- center.y;
//4根线限定菱形
if (!(y < a * x + b
&& y > a * x - b
&& y < -a * x + b
&& y > -a * x - b)) {
return false;
}
return super.onTouchEvent( event);
}
}
没什么好说的。。