检测其他手势

时间:2014-05-02 11:21:25

标签: android logging

您好我正在使用它来检测左/右滑动并单击手势。

@Override
public boolean onTouchEvent(MotionEvent event) {
    int action = event.getAction();
    switch (action) {
        case MotionEvent.ACTION_DOWN:
            touchBegan(event);
            return true;
        case MotionEvent.ACTION_MOVE:
            touchMoved(event);
            return true;
        case MotionEvent.ACTION_UP:
            touchEnded(event);
            return true;
    }

    return false;
}

private void touchBegan(MotionEvent event)
{
    float x = event.getX();
    mTouchStartX = x;
    mTouchStartY = event.getY();
    mStartTime = AnimationUtils.currentAnimationTimeMillis();
    mStartOffset = mOffset;

    mTouchMoved = false;

    mTouchStartPos = (x / mWidth) * MOVE_POS_MULTIPLE - 5;
    mTouchStartPos /= 2;

    mVelocity = VelocityTracker.obtain();
    mVelocity.addMovement(event);
}

private void touchMoved(MotionEvent event) {
    float pos = (event.getX() / mWidth) * MOVE_POS_MULTIPLE - 5;
    pos /= 2;

    if (!mTouchMoved) {
        float dx = Math.abs(event.getX() - mTouchStartX);
        float dy = Math.abs(event.getY() - mTouchStartY);

        if (dx < TOUCH_MINIMUM_MOVE && dy < TOUCH_MINIMUM_MOVE)
            return;

        mTouchMoved = true;
    }

    mOffset = mStartOffset + mTouchStartPos - pos;

    invalidate();
    mVelocity.addMovement(event);
}

private void touchEnded(MotionEvent event) {
    float pos = (event.getX() / mWidth) * MOVE_POS_MULTIPLE - 5;
    pos /= 2;

    if (mTouchMoved) {
        mStartOffset += mTouchStartPos - pos;
        mOffset = mStartOffset;

        mVelocity.addMovement(event);

        mVelocity.computeCurrentVelocity(1000);
        double speed = mVelocity.getXVelocity();

        speed = (speed / mWidth) * MOVE_SPEED_MULTIPLE;
        if (speed > MAX_SPEED)
            speed = MAX_SPEED;
        else if (speed < -MAX_SPEED)
            speed = -MAX_SPEED;
        startAnimation(-speed);
    } else {
        if (mTouchRect != null) {
            // boolean test = RectF.intersects(mTouchRect, new
            // RectF(event.getX(),event.getY(),10,10));
            if (mCoverFlowListener != null && topImageClickEnable) {
                mCoverFlowListener.topImageClicked(this,
                        getActuallyPosition((int) mOffset));
            }
        }
    }
    mVelocity.clear();
    mVelocity.recycle();
}

但现在我想整合向上/向下滑动+ onlongclick事件。 你能告诉我如何做到这一点吗?

1 个答案:

答案 0 :(得分:0)

public class MainActivity extends Activity {

    String LOGTAG = "MainActivity";
    Boolean isSwipe = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final SwipeGestureDetector swipeGestureDetector = new SwipeGestureDetector();
        final GestureDetector gestureDetector = new GestureDetector(this,
                swipeGestureDetector);
        ImageView imageView = (ImageView) findViewById(R.id.imageView);
        View.OnTouchListener gestureListener = new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return gestureDetector.onTouchEvent(event);
            }
        };

        // Attach listener to listen for gestures on a view. eg. an ImageView 
        imageView.setOnTouchListener(gestureListener);


        //inside long click listener we first check if a swipe has been detected.
        //return false if we want to consume the swipe event
        //true if we want to consume the long click event. 
        //You can set a click listener too in a similar way. 
        imageView.setOnLongClickListener(new View.OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                if (isSwipe) {
                    Log.d(LOGTAG, "Swipe inside long click");
                    return false;
                } else {
                    Log.d(LOGTAG, "Long Click");
                    return true;
                }
            }
        });
    }

    class SwipeGestureDetector extends SimpleOnGestureListener {

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {

            switch (getSlope(e1.getX(), e1.getY(), e2.getX(), e2.getY())) {
            case 0:
                isSwipe = false;
                return false;
            case 1:
                Log.d(LOGTAG, "top");
                isSwipe = true;
                return true;
            case 2:
                Log.d(LOGTAG, "left");
                isSwipe = true;
                return true;
            case 3:
                Log.d(LOGTAG, "down");
                isSwipe = true;
                return true;
            case 4:
                Log.d(LOGTAG, "right");
                isSwipe = true;
                return true;
            }
            return false;
        }

        private int getSlope(float x1, float y1, float x2, float y2) {
            final ViewConfiguration vc = ViewConfiguration
                    .get(getApplicationContext());
            final int swipeMaxOffPath = vc.getScaledTouchSlop();
            Double angle = Math.toDegrees(Math.atan2(y1 - y2, x2 - x1));
            if (angle > 45 && angle <= 135) {
                // top
                if (Math.abs(y2 - y1) > swipeMaxOffPath)
                    return 1;
            }
            if (angle >= 135 && angle < 180 || angle < -135 && angle > -180) {
                // left
                if (Math.abs(x2 - x1) > swipeMaxOffPath)
                    return 2;
            }
            if (angle < -45 && angle >= -135) {
                if (Math.abs(y2 - y1) > swipeMaxOffPath)
                    return 3;
            }

            if (angle >= -45 && angle <= 45) { // right

                if (Math.abs(x2 - x1) > swipeMaxOffPath)
                    return 4;
            }
            return 0;
        }
    }
}