【问题标题】:How to catch the moment of intersection line with touch?如何用触摸捕捉相交线的时刻?
【发布时间】:2014-03-28 06:52:51
【问题描述】:

应用程序正在关注。我在屏幕上有一条线。我触摸屏幕并移动手指。在我的手指碰到这条线的那一刻——我想在相交的那一刻显示一条消息。

代码:

    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction() ;
            case MotionEvent.ACTION_DOWN: 
                break;
            case MotionEvent.ACTION_MOVE:
                boolean isline = check_line(event.getX(),event.getY());
                if(isline){
                    //SHOW MESSAGE
                }
                break;
            case MotionEvent.ACTION_UP:   
                break;
            }
        return true;
    }

但应用程序有时无法捕捉手指在线的那一刻。如果我移动缓慢 - 那么消息总是会出现。但如果我移动得快一点 - 没有消息。

直线的方程是 A*x + B*y + C = 0

check_line 是一个数学函数,它只是将我的 x 和 y 放在这个等式中。因此,当方程满足时,它返回 true,否则 - false。

但我希望所有案件都被抓住。

怎么办?

【问题讨论】:

    标签: android multithreading touch android-canvas


    【解决方案1】:

    如果你移动得很快,那么你无法得到你移动的所有点,这就是你的方程出错的原因..

    解决方案:

    使用直线相交方程.. 对于线相交,您需要四个 x-y 值。您已经从行中获得了两个 x-y 值,第三个 x-y 值是当前触摸事件,第四个 x-y 值应该是最后一个触摸事件。

    int lastX;
    int lastY;
    int currentX;
    int currentY;
    
    case MotionEvent.ACTION_MOVE:
       currentX = event.getX();
       currentY = event.getY();
    
       //your line intersect code
    
    
       lastX = event.getX();
       lastY = event.getY();
    break;
    

    Line Intersect 的一些链接:

    Solution1

    Solution2

    【讨论】:

      【解决方案2】:

      要击中你当前已经击中的那条线,所以你应该增加“触摸范围”。

      例如,您可以制作一个具有一定半径的圆(其中心在 getX 和 getY 中)并检查一个圆是否与您的线相交。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多