Search code examples
androideventsmotion

android how to get last movement


For example, I move my finger down the screen and then back up. So, this should count as two drags, the last movement before I paused for a split second and after that when I moved back up. I basically count every time I make a new movement without lifting my finger off the screen. So, how do I get the last movement before I stop movement without lifting my finger?

I'm using motion event. Here is the code in action_move:

case MotionEvent.ACTION_MOVE:

        posY = event.getY();
        posX = event.getX();
        diffPosY = posY - oldY;
        diffPosX = posX - oldX;

        if (checkMovement(posY, oldY)){
            if (diffPosY > 0 || diffPosY < 0){

                count +=1;


                }
        } 


    public boolean checkMovement(float posY, float oldY) {

    int newY = Math.round(posY);
    double distance = Math.abs(newY - oldY);

    oldY = newY;

    if (distance < 25)


    return false;

    return true;
}   

Solution

  • Simple like this

    private int mLastMovY = 0;
    
    case MotionEvent.ACTION_MOVE:
    
            posY = event.getY();
            posX = event.getX();
            diffPosY = posY - oldY;
            diffPosX = posX - oldX;
            if(diffPosY > 0){//up
                if(mLastMovY != 0){//if have any drag down before, the value will != 0
                    count +=1; 
                    //could save value of mLastMovY before reset it, this is last position when user drag down
                    mLastMovY = 0;//reset it to avoid 'count' be increased
                }
            }
            else{//down
                mLastMovY = posY;//drag down will assign value to mLastMovY
            }