我不认为有一种方法,实际上使可点击区域矩形之外。 如果你的圆圈是图像,你应该让你的浮动按钮的宽度和高度设置为wrap_content。任何方式你应该设置它的宽度和高度以匹配圆的直径。而处理该事件是这样的:
view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
float radius = layoutParams.width/2f;
float x = event.getX() - radius;
float y = event.getY() - radius;
if (isInCircle(radius, x, y)) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// perform your action
}
return true;
} else {
return false;
}
}
private boolean isInCircle(float radius, float x, float y) {
if (Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)) < radius) {
//touch was performed inside the circle;
return true;
} else {
//touched ouside the circle
return false;
}
}
}
);
这将任何设备上工作,只要你的按钮在你绘制的圆紧紧包裹着。
如果不需要触摸事件陷入更深,您的浮动按钮下,当你错过了一圈,就可以避免一些重复计算是这样的:
view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
float radius = layoutParams.width/2f;
float x = event.getX() - radius;
float y = event.getY() - radius;
if (isInCircle(radius, x, y)) {
// perform your action
}
}
return true;
}
private boolean isInCircle(float radius, float x, float y) {
if (Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)) < radius) {
//touch was performed inside the circle;
return true;
} else {
//touched ouside the circle
return false;
}
}
}
);
请记住,这个工作你的按钮应该是严格的矩形。如果你想要一个按钮是一个椭圆,数学应该是不同的。
你可以只使用两个按钮,放置一个在工厂的顶部... – Dportology
我有一个自定义的椭圆形按钮同样的问题..的onclick在盒子发射反正...所以这不是只有浮动动作按钮我想,但在布局一般.. 它看起来像他们必须只是平方.. –