Android 中 View 的手势事件处理

View 作为Android中最直接的和用户进行交互的单位,对于 View 的事件处理重要程度自然不言而喻,View 的事件处理直接影响到用户体验,下面我们来看一下对 View 的触摸事件的处理:

首先,View 的源代码中已经给我们写了一个 onTouchEvent 方法用于处理最直接的触摸事件,我们可以在官方文档中看到对这个方法的介绍:

public boolean  onTouchEvent (MotionEvent event) 


Added in API level 1



Implement this method to handle touch screen motion events. 

If this method is used to detect click actions, it is recommended that the actions be performed by implementing and calling performClick(). This will ensure consistent system behavior, including: 
•obeying click sound preferences 
•dispatching OnClickListener calls 
•handling ACTION_CLICK when accessibility features are enabled 



Parameters


event 
The motion event. 


Returns
True if the event was handled, false otherwise. 

大致意思是:实现这个方法去处理屏幕的触摸事件,如果这个方法用于处理单击事件,它将会:播放单击事件的声音,回调OnClickListener 接口的方法,如果可能的话处理单击动作。
简答来说就是我们可以在这个方法中处理当前 View 的触摸事件(单击事件也是一种触摸事件)。
方法的参数是一个 MotionEvent 类,用于储存当前触摸事件的信息,我们可以利用这些信息达到我们想要的效果。

接下来介绍一个配合这个方法使用的类:VelocityTracker(速度追踪类),这个类用于获取触摸移动的时候的速度,一般来说,我们会在 onTouchEvent 中使用这个类,先看看官方文档的说明:

这里写图片描述

文档里面提到的方法已经可以完成一些基本的需求,这里解释一下里面提到的方法的作用:

//// 通过静态方法实例化这个类的一个对象
VelocityTracker velocityTracker = VelocityTracker.obtain();
// 设置这个类要检测的触摸事件对象
velocityTracker.addMovement(event);
// 设置计算速度的时间间隔(毫秒)
velocityTracker.computeCurrentVelocity(1000);
/*
 * 获取在上一个设置的时间间隔(这里是1000ms)内这个检测的触摸事件在 X 方向和 Y 方向上移动的距离,
 * 那么就可以根据移动的距离和时间间隔算出速度
 */
 // 获取 x 方向上的移动速度
velocityTracker.getXVelocity();
 // 获取 y 方向上的移动速度
velocityTracker.getYVelocity();

下面我们通过一个小例子来具体的看一下怎么使用,假设我们要在屏幕上自由的移动手指,并且随时把手指的坐标和在 X 、Y 方向上的移动速度显示出来。新建一个 Android 工程:

activty_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    tools:context="com.example.administrator.viewontouchevent.MainActivity"
    android:orientation="vertical">

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/showXYTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="坐标:"
            android:background="#3300ff00"/>
        <TextView
            android:id="@+id/showVelocityTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="速度:"
            android:background="#220000ff"/>
    </TableRow>


</LinearLayout>

主布局文件中有两个 TextView 控件成一行排列,分别用于显示当前手指的坐标和 X 、 Y 方向上的移动速度。接下来,我们要自定义一个控件,然后重写 onTouchEvent 方法用于检测我们的手指移动的触摸信息并且传递给两个 TextView 控件。

但是在这里我们仔细思考一下:我们重写的 onTouchEvent 方法的参数和返回值都是固定的,不允许我们更改,那么我们该怎么将里面的数据传递出去呢?其实利用java中的回调机制就可以很好地解决这个问题:
我们新建一个自定义接口 GetInfFromMotionEvent:

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值