Android : 使用GestureDetector 进行手势识别—简单应用

示例图:

GestureDetector 介绍:

GestureDetector 是 Android 开发中用于识别和处理手势的一个类。它允许开发者检测用户在触摸屏上的各种手势,如滑动、长按、双击等。通过使用 GestureDetector,您可以轻松地为应用程序添加手势识别功能,从而提供更直观和自然的用户界面。

API:手势检测器 |Android 开发者 (google.cn)

使用 GestureDetector 的基本步骤如下:

  1. 创建 GestureDetector 实例:首先,您需要创建一个 GestureDetector 的实例。这通常通过传递一个 GestureDetector.SimpleOnGestureListener 的实例来完成,该实例实现了 GestureDetector.OnGestureListener 接口。
  2. 设置 View 的 OnTouchListener:将 GestureDetector 实例设置为您想要检测手势的视图的 OnTouchListener。这样,当用户与视图交互时,GestureDetector 就会捕获这些事件。
  3. 实现手势识别逻辑:在 SimpleOnGestureListener 的实现中,您需要覆盖相应的方法来处理不同类型的手势。例如,您可以覆盖 onDown()onScroll()onLongPress() 等方法来处理相应的手势。
  4. 处理手势事件:在覆盖的方法中,您可以添加自定义逻辑来处理不同类型的手势。例如,当检测到滑动手势时,您可以执行某个动作;当检测到长按手势时,您可以显示一个菜单等。

通过这种方式,GestureDetector 使得处理用户在触摸屏上的手势变得非常简单和直观。这为创建直观和交互性强的应用程序提供了强大支持。

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>

猜你喜欢

转载自blog.csdn.net/jiayou2020527/article/details/135282854