Android 如何检测左/右和上/下之间的滑动方向

Android 如何检测左/右和上/下之间的滑动方向,android,user-interface,user-interaction,Android,User Interface,User Interaction,我的问题:我如何检测用户何时上下移动手指而不是左/右移动手指(以及我如何知道他们手指移动的方向) 我的情况:我想在他们上下移动手指时更改我的应用程序的亮度(上=亮,下=暗),我想根据他们的左/右滑动在活动和/或视图之间切换。你只需扩展SimpleOnGestureListener类 在你的课堂上宣布这一点 private static final int SWIPE_MIN_DISTANCE = 120; private static final int SWIPE_MAX_OFF_PATH =

我的问题:我如何检测用户何时上下移动手指而不是左/右移动手指(以及我如何知道他们手指移动的方向)


我的情况:我想在他们上下移动手指时更改我的应用程序的亮度(上=亮,下=暗),我想根据他们的左/右滑动在活动和/或视图之间切换。

你只需扩展SimpleOnGestureListener类

在你的课堂上宣布这一点

private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
作为水平滑动的示例,您可以看到以下代码

 class MyGestureDetector extends SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        try {
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH){
                return false;
            }
            // right to left swipe
            if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                onLeftSwipe();
            } 
            // left to right swipe
            else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                onRightSwipe();
            }
        } catch (Exception e) {

        }
        return false;
      }
   }

对于垂直滑动,您也可以这样做。

我为此编写了一个简单的类:它有很好的文档记录,所以我不会在这里解释它

public class OnSwipeListener extends GestureDetector.SimpleOnGestureListener {

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

        // Grab two events located on the plane at e1=(x1, y1) and e2=(x2, y2)
        // Let e1 be the initial event
        // e2 can be located at 4 different positions, consider the following diagram
        // (Assume that lines are separated by 90 degrees.)
        //
        //
        //         \ A  /
        //          \  /
        //       D   e1   B
        //          /  \
        //         / C  \
        //
        // So if (x2,y2) falls in region:
        //  A => it's an UP swipe
        //  B => it's a RIGHT swipe
        //  C => it's a DOWN swipe
        //  D => it's a LEFT swipe
        //

        float x1 = e1.getX();
        float y1 = e1.getY();

        float x2 = e2.getX();
        float y2 = e2.getY();

        Direction direction = getDirection(x1,y1,x2,y2);
        return onSwipe(direction);
    }

    /** Override this method. The Direction enum will tell you how the user swiped. */
    public boolean onSwipe(Direction direction){
        return false;
    }

    /**
     * Given two points in the plane p1=(x1, x2) and p2=(y1, y1), this method
     * returns the direction that an arrow pointing from p1 to p2 would have.
     * @param x1 the x position of the first point
     * @param y1 the y position of the first point
     * @param x2 the x position of the second point
     * @param y2 the y position of the second point
     * @return the direction
     */
    public Direction getDirection(float x1, float y1, float x2, float y2){
        double angle = getAngle(x1, y1, x2, y2);
        return Direction.fromAngle(angle);
    }

    /**
     *
     * Finds the angle between two points in the plane (x1,y1) and (x2, y2)
     * The angle is measured with 0/360 being the X-axis to the right, angles
     * increase counter clockwise.
     *
     * @param x1 the x position of the first point
     * @param y1 the y position of the first point
     * @param x2 the x position of the second point
     * @param y2 the y position of the second point
     * @return the angle between two points
     */
    public double getAngle(float x1, float y1, float x2, float y2) {

        double rad = Math.atan2(y1-y2,x2-x1) + Math.PI;
        return (rad*180/Math.PI + 180)%360;
    }


    public enum Direction{
        up,
        down,
        left,
        right;

        /**
         * Returns a direction given an angle.
         * Directions are defined as follows:
         *
         * Up: [45, 135]
         * Right: [0,45] and [315, 360]
         * Down: [225, 315]
         * Left: [135, 225]
         *
         * @param angle an angle from 0 to 360 - e
         * @return the direction of an angle
         */
        public static Direction fromAngle(double angle){
            if(inRange(angle, 45, 135)){
                return Direction.up;
            }
            else if(inRange(angle, 0,45) || inRange(angle, 315, 360)){
                return Direction.right;
            }
            else if(inRange(angle, 225, 315)){
                return Direction.down;
            }
            else{
               return Direction.left;
           }

        }

       /**
         * @param angle an angle
         * @param init the initial bound
         * @param end the final bound
         * @return returns true if the given angle is in the interval [init, end).
         */
        private static boolean inRange(double angle, float init, float end){
            return (angle >= init) && (angle < end);
        }
    }
 }
