android如何获得最后的动作

时间:2015-06-25 10:37:25

标签: android events motion

例如,我将手指向下移动屏幕,然后再向上移动。所以,这应该算作两个拖拽,在我暂停一瞬间之前的最后一个动作,之后当我向后移动时。每次我做一个新动作时,我都会计算,而不会将手指从屏幕上抬起。那么,如何在不抬起手指的情况下停止运动之前获得最后一次动作?

我正在使用动作事件。以下是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;
}   

1 个答案:

答案 0 :(得分:1)

像这样简单

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
        }