欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Android手势滑动实现

程序员文章站 2022-03-28 16:15:00
详细的博客介绍: https://www.gcssloop.com/customview/gestruedector左滑,右滑,上划,下滑的实现总结:1.实现接口android.view.GestureDetector.OnGestureListener;2.onTouchEvent返回gestureDetector.onTouchEvent(event);3.重载实现onFling方法,判断x和y位移,以及速度;@Override public boo......
详细的博客介绍:
    https://www.gcssloop.com/customview/gestruedector

 

左滑,右滑,上划,下滑的实现总结:

1.实现接口android.view.GestureDetector.OnGestureListener;

2.onTouchEvent返回gestureDetector.onTouchEvent(event);

3.重载实现onFling方法,判断x和y位移,以及速度;

 

@Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        float minMoveDistance = 60;
        float minVelocity = 0;
        float beginX = e1.getX();
        float endX = e2.getX();
        float beginY = e1.getY();
        float endY = e2.getY();

        if(Math.abs(beginX-endX) > minMoveDistance || Math.abs(beginY-endY) > minMoveDistance)
        {
            mv.tilt = false;
            mv.zoom = false;
            mv.shift = true;
        }

        if(beginX-endX > minMoveDistance && Math.abs(velocityX) > minVelocity)
        {
            //左滑
            swipeLeft = true;
            mv.displacementX -= beginX-endX;
            mv.postInvalidate();

        }else if(endX-beginX >minMoveDistance && Math.abs(velocityX)>minVelocity)
        {
            //右滑
            swipeRight = true;
            mv.displacementX += endX-beginX;
            mv.postInvalidate();
        }else if(beginY-endY > minMoveDistance && Math.abs(velocityY) > minVelocity)
        {
            //上划
            swipeUp = true;
            mv.displacementY -= beginY-endY;
            mv.postInvalidate();
        }else if(endY-beginY >minMoveDistance && Math.abs(velocityY)>minVelocity)
        {
            //下划
            swipeDown = true;
            mv.displacementY += endY-beginY;
            mv.postInvalidate();
        }

        return false;
    }

 

 

本文地址:https://blog.csdn.net/John_chaos/article/details/107598699

相关标签: app