公共类OnSwipeListener扩展了GestureDetector.SimpleOnGestureListener{
@凌驾
公共布尔onFling(MotionEvent e1、MotionEvent e2、float-velocityX、float-velocityY){
//抓取位于e1=(x1,y1)和e2=(x2,y2)平面上的两个事件
//设e1为初始事件
//E2可以位于4个不同的位置,考虑下面的图表
//(假设直线之间相隔90度。)
//
//
//\A/
//          \  /
//D e1 B
//          /  \
///C\
//
//因此,如果(x2,y2)落在区域中:
//A=>这是一个向上滑动
//B=>这是右击
//C=>这是向下击
//D=>是左击
//
浮点x1=e1.getX();
float y1=e1.getY();
float x2=e2.getX();
float y2=e2.getY();
方向=方向(x1,y1,x2,y2);
返回(方向);
}
/**重写此方法。方向枚举将告诉您用户如何刷卡*/
公共布尔值onSwipe(方向){
返回false;
}
/**
*给定平面p1=(x1,x2)和p2=(y1,y1)中的两个点,此方法
*返回从p1指向p2的箭头的方向。
*@param x1第一点的x位置
*@param y1第一个点的y位置
*@param x2第二个点的x位置
*@param y2第二个点的y位置
*@返回方向
*/
公共方向getDirection(浮点x1、浮点y1、浮点x2、浮点y2){
双角度=角度(x1,y1,x2,y2);
返回方向。从角度(角度);
}
/**
*
*查找平面(x1,y1)和(x2,y2)中两点之间的角度
*测量角度时,0/360为向右的X轴角度
*逆时针增加。
*
*@param x1第一点的x位置
*@param y1第一个点的y位置
*@param x2第二个点的x位置
*@param y2第二个点的y位置
*@返回两点之间的角度
*/
公共双getAngle(浮点x1、浮点y1、浮点x2、浮点y2){
double rad=Math.atan2(y1-y2,x2-x1)+Math.PI;
返回(rad*180/Math.PI+180)%360;
}
公共枚举方向{
向上的
下来,,
左边
正确的;
/**
*返回给定角度的方向。
*方向定义如下:
*
*上升:[45135]
*右图:[0,45]和[315,360]
*下降:[225315]
*左:[135225]
*
*@param角度从0到360-e的角度
*@返回一个角度的方向
*/
公共静态方向从角度(双角度){
如果(在范围内(角度,45,135)){
返回方向。向上;
}
else if(范围(角度,0,45)| |范围(角度,315,360)){
返回方向。右;
}
否则,如果(在范围内(角度,225,315)){
返回方向:向下;
}
否则{
返回方向:左;
}
}
/**
*@param角一个角
*@param init初始绑定
*@param结束最终绑定
*@return如果给定角度在间隔[init,end]内,则返回true。
*/
范围内的私有静态布尔值(双角度、浮点初始、浮点结束){
返回(角度>=初始)和(角度<结束);
}
}
}

要使用它,只需扩展
onswiplistener
并覆盖
onSwipe
方法

我在bitbucket上有一个开源的手势库。在这个库中有一个“HGFling”类。它演示了如何检测投掷的方向。你可以从以下位置下载该库:。它是开源的。

我解决了这个问题方式:

