Motionevent Action_MOVE keeps firing X and Y even if not moved

倖福魔咒の 提交于 2019-12-23 12:27:24

问题


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:


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


来源:https://stackoverflow.com/questions/12328867/motionevent-action-move-keeps-firing-x-and-y-even-if-not-moved

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!