100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > Android官方开发文档Training系列课程中文版:手势处理之监测通用手势

Android官方开发文档Training系列课程中文版:手势处理之监测通用手势

时间:2022-06-25 09:24:41

相关推荐

Android官方开发文档Training系列课程中文版:手势处理之监测通用手势

原文地址:/training/gestures/index.html

引言

这节课将会学习如何让用户通过触摸手势与APP产生交互。Android提供了许多相关API来帮助你创建、检测手势。

尽管APP不应该将触摸手势作为基本的输入特性,但是触摸手势可以使APP快速提高可操作性与吸引力。

为了提供一种一贯的,直观的用户体验,APP应当使用Android通用的触摸手势标准。

检测通用手势

当用户将一根或者多根手指放置在触摸屏上时就会产生触摸事件,应用程序需要将这次的触摸行为解释为一种特别的手势事件。下面是检测手势事件相应的执行步骤:

1.收集触摸事件的相关数据。2.解释这些数据,查看是否有程序所支持的任何标准手势。

收集数据

当用户将手指放在屏幕上时,这会回调相应View的onTouchEvent()方法。

手势开始于用户第一次触到屏幕时,接下来系统会追踪手指的位置,最后结束于手指离开屏幕时的最后一次事件。在这整个交互过程中,MotionEvent对象由onTouchEvent()方法分发,并提供了每个事件的相关详细信息。APP可以使用MotionEvent对象所提供的数据来检查是否有APP所关心的事件发生。

为Activity或View捕获触摸事件

为了拦截Activity或者View的触摸事件,需要重写它们的onTouchEvent()回调方法。

下面的代码使用getActionMasked()方法来提取event参数中含有的用户执行行为。它提供了你所关心的原始数据:

public class MainActivity extends Activity {...// This example shows an Activity, but you would use the same approach if// you were subclassing a View.@Overridepublic 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);}}

使单个View捕获触摸事件

除了onTouchEvent()方法之外,你还可以使用View.OnTouchListener来监听触摸手势。这使得不重写onTouchEvent()还可以监听触摸事件成为了可能:

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

要注意所创建的监听器在ACTION_DOWN事件时返回的false。如果你这么做了,那么监听器接下来对于ACTION_MOVE及ACTION_UP等一系列事件将不会调用。这是因为ACTION_DOWN事件是所有触摸事件的起点。

如果你创建了一个自定义View,你可以像上面描述的那样重写onTouchEvent()方法。

检测手势

Android提供了GestureDetector类来检测通用手势。这些手势包括onDown(), onLongPress(), onFling()等等。你可以将GestureDetector与onTouchEvent()结合使用。

检查所有支持的手势

当你在实例化GestureDetectorCompat对象时,其中一个参数需要实现GestureDetector.OnGestureListener接口。

GestureDetector.OnGestureListener接口的作用是:在指定的触摸事件发生时通知用户。为了使GestureDetector对象可以接收到触摸事件,你需要重写View或者Activity的onTouchEvent()方法,并将所有的事件传递给GestureDetector对象。

在下面的代码中,由onTouchEvent()方法所返回的true代表了你要负责处理这次的触摸事件。返回值false则代表你想忽略这次事件,直到这次的触摸事件被处理完毕。

运行下面的代码找找感觉:当你在触摸屏上操作时每种行为是如何被触发的;以及每一种触摸事件的MotionEvent对象的内容是什么。你将会意识到一个简单的触摸事件是由多么庞大的数据处理产生的。

public class MainActivity extends Activity implements GestureDetector.OnGestureListener,GestureDetector.OnDoubleTapListener{private static final String DEBUG_TAG = "Gestures";private GestureDetectorCompat mDetector; // Called when the activity is first created. @Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// Instantiate the gesture detector with the// application context and an implementation of// GestureDetector.OnGestureListenermDetector = new GestureDetectorCompat(this,this);// Set the gesture detector as the double tap// listener.mDetector.setOnDoubleTapListener(this);}@Override public boolean onTouchEvent(MotionEvent event){ this.mDetector.onTouchEvent(event);// Be sure to call the superclass implementationreturn super.onTouchEvent(event);}@Overridepublic boolean onDown(MotionEvent event) { Log.d(DEBUG_TAG,"onDown: " + event.toString()); return true;}@Overridepublic boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) {Log.d(DEBUG_TAG, "onFling: " + event1.toString()+event2.toString());return true;}@Overridepublic void onLongPress(MotionEvent event) {Log.d(DEBUG_TAG, "onLongPress: " + event.toString()); }@Overridepublic boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY) {Log.d(DEBUG_TAG, "onScroll: " + e1.toString()+e2.toString());return true;}@Overridepublic void onShowPress(MotionEvent event) {Log.d(DEBUG_TAG, "onShowPress: " + event.toString());}@Overridepublic boolean onSingleTapUp(MotionEvent event) {Log.d(DEBUG_TAG, "onSingleTapUp: " + event.toString());return true;}@Overridepublic boolean onDoubleTap(MotionEvent event) {Log.d(DEBUG_TAG, "onDoubleTap: " + event.toString());return true;}@Overridepublic boolean onDoubleTapEvent(MotionEvent event) {Log.d(DEBUG_TAG, "onDoubleTapEvent: " + event.toString());return true;}@Overridepublic boolean onSingleTapConfirmed(MotionEvent event) {Log.d(DEBUG_TAG, "onSingleTapConfirmed: " + event.toString());return true;}}

检测支持手势的子集

如果你只是想处理几种手势,那么你可以继承GestureDetector.SimpleOnGestureListener接口。

GestureDetector.SimpleOnGestureListener提供了对于onTouchEvent()方法所有的实现,这样你只用重写你所关心的方法。比如说,在下面的代码中创建了一个继承GestureDetector.SimpleOnGestureListener接口的类,然后重写了它的onFling()方法及onDown()方法。

无论你是否使用了GestureDetector.OnGestureListener接口,最佳的练习点在于重写了返回true的onDown()方法。这是因为所有的手势都是从onDown()开始的。如果在onDown()方法中返回了false,就像GestureDetector.SimpleOnGestureListener默认做的那样,那么系统会认为你想忽略余下的手势,并且GestureDetector.OnGestureListener接口的其它方法都不会被调用。这会在APP内埋下一个潜在的不易察觉的问题。如果你确认你要忽略整个手势流,那么onDown()中的结果false将是唯一的机会。

public class MainActivity extends Activity {private GestureDetectorCompat mDetector; @Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mDetector = new GestureDetectorCompat(this, new MyGestureListener());}@Override public boolean onTouchEvent(MotionEvent event){ this.mDetector.onTouchEvent(event);return super.onTouchEvent(event);}class MyGestureListener extends GestureDetector.SimpleOnGestureListener {private static final String DEBUG_TAG = "Gestures"; @Overridepublic boolean onDown(MotionEvent event) { Log.d(DEBUG_TAG,"onDown: " + event.toString()); return true;}@Overridepublic boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) {Log.d(DEBUG_TAG, "onFling: " + event1.toString()+event2.toString());return true;}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。