viewPager.setOnTouchListener(new View.OnTouchListener() {
        float prevX = -1;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (prevX != -1) {
                if (event.getX() > prevX) {
                    if (viewPager.getCurrentItem() == 0) {
                         // Left to Right swipe
                    }
                    //Log.d("DEBUG", MotionEvent.ACTION_MOVE + ":" + event.getAction() + ":" + event.getActionMasked() + ":Left Swipe" + ":" + prevX + ":" + event.getX() + ":" + viewPager.getCurrentItem());
                } else if (prevX > event.getX()) {
                       // Right to left swipe
                    //Log.d("DEBUG", MotionEvent.ACTION_MOVE + ":" + event.getAction() + ":" + event.getActionMasked() + ":Right Swipe" + ":" + prevX + ":" + event.getX() + ":" + viewPager.getCurrentItem());
                }
            }
            if (event.getAction() == MotionEvent.ACTION_MOVE) {
                prevX = event.getX();
            } else {
                prevX = -1;
            }
            return false;
        }
    });
答案很完美,我写的这个答案是关于如何在
活动
片段
中使用它,因为很多人都在寻找它

public class MyActivity extends Activity implements View.OnTouchListener{

     private RelativeLayout someLayout;
     //take any layout on which you want your gesture listener;

     @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    gestureDetector=new GestureDetector(this,new OnSwipeListener(){

        @Override
        public boolean onSwipe(Direction direction) {
            if (direction==Direction.up){
               //do your stuff
                Log.d(TAG, "onSwipe: up");

            }

            if (direction==Direction.down){
               //do your stuff
                Log.d(TAG, "onSwipe: down");
            }
            return true;
        }


    });
    someLayout.setOnTouchListener(this);
}

    @Override
    public boolean onTouch(View v, MotionEvent event) {
      Log.d(TAG, "onTouch: ");
      gestureDetector.onTouchEvent(event);
      return true;
  }


}

完整的使用示例用于上述内容:

如果要将OnSwipeListener应用于其中一个视图,请执行以下操作:
无论此视图位于何处-为该视图设置触摸侦听器,如下所示:

myview.setOnTouchListener(this);
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    return detector.onTouchEvent(motionEvent);
}
现在,在活动的OnCreate或自定义视图构造函数中执行以下操作:

// Global
private GestureDetectorCompat detector; 

// In OnCreate or custome view constructor (which extends one of Android views)
detector = new GestureDetectorCompat(context, onSwipeListener);
在同一类中重写onTouch事件,如下所示:

myview.setOnTouchListener(this);
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    return detector.onTouchEvent(motionEvent);
}
并在同一类中具有此侦听器对象:

OnSwipeListener onSwipeListener = new OnSwipeListener() {
    @Override
    public boolean onSwipe(Direction direction) {
        // Possible implementation 
        if(direction == Direction.left|| direction == Direction.right) {
            // Do something COOL like animation or whatever you want
            // Refer to your view if needed using a global reference
            return true;
        }
        else if(direction == Direction.up|| direction == Direction.down) {
            // Do something COOL like animation or whatever you want
            // Refer to your view if needed using a global reference
            return true;
        }
        return super.onSwipe(direction);
    }
};

对于这样一个简单的问题来说,现有的答案太复杂了。 我建议使用另一种方法(代码是as3,但您可以理解):

