1

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

到目前为止,我已经像这样设置了我的 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;


}

有什么建议么?

/米

4

1 回答 1

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;
}

有一个伟大的一周!/米

于 2012-12-16T15:26:52.850 回答