Motionevent Action_MOVE keeps firing X and Y even

2019-07-13 16:54发布

I am trying out simple program that have sounds based if moved. so at the beginning I have down - which play sound 1 and from then every move it is keeps playing a sound. At count 4 I have made it to play from start.

here is the problem: When I don't move my finger and hold it in the same place the sound still keeps 1 by 1 - Figured out the x and y value firing. How do I stop this??

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

1条回答
不美不萌又怎样
2楼-- · 2019-07-13 17:51

MOVE is very sensitive and will continue to be called as long as your finger is down. Set Old Value at the end of the sound playing code so it will only play if moved another 50 distance from that spot.

Something like this.

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;
    }
查看更多
登录 后发表回答