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>

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Android Studio是一款用于开发Android应用程序的集成开发环境(IDE)。它提供了丰富的功能和工具,其中包括手势识别功能。 Android Studio中的手势识别是指通过触摸屏幕上的手势来实现特定操作或交互。Android系统提供了GestureDetector类来处理手势识别,它可以用于检测和处理单击、长按、滑动、双击等手势。 要在Android Studio中使用手势识别功能,你可以按照以下步骤进行操作: 1. 在布局文件中添加一个View组件,用于接收用户的手势输入。 2. 在Java代码中实例化GestureDetector对象,并重写其回调方法,以响应不同的手势事件。 3. 将GestureDetector对象与View组件进行关联,通过设置触摸监听器来监听用户的手势输入。 4. 在回调方法中编写相应的逻辑,根据不同的手势事件执行相应的操作。 以下是一个简单的示例代码,演示如何在Android Studio中实现手势识别: ```java import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View; public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener { private GestureDetector gestureDetector; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); View view = findViewById(R.id.my_view); // 替换为你的View组件的ID gestureDetector = new GestureDetector(this, this); view.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return gestureDetector.onTouchEvent(event); } }); } @Override public boolean onDown(MotionEvent e) { // 手指按下时触发 return true; } @Override public void onShowPress(MotionEvent e) { // 手指按下后未移动或松开时触发 } @Override public boolean onSingleTapUp(MotionEvent e) { // 单击时触发 return true; } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { // 滑动时触发 return true; } @Override public void onLongPress(MotionEvent e) { // 长按时触发 } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { // 快速滑动时触发 return true; } } ``` 这是一个简单的示例,你可以根据自己的需求进行扩展和修改。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

javaGHui

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值