移动几个单位后触摸事件

时间:2012-12-14 14:17:27

标签: android motionevent

我正在尝试制作触摸事件,直到手指从初始位置移动了几个单位后才会激活。

到目前为止,我已经设置了这样的onTouch方法:

private XYEvents xyEvent =  new XYEvents();

public boolean motionTracker(MotionEvent event, int n)
{
    int note = n;   

    switch(event.getAction())
    {
    case MotionEvent.ACTION_DOWN:
            xyEvent.setInitial(event);
            playNote(note);

        break; 


    case MotionEvent.ACTION_MOVE:
        byte data1;
        byte data2;

//I figured I should input a condition to check if the finger has moved a few units before it should start doing stuff like so:

            if (xyEvent.getXThreshold(event))
            {
                int xMod = xyEvent.eventActions(event)[0];

                data1 = (byte) (xMod & 0x7f);
                data2 = (byte) ((xMod & 0x7f00) >> 8);
                xModulation((int)data1, (int)data2); 
            }


        break;
     }

这个方法是我遇到的问题:

private float initialX, initialY;
private int xValue; 

boolean getXThreshold(MotionEvent event)
{
    float deltaX = event.getX();
    float threshold = 10;

    float condition = (deltaX - initialX);

    if(condition <= threshold || condition >= -threshold )
        return false;
    else 
        return true;
}

getXThreshold方法似乎在另一个看起来像这样的方法中做了它应该做的事情:

public int[] eventActions(MotionEvent event)
{   
    int value = xValue;

    int xNull = 8192;


    if(!getXThreshold(event))
        xValue = xNull;


    if(getXThreshold(event))
        xValue = xHandleMove(event, true);


    return value;


}

有什么建议吗?

/ M

1 个答案:

答案 0 :(得分:3)

似乎这个论点:

if(condition <= threshold || condition >= -threshold )
    return false;
else 
    return true;

需要翻转,否则由于某种原因它总是返回false。

现在它看起来像这样,效果很好。

boolean getXThreshold(MotionEvent event)
{
    float deltaX = event.getX();
    float threshold = 10;

    float condition = (deltaX - initialX);

    return condition >= threshold || condition <= -threshold;
}

度过愉快的一周! / M