var touchDistance:Number=Point.distance(\u moveTouchPoint,\u startTouchPoint);
如果(触摸距离>=滑动距离)
{
var xDiff:Number=\u moveTouchPoint.x-\u startTouchPoint.x;
var yDiff:Number=\u moveTouchPoint.y-\u startTouchPoint.y;
var yGreater:Boolean=Math.abs(yDiff)>=Math.abs(xDiff);
if(yGreater)
{
//方向是向上还是向下
将播放方向更改为(yDiff<0?方向向上:DIRECTIO
class ClientFragment : Fragment(), View.OnTouchListener {

    private lateinit var gestureDetector: GestureDetector

    override fun onTouch(v: View?, event: MotionEvent?): Boolean {
        Log.d(TAG, "onTouch: ");
        gestureDetector.onTouchEvent(event);
        return true
    }


override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

 ...



 gestureDetector = GestureDetector(activity, object : OnSwipeListener() {

            override fun onSwipe(direction: Direction): Boolean {

                when(direction){
                   Direction.up ->
                   {
                       Log.d(TAG, "onSwipe: up")
                       sendCommand("UP")
                       return true
                   }
                     Direction.down ->{
                         Log.d(TAG, "onSwipe: down")
                         sendCommand("DOWN")
                         return true
                     }

                    Direction.left ->
                    {
                        Log.d(TAG, "onSwipe: left")
                        sendCommand("LEFT")
                        return true

                    }
                    Direction.right ->{
                        Log.d(TAG, "onSwipe: right")
                        sendCommand("RIGHT")
                        return true

                    }
                    else -> {
                    }
                }
                return true
            }


        })
        dpadLayout.setOnTouchListener(this)
float initialX, initialY;

@Override
public boolean onTouchEvent(MotionEvent event) {

    int action = event.getActionMasked();

    switch (action) {

        case MotionEvent.ACTION_DOWN:
            initialX = event.getX();
            initialY = event.getY();


            // Log.d(TAG, "Action was DOWN");
            break;

        case MotionEvent.ACTION_MOVE:

            //Log.d(TAG, "Action was MOVE");
            break;

        case MotionEvent.ACTION_UP:
            float finalX = event.getX();
            float finalY = event.getY();


            //Log.d(TAG, "Action was UP");

            if (initialX < finalX) {
                // Log.d(TAG, "Left to Right swipe performed");
            }

            if (initialX > finalX) {
                // Log.d(TAG, "Right to Left swipe performed");
            }

            if (initialY < finalY) {
                // Log.d(TAG, "Up to Down swipe performed");
            }

            if (initialY > finalY) {
                // Log.d(TAG, "Down to Up swipe performed");
            }

            break;

        case MotionEvent.ACTION_CANCEL:
            //Log.d(TAG,"Action was CANCEL");
            break;

        case MotionEvent.ACTION_OUTSIDE:
            // Log.d(TAG, "Movement occurred outside bounds of current screen element");
            break;
    }

    return super.onTouchEvent(event);
}
  public class OnSwipeListener implements View.OnTouchListener{
    public enum Direction{up,down,left,right;}
    private GestureDetector gestureDetector;
    private Context context;
    public OnSwipeListener(Context c) {
        this.context = c;

        gestureDetector = new GestureDetector(c, new GestureListener(c));
    }
    public boolean onTouch(final View view, final MotionEvent motionEvent) {

        return gestureDetector.onTouchEvent(motionEvent);
    }

    public void onSwipeRight() {
    }

    public void onSwipeLeft() {
    }

    public void onSwipeUp() {
    }

    public void onSwipeDown() {
    }

    public void onClick() {
    }

    public void onDoubleClick() {
    }

    public void onLongClick() {
    }
    public double getAngle(float x1, float y1, float x2, float y2) {
        double rad = Math.atan2(y1-y2,x2-x1) + Math.PI;
        return (rad*180/Math.PI + 180)%360;
    }

    private final class GestureListener extends GestureDetector.SimpleOnGestureListener {
        Context context;
        public GestureListener(Context c) {
            this.context = c;
        }

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            onClick();
            return super.onSingleTapUp(e);
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            onDoubleClick();
            return super.onDoubleTap(e);
        }

        @Override
        public void onLongPress(MotionEvent e) {
            onLongClick();
            super.onLongPress(e);
        }
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            float x1 = e1.getX();
            float y1 = e1.getY();

            float x2 = e2.getX();
            float y2 = e2.getY();

            Direction direction = getDirection(x1,y1,x2,y2);


            return onSwipe(direction);
        }

        public Direction getDirection(float x1, float y1, float x2, float y2){
            double angle = getAngle(x1, y1, x2, y2);
            return fromAngle(angle);
        }

        public  Direction fromAngle(double angle){
            if(inRange(angle, 45, 135)){
                onSwipeUp();
                return Direction.up;
            }
            else if(inRange(angle, 0,45) || inRange(angle, 315, 360)){
                onSwipeRight();
                return Direction.right;
            }
//            else if(inRange(angle, 225, 315)){
//                //onSwipeDown();
//
//            }
            else if(inRange(angle,135, 225)){
                onSwipeLeft();
                return Direction.left;
            }
            else {
                return Direction.down;
            }


        }
         private  boolean inRange(double angle, float init, float end){
            return (angle >= init) && (angle < end);
        }

        public boolean onSwipe(Direction direction){
            return false;
        }
    };
}
Best Way and Simple Way to detect Swipe left,Right,Top,Bottom

1) First Make One Java Class and implments: -  View.OnTouchListener
Add below code in  this Class: - 

public  class OnSwipeTouchListener implements View.OnTouchListener {
    private final GestureDetector gestureDetector;
    Context context;
    OnSwipeTouchListener(Context ctx, View mainView) {
        gestureDetector = new GestureDetector(ctx, new GestureListener());
        mainView.setOnTouchListener(this);
        context = ctx;
    }
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }
    public class GestureListener extends
            GestureDetector.SimpleOnGestureListener {
        private static final int SWIPE_THRESHOLD = 100;
        private static final int SWIPE_VELOCITY_THRESHOLD = 100;
        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            boolean result = false;
            try {
                float diffY = e2.getY() - e1.getY();
                float diffX = e2.getX() - e1.getX();
                if (Math.abs(diffX) > Math.abs(diffY)) {
                    if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffX > 0) {
                            onSwipeRight();
                        } else {
                            onSwipeLeft();
                        }
                        result = true;
                    }
                }
                else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        onSwipeBottom();
                    } else {
                        onSwipeTop();
                    }
                    result = true;
                }
            }
            catch (Exception exception) {
                exception.printStackTrace();
            }
            return result;
        }
    }
    void onSwipeRight() {
        Toast.makeText(context, "You Swipe Right", Toast.LENGTH_SHORT).show();
        this.onSwipe.swipeRight();
    }
    void onSwipeLeft() {
        Toast.makeText(context, "You Swipe Left", Toast.LENGTH_SHORT).show();
        this.onSwipe.swipeLeft();
    }
    void onSwipeTop() {
        Toast.makeText(context, "You Swipe Up", Toast.LENGTH_SHORT).show();
        this.onSwipe.swipeTop();
    }
    void onSwipeBottom() {
        Toast.makeText(context, "You Swipe Down", Toast.LENGTH_SHORT).show();
        this.onSwipe.swipeBottom();
    }
    interface onSwipeListener {
        void swipeRight();
        void swipeTop();
        void swipeBottom();
        void swipeLeft();
    }
    onSwipeListener onSwipe;
}

