Android: Gesture recognition using GestureDetector—simple application

sample graph:

GestureDetector introduce:

GestureDetector It is a class used to recognize and process gestures in Android development. It allows developers to detect various user gestures on the touch screen, such as swipes, long presses, double clicks, etc. By using it  GestureDetector, you can easily add gesture recognition capabilities to your applications, providing a more intuitive and natural user interface.

API: Gesture Detector | Android Developer (google.cn)

The basic steps to use  GestureDetector are as follows:

  1. Create a GestureDetector instance : First, you need to create an  GestureDetector instance of GestureDetector. This is usually  GestureDetector.SimpleOnGestureListener done by passing an instance of an interface that implements  GestureDetector.OnGestureListener the interface.
  2. Set the View's OnTouchListenerGestureDetector Set the instance to the view you want to detect gestures for  OnTouchListener. GestureDetector This way, these events are captured when the user interacts with the view .
  3. Implement gesture recognition logic : In  SimpleOnGestureListener your implementation, you need to override the corresponding methods to handle different types of gestures. For example, you can override  onDown(), onScroll(), onLongPress() and other methods to handle the corresponding gesture.
  4. Handling gesture events : In the overridden method, you can add custom logic to handle different types of gestures. For example, you can perform an action when a swipe gesture is detected, display a menu when a long press gesture is detected, and so on.

In this way, GestureDetector handling user gestures on the touch screen becomes very simple and intuitive. This provides powerful support for creating intuitive and interactive applications.

MainActivity.java

package com.example.mygesturedetectordemo;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private TextView textView;
    //1.创建 GestureDetector 实例
    GestureDetector mGestureDetector;

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.textView);
        //1.1 初始化对象
        mGestureDetector = new GestureDetector(this, new MyGestureListener());
        //2.设置 View 的 OnTouchListener 设置触摸监听事件
        textView.setOnTouchListener((v, event) -> {
            //分析给定的运动事件,如果适用,则触发 对提供的进行适当的回调。OnGestureListener
            mGestureDetector.onTouchEvent(event);
            return true;
        });
    }

    //3. 实现手势识别逻辑:在 SimpleOnGestureListener 的实现中
    class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
        //4.处理简单的手势事件

        /**
         * 滑动
         *
         * @param e1        开始位置
         * @param e2        结束位置
         * @param velocityX 表示在X轴方向上的速度,单位是像素/毫秒
         * @param velocityY 表示在Y轴方向上的速度,单位是像素/毫秒
         * @return false
         */
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            //从右往左边滑动 开始x坐标 - 结束x坐标  = 滑动的距离   > 50
            if (e1.getX() - e2.getX() > 50) {
                textView.setText("从右往左边滑动");
//              Toast.makeText(MainActivity.this,"从右往左边滑动",Toast.LENGTH_SHORT).show();
                //从左往右边滑动 开始x坐标 + 结束x坐标  = 滑动的距离   > 50
            } else if (e1.getX() + e2.getX() > 50) {
                textView.setText("从左往右边滑动");
            }
            return super.onFling(e1, e2, velocityX, velocityY);
        }


    }

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="500dp"
        android:layout_height="500dp"
        android:background="#FF5722"
        android:gravity="center"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Guess you like

Origin blog.csdn.net/jiayou2020527/article/details/135282854