美文网首页
android 判断手指滑动方向

android 判断手指滑动方向

作者: cc_And | 来源:发表于2021-07-20 14:14 被阅读0次
    private float downX, downY;
    
        private final float SLIDE_ANGLE = 45;
    
        private boolean mIsSlideHorizontally;//是否是水平滑动
    
        @Override
    
        public boolean dispatchTouchEvent(MotionEvent ev) {
    
            switch (ev.getAction()) {
    
            case MotionEvent.ACTION_DOWN:
    
                downX = ev.getX();
    
                downY = ev.getY();
    
                break;
    
            case MotionEvent.ACTION_MOVE:
    
            case MotionEvent.ACTION_UP:
    
                float moveX = ev.getX();
    
                float moveY = ev.getY();
    
                float xDiff = Math.abs(moveX - downX);
    
                float yDiff = Math.abs(moveY - downY);
    
                double squareRoot = Math.sqrt(xDiff * xDiff + yDiff * yDiff);
    
                //滑动的角度
    
                int yAngle = Math.round((float) (Math.asin(yDiff / squareRoot) / Math.PI * 180));
    
                int xAngle = Math.round((float) (Math.asin(xDiff / squareRoot) / Math.PI * 180));
    
                boolean isMeetSlidingYAngle = yAngle > SLIDE_ANGLE;//滑动角度是否大于45du
    
                boolean isMeetSlidingXAngle = xAngle > SLIDE_ANGLE;//滑动角度是否大于45du
    
                boolean isSlideUp = moveY < downY && isMeetSlidingYAngle;
    
                boolean isSlideDown = moveY > downY && isMeetSlidingYAngle;
    
                boolean isSlideLeft = moveX < downX && isMeetSlidingXAngle;
    
                boolean isSlideRight = moveX > downX && isMeetSlidingXAngle;
    
                if (isSlideUp) {
    
                    Log.d("chenhaocc", "向上滑动");
    
                    mIsSlideHorizontally = false;
    
                } else if (isSlideDown) {
    
                    Log.d("chenhaocc", "向下滑动");
    
                    mIsSlideHorizontally = false;
    
                } else if (isSlideLeft) {
    
                    Log.d("chenhaocc", "向左边滑动");
    
                    mIsSlideHorizontally = true;
    
                } else if (isSlideRight) {
    
                    mIsSlideHorizontally = true;
    
                    Log.d("chenhaocc", "向右边滑动");
    
                }
    
            }
    
            return super.dispatchTouchEvent(ev);
    
        }
    

    相关文章

      网友评论

          本文标题:android 判断手指滑动方向

          本文链接:https://www.haomeiwen.com/subject/xaalmltx.html