Android 简单的 上下左右 手势滑动监听

直接上代码:

    private float mDx, mDy;//按下坐标值
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mDx = event.getX();
                mDy = event.getY();
                Log.i(TAG, "down=mDx=" + mDx + ",mDy=" + mDy);
                break;
            case MotionEvent.ACTION_MOVE:
                float mx = event.getX();
                float my = event.getY();

                float disW = mx - mDx;//x轴滑动距离
                float disH = my - mDy;//y轴滑动距离

                if (Math.abs(disW) > Math.abs(disH)) {//滑动轴判断(这个条件可根据实际需求判断)
                    if (disW > 0) {//往右滑
                        //你的逻辑...

                        Log.i(TAG, "move=往右滑=mDx=" + mDx + ",mDy=" + mDy + ",mx=" + mx + &#

你可能感兴趣的:(android,android,上下左右,手势监听)