繁体   English   中英

如何只允许在“浮动操作按钮”的圆圈内单击?

[英]How to allow clicks inside Floating Action Button's circle only?

这些是遵循Google规范的FAB尺寸。 在此处输入图片说明

在我的应用程序案例中,我需要一个Click侦听器,该侦听器仅通过单击FAB的圆圈来触发,从而避免在方形容器(56x56)中单击。

我已经在考虑在我的“整个”按钮中的每个onClick事件中获取X,Y,然后验证它是否在Fab的圈子内,但是我想摆脱这种解决方法,因为这可能会导致不同设备分辨率的准确性单击问题。 。

您有替代解决方案的想法吗?

提前致谢。

编辑:

我实际上已经实现了用Java翻译的javascript解决方案
链接
您是否认为我在设备屏幕更改上将始终获得相同的准确性?

我认为除了矩形之外,没有其他方法可以使可点击区域真正变为现实。 如果圆是图像,则应将浮动按钮的宽度和高度设置为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;
                                    }
                                }
                            }
    );

请记住,要执行此操作,您的按钮应严格为矩形。 如果您希望按钮为椭圆形,则数学应该不同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM