0

I'm currently using the MotionEvent.ACTION_MOVE function to detect if a user moves an object (in this case, an ImageView).

The problem is that now my algorithm gets more complicated and I need to distinguish if a user clicks on this object or moves it.

I tried to use the MotionEvent.ACTION_DOWN function but the problem is that, each time I click on the object, the MotionEvent.ACTION_MOVE is fired too.

How can achieve this? (Code is very welcome)

Thanks in advance.

== EDIT ==

Here is my code :

img_view.setOnTouchListener(new View.OnTouchListener()
{   
    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        switch(event.getAction())
        {
                case MotionEvent.ACTION_DOWN :
            {
            }                   
            break;

            case MotionEvent.ACTION_MOVE :
            {
            }
                    break;  

            case MotionEvent.ACTION_UP :
            {
            }
            break;
            }

        return true;
    }
});
thomaus
  • 6,170
  • 8
  • 45
  • 63

2 Answers2

1

For detecting moving around, you want to check if someone's finger... is moving around :)

Check if the position is changing, while in ACTION_DOWN. If it is, by a certain degree, you know the user is dragging his finger across the screen.

You might want to take a look at this tutorial project, it's a simple example of an ImageView with multiple actions such as dragging, clicking and pinching.

Stefan de Bruijn
  • 6,289
  • 2
  • 23
  • 31
0

Answer :

        final GestureDetector gesture_getector = new GestureDetector(HomeActivity.this, new GestureListener());
    view_img.setOnTouchListener(new View.OnTouchListener()
    {
        @Override
        public boolean onTouch(final View view, final MotionEvent event)
        {
            if (gesture_getector.onTouchEvent(event))
            {
                return true;
            }

                // End of move object detection
            if (event.getAction() == MotionEvent.ACTION_UP)
            {
                if (cursor_is_scrolling)
                {
                    cursor_is_scrolling  = false;                       
                };
            }

            return false;
        }
    });


private class GestureListener extends SimpleOnGestureListener
{
    @Override
    public boolean onDown(MotionEvent e) { return true; }

    // Single click detection
    @Override
    public boolean onSingleTapUp(MotionEvent e)
    {
        return true;
    }

    // Move object detection
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
    {
        cursor_is_scrolling  = true;

        return true;
    }
}
thomaus
  • 6,170
  • 8
  • 45
  • 63