Search code examples
androideventsviewcoordinatesontouch

Get coordinates of start and end of a view Android


Im trying to get the start and end coordY of a view in Android to create a animation.

I tried with the event OnTouch but I just can access to the initial coordY.

I tried to with MotionEvent but it didnt work.

public boolean onTouch(View v, MotionEvent event)
        {
            float x = event.getX();
            float y = event.getY();
            //Log.d("S&R", "Y "+y);
            switch (event.getAction() & MotionEvent.ACTION_MASK)
            {
                case MotionEvent.ACTION_DOWN:
                    Log.d("S&R", "ACTION_DOWN");
                    break;
                case MotionEvent.ACTION_UP:
                    Log.d("S&R", "ACTION_UP");
                    break;
                case MotionEvent.ACTION_MOVE:
                    Log.d("S&R", "ACTION_MOVE");
                    break;
            }
            return false;
        }

I only want to know if if the touch was up-to-down or down-to-up.

How could I do it?

Thank you.


Solution

  • I got it!

    public boolean onTouch(View v, MotionEvent event) {
    
    
                boolean isReleased = event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL;
                boolean isPressed = event.getAction() == MotionEvent.ACTION_DOWN;
                boolean isMoved = event.getAction() == MotionEvent.ACTION_MOVE;
    
    
                if(isPressed){
                    Log.d("S&R","isPressed");
                    lastY=event.getY();
                    return true;
                }
    
                else if(isReleased){
                    Log.d("S&R","isReleased");
                    lastY=event.getY();
                    return true;
                }
    
                else if(isMoved)
                {
                   //Log.d("S&R","MOVE");
                   Log.d("S&R","lastY=> "+lastY+" currentY=> "+event.getY());
    
                   if(event.getY()<lastY)
                   {
                       Log.d("S&R","up");
    
                   }
                    else if(event.getY()>lastY)
                   {
                       Log.d("S&R","down");
    
                   }
                    lastY=event.getY();
                    return true;
                }
                return false;
            }
    

    Thanks for all guys.