河南省建设厅网站建设领域涉黑,啥是网络推广,wordpress如何设置网站地图,手机百度识图网页版入口原文地址#xff1a;http://android.xsoftlab.net/training/gestures/index.html
引言
这节课将会学习如何让用户通过触摸手势与APP产生交互。Android提供了许多相关API来帮助你创建、检测手势。
尽管APP不应该将触摸手势作为基本的输入特性#xff0c;但是触摸手势可以使…原文地址http://android.xsoftlab.net/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.
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);}
}
使单个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;}}
}