4

我正在尝试一个简单的程序,它在移动时会发出声音。所以一开始我就失败了——它播放声音 1,从那时起它的每一个动作都在播放声音。在计数 4 时,我已经让它从头开始播放。

这是问题所在:当我不移动手指并将其保持在同一位置时,声音仍保持 1 比 1 - 计算出 x 和 y 值触发。我该如何阻止这个?

OnTouchListener MyOnTouchListener= new OnTouchListener()
{
public boolean onTouch(View view, MotionEvent event)
    {
    switch(event.getAction() & MotionEvent.ACTION_MASK)
        {

         case MotionEvent.ACTION_DOWN:
           x = (int) event.getX();
            y = (int) event.getY();    
            oldval = x+y;
            break;

    case MotionEvent.ACTION_MOVE:
        {
          Log.e("X value", "X is "+x);
          Log.e("Y value", "Y is "+y);
        try 
            {
                Thread.sleep(500);
            } catch (InterruptedException e) {
            }

            int newval= (int) (event.getX() + event.getY()); 

            if(Math.abs(oldval-newval)>50)
            {

                Log.e("First", "next button");
                longpressCount++;
                if(longpressCount==1)
                {
                   Log.e("1", "BUTTON PRESSED");
                }
                else if(longpressCount==2)
                {
                    Log.e("2", "BUTTON PRESSED");
                }
                else if(longpressCount==3)
                {
                    Log.e("3", "BUTTON PRESSED");
                }
                else if(longpressCount==4)
                {
                    Log.e("4", "BUTTON PRESSED");
                    longpressCount = 0;
                }

            }

            break;
         }
        }
        return true;
    }
4

1 回答 1

12

MOVE 非常敏感,只要您的手指向下,就会继续调用。在声音播放代码的末尾设置旧值,这样它只会在距离该位置再移动 50 距离时播放。

像这样的东西。

OnTouchListener MyOnTouchListener= new OnTouchListener()
{
public boolean onTouch(View view, MotionEvent event)
    {
    switch(event.getAction() & MotionEvent.ACTION_MASK)
        {

         case MotionEvent.ACTION_DOWN:
           x = (int) event.getX();
            y = (int) event.getY();    
            oldval = x+y;
            break;

    case MotionEvent.ACTION_MOVE:
        {
          Log.e("X value", "X is "+x);
          Log.e("Y value", "Y is "+y);
        try 
            {
                Thread.sleep(500);
            } catch (InterruptedException e) {
            }

            int newval= (int) (event.getX() + event.getY()); 

            if(Math.abs(oldval-newval)>50)
            {

                Log.e("First", "next button");
                longpressCount++;
                if(longpressCount==1)
                {
                   Log.e("1", "BUTTON PRESSED");
                }
                else if(longpressCount==2)
                {
                    Log.e("2", "BUTTON PRESSED");
                }
                else if(longpressCount==3)
                {
                    Log.e("3", "BUTTON PRESSED");
                }
                else if(longpressCount==4)
                {
                    Log.e("4", "BUTTON PRESSED");
                    longpressCount = 0;
                }
                oldval = event.getX() + event.getY();
            }

            break;
         }
        }
        return true;
    }
于 2012-09-08T07:58:40.133 回答