Android:如何停止调用View.OnTouchListener ACTION_MOVE

时间:2012-06-12 03:14:31

标签: android view ontouchlistener

我使用LinearLayout ViewTouchListener进行了简单的幻灯片操作。

当我触摸和移动时,问题是MotionEvent.ACTION_MOVE一直被调用。

如何停止调用MotionEvent.ACTION_MOVE

v.setOnTouchListener(null)不好:返回false并且无效。

我希望MotionEvent.ACTION_MOVE只能被调用一次。

    private View.OnTouchListener touchEvent = new View.OnTouchListener() {
        private float tempX;
        private final float BASE_VALUE = 100;
        private final boolean LEFT = false;
        private final boolean RIGHT = true;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub

            if(event.getAction() == MotionEvent.ACTION_DOWN){
                tempX = event.getX();
            }

            if(event.getAction() == MotionEvent.ACTION_MOVE){
                if(event.getX() < (tempX + BASE_VALUE)){
                    changeView(LEFT);
                }else if(event.getX() > (tempX + BASE_VALUE)){
                    changeView(RIGHT);
                }
            }

效果不佳。

但是这个实现并不错(这个代码是我的想法)。

代码:

private View.OnTouchListener touchEvent = new View.OnTouchListener() {
    private VelocityTracker mVelocityTracker;
    private float tempX;
    private final int MIN_DISTANCE = 120;
    private final int THRESHOLD_VELOCITY = 200; 
    private final boolean LEFT = false;
    private final boolean RIGHT = true;
    private MotionEvent cancelEvent = MotionEvent.obtain(100, 100, MotionEvent.ACTION_CANCEL, 0, 0, 0);
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        if(event.getAction() == MotionEvent.ACTION_DOWN){
            Log.d(TAG, "Down");
            mVelocityTracker = VelocityTracker.obtain();
            mVelocityTracker.addMovement(event);
            tempX = event.getX();
        }
        if(event.getAction() == MotionEvent.ACTION_MOVE){
            mVelocityTracker.addMovement(event);
            mVelocityTracker.computeCurrentVelocity(100);
            float velocityX = mVelocityTracker.getXVelocity();
            if(tempX - event.getX() > MIN_DISTANCE 
                   && Math.abs(velocityX) > THRESHOLD_VELOCITY){
                   Log.d(TAG, "LEFT");
                   changeView(LEFT);
            }else if(event.getX() - tempX > MIN_DISTANCE 
                   && Math.abs(velocityX) > THRESHOLD_VELOCITY){
                   Log.d(TAG, "RIGHT");
                   changeView(RIGHT);
            }
            v.dispatchTouchEvent(cancelEvent);
        }

2 个答案:

答案 0 :(得分:2)

制作SWIPELISTENER。

守则。

import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;


public abstract class OnSwipeListener implements OnTouchListener {
public final static int SWIPED_LEFT = 1;
public final static int SWIPED_RIGHT = 2;
public final static int SWIPED_UP = 3;
public final static int SWIPED_DOWN = 4;
public final static int CLICKED = 5;

private int SWIPE_ACTION = 0;
private float firstTouchX = 0;
private float firstTouchY = 0;
private float lastTouchX = 0;
private float lastTouchY = 0;
private int activePointerID;

@Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    if(event.getAction() == event.ACTION_DOWN){
        final float x = event.getX();
        final float y = event.getY();

        firstTouchX = x;
        firstTouchY = y;

        activePointerID = event.getPointerId(0);

        v.setBackgroundResource(R.drawable.layoutbutton2);
    }

    if(event.getAction() == event.ACTION_UP){
        float dX = lastTouchX - firstTouchX;
        float dY = lastTouchY - firstTouchY;

        final int pointerIndex = event.findPointerIndex(activePointerID);

        if((firstTouchX + 10) == event.getX(activePointerID) &&
                (firstTouchY + 10) == event.getY(activePointerID)){

            SWIPE_ACTION = CLICKED;

        }else{
            if(Math.abs(dX) > Math.abs(dY)){
                if(dX > 0){
                    SWIPE_ACTION = SWIPED_RIGHT;
                }else{
                    SWIPE_ACTION = SWIPED_LEFT;
                }
            }else{
                if(dY > 0){
                    SWIPE_ACTION = SWIPED_DOWN;
                }else{
                    SWIPE_ACTION = SWIPED_UP;
                }
            }
        }
        v.setBackgroundResource(R.drawable.layoutbutton1);
        onSwipe(v, event);
    }
    if(event.getAction() == event.ACTION_MOVE){
        final int pointerIndex = event.findPointerIndex(activePointerID);
        final float x = event.getX(pointerIndex);
        final float y = event.getY(pointerIndex);

        lastTouchX = x;
        lastTouchY = y;

        v.setBackgroundResource(R.drawable.layoutbutton1);
    }
    if(event.getAction() == event.ACTION_CANCEL){
        v.setBackgroundResource(R.drawable.layoutbutton1);
    }

    return true;

}

public int getSwipeTo(){
    return SWIPE_ACTION;
}

abstract public void onSwipe(View v, MotionEvent event);

}

答案 1 :(得分:0)

我假设您在询问用户抬起手指时如何终止活动?您可以调用ACTION_UP,就像用户将手指从设备上抬起一样。

 if(event.getAction() == MotionEvent.ACTION_UP){
           //your code, maybe tempX = null;
        }

然后,您可以重新初始化您的程序,以便可以让其他用户触摸。