And In Your MainActivity Class Use This Code: - 

public class MainActivity extends AppCompatActivity {
    OnSwipeTouchListener onSwipeTouchListener;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        onSwipeTouchListener = new OnSwipeTouchListener(this, findViewById(R.id.relativeLayout));
    }
}

In activity_main.xml: - 
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity">
   
</RelativeLayout>
float firstX_point, firstY_point;
@Override
public boolean onTouchEvent(MotionEvent event) {

    int action = event.getAction();

    switch (action) {

        case MotionEvent.ACTION_DOWN:
            firstX_point = event.getRawX();
            firstY_point = event.getRawY();
            break;

        case MotionEvent.ACTION_UP:

            float finalX = event.getRawX();
            float finalY = event.getRawY();

            int distanceX = (int) (finalX - firstX_point);
            int distanceY = (int) (finalY - firstY_point);

            if (Math.abs(distanceX) > Math.abs(distanceY)) {
                if ((firstX_point < finalX)) {
                    Log.d("Test", "Left to Right swipe performed");
                } else {
                    Log.d("Test", "Right to Left swipe performed");
                }
            }else{
                if ((firstY_point < finalY)) {
                    Log.d("Test", "Up to Down swipe performed");
                } else {
                    Log.d("Test", "Down to Up swipe performed");
                }
            }


            break;
    }

    return true;
}