Detecting Common Gestures

当用户将一个或多个手指放在屏幕上时,“touch gesture“(触碰手势)就会发生,然后你的应用程序就会把你的 ”触碰“过程解释为一个特有的手势。

这个过程分为两个步奏,

1.收集 触碰事件的数据。

2.将收集到的数据翻译(解释),查看是否符合你的程序所支持的任何手势的标准。

收集数据(Gather Data)

当用户将手指放在屏幕上的时候,这个会在View组件上触发 onTouchEvent()回调方法,然后就会接收 touch event(触碰事件),每一个事件都会包含 位置,压力,尺寸,其他手指的触碰的信息,等等。通过这些信息,触碰事件被识别为一个手势。然后onTouchEvent()就会被多次触发。

当用户第一次触碰屏幕,手势 就开始了。然后系统会记录 用户手指的位置轨迹,当用户手指离开屏幕的时候结束。通过这种交互机制,MotionEvent类就会被传递给onTouchEvent()用来提供详细的交互信息。然后你的应用程序就可以根据MotionEvent提供的信息来决定这个手势是否是你要监测的手势。

Capturing touch events for an Activity or View

为了与touch event交互,Activity 或者View,要复写onTouchEvent()方法。

下面的代码 通过getActionMasked()方法从event中获取action。 这个方法会返回给你一系列数据,根据这些数据来决定 你需要监测的是哪些手势。

public class MainActivity extends Activity {
...
// This example shows an Activity, but you would use the same approach if
// you were subclassing a View.
@Override
public boolean onTouchEvent(MotionEvent event){ 

    int action = MotionEventCompat.getActionMasked(event);

    switch(action) {
        case (MotionEvent.ACTION_DOWN) :
            Log.d(DEBUG_TAG,"Action was DOWN");
            return true;
        case (MotionEvent.ACTION_MOVE) :
            Log.d(DEBUG_TAG,"Action was MOVE");
            return true;
        case (MotionEvent.ACTION_UP) :
            Log.d(DEBUG_TAG,"Action was UP");
            return true;
        case (MotionEvent.ACTION_CANCEL) :
            Log.d(DEBUG_TAG,"Action was CANCEL");
            return true;
        case (MotionEvent.ACTION_OUTSIDE) :
            Log.d(DEBUG_TAG,"Movement occurred outside bounds " +
                    "of current screen element");
            return true;
        default :
            return super.onTouchEvent(event);
    }
}

这种监测方法是通过 过程来监测用户手势的,如果你只需要监测一些常规的手势,例如: 双击,长按,抛掷,等。你可以通过GestureDetector类来监听。 这样你就可以不用监测 每一个独立的 touch事件。

Capturing touch events for a single view

你可以将 View.OnTouchListener 对象 附加 在任何View对象中通过 setOnTouchListener()方法。这样我们即使不继承View对象,也可以实现对 touch event的监听。

View myView = findViewById(R.id.my_view);
myView.setOnTouchListener(new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        // ... Respond to touch events
        return true;
    }
});

需要注意的是,如果你自定义一个Listener然后在ACTION_DOWN事件中返回false,那么其他 后续动作事件,例如ACTION_MOVE和ACTION_UP将不会被调用。因为ACTION_DOWN是所有touch event的开始。

如果你创建了一个自定义的View控件,你可以通过复写onTouchEvent(),就像上面描述的一样。

Detect Gestures

未完……

时间: 2024-08-04 21:02:32

Detecting Common Gestures的相关文章

Android官方开发文档Training系列课程中文版:手势处理之记录手指移动的轨迹

原文地址:http://android.xsoftlab.net/training/gestures/movement.html 这节课将会学习如何在触摸事件中记录手指移动的轨迹. 当手指触摸的位置.压力或者尺寸发生变化时,ACTION_MOVE事件就会被触发.与Detecting Common Gestures中描述的一样,所有的事件都被记录在一个MotionEvent对象中. 因为基于手指的触摸并不是很精确的交互方式,所以检测触摸事件的行为需要更多的轨迹点.为了帮助APP区分基于轨迹的手势(

Android官方开发文档Training系列课程中文版:手势处理之拖拽或缩放

原文地址:https://developer.android.com/training/gestures/scale.html 这节课主要学习如何使用触摸手势来拖动.放大屏幕上的对象. 拖动对象 如果你的重点在Android 3.0以上的版本,那么你可以使用内置的拖拽事件监听器View.OnDragListener. 触摸手势最常见的操作就是使用它来拖动屏幕上的对象.下面的代码允许用户拖动屏幕上的图像.要注意以下几点: 在拖动操作中,APP会一直保持手指拖动的轨迹,就算是另一只手指触到屏幕也是.

IOS开发—Using UIGestureRecognizer with Swift Tutoria

Update note: This tutorial was fully updated for iOS 8 and Swift by Caroline Begbie. Original post by Ray Wenderlich. Update 12/10/14: Updated for Xcode 6.1.1. If you need to detect gestures in your app, such as taps, pinches, pans, or rotations, it'

Tracking Movement

这篇文章用来讲解,如何跟踪touch event的轨迹. onTouchEvent()方法 由 ACTION_MOVE 事件触发(只要当前的接触的 position, pressure,size发生了变化). 就像上一篇文章 Detecting Common Gestures讲到的一样,这些event会被记录在MotionEvent中,而这个MotionEvent将作为onTouchEvent()的参数. 用户交互中基于手指触碰的反馈往往都不是最精确的,所以 监测 touch event的时候

微软Hololens学院教程- Holograms 101: Introduction with Device

这篇文章将通过一个完整的实例来了解设备的核心特性,包括凝视,手势,声音输入和空间声音与空间映射.先决条件 1.一个 Windows 10 PC 并安装有相应的软件tools installed..2. 开发者模式的HoloLensconfigured for development. . 项目文件 1.下载此项目所需文件files. 2.将其保存在桌面或者其他易于找到的位置, 保持文件名为 Origami. 章节 1."Holo"world 目标     1设置Unity.     2

ModSecurity web application firewall (WAF) Research

catalog 0. 引言 1. OWASP ModSecurity Core Rule Set (CRS) Project 2. Installation mod_security for Apache 3. Installation mod_security for nginx 4. Installation mod_security for IIS 5. mod_security Configuration Directives 6. Processing Phases 7. Variab

10种散发着爱情信号的肢体语言

10 body language love signals 10种散发着爱情信号的肢体语言 Do you ever wish there was a magic wand that would make you more attractive to the opposite sex, or at least help you understand who is attracted to you? Well, it's no magic but a science. Here's how men

Genymotion User Guide

Discover Genymotion User Guide FAQ Support/Contact API Get Genymotion Go to preview program Overview Features Requirements Installation Quick Start Licensing Genymotion Application Genymotion virtual devices Eclipse Plugin IntelliJ Plugin Genymotion

word文档批量合并工具

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases. ; #Warn ; Enable warnings to assist with detecting common errors. SendMode Input ; Recommended for new scripts due to its superior speed and reliability. SetWorki