Detecting Common Gestures
来源:互联网 发布:学java去哪儿好 编辑:程序博客网 时间:2024/05/01 05:50
当用户将一个或多个手指放在屏幕上时,“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.@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); } }
这种监测方法是通过 过程来监测用户手势的,如果你只需要监测一些常规的手势,例如: 双击,长按,抛掷,等。你可以通过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
Android 提供了 GestureDetector类 用来监测 常用手势,例如 onDown(),onLongPress(),onFling()等等,你可以通过GestureDetector 结合 onTouchEvent()来实现上面的监测。
Detecting All Supported Gestures
监测所有常用的手势
当你实例化 一个GestureDetectorCompat(或者GestureDetector)对象的时候,其中一个参数就是 实现了 GestureDetector.OnGestureListener接口的类。 当用户的touch事件触发了之后,这个类就会通知用户。
(GestureDetectorCompat用来兼容低版本系统)
你可以通过 复写View或者Activity的 onTouchEvent();方法,然后将 要监测的event传递给GestureDetectorCompat(或者GestureDetector)实例的onTouchEvent(event)方法。
下面的代码片中,on< TouchEvent >(以on开头,用来处理TouchEvent的方法,例如onDown,onFling)方法如果返回值为true,则表明 你 已经处理了这个touch event,如果返回false,当前控件将会把这个touch event传递到View对象 栈 中,依次向下处理,直到这个touch event被成功的“消化”。
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. @Override public 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.OnGestureListener mDetector = 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 implementation return super.onTouchEvent(event); } @Override public boolean onDown(MotionEvent event) { Log.d(DEBUG_TAG,"onDown: " + event.toString()); return true; } @Override public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) { Log.d(DEBUG_TAG, "onFling: " + event1.toString()+event2.toString()); return true; } @Override public void onLongPress(MotionEvent event) { Log.d(DEBUG_TAG, "onLongPress: " + event.toString()); } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { Log.d(DEBUG_TAG, "onScroll: " + e1.toString()+e2.toString()); return true; } @Override public void onShowPress(MotionEvent event) { Log.d(DEBUG_TAG, "onShowPress: " + event.toString()); } @Override public boolean onSingleTapUp(MotionEvent event) { Log.d(DEBUG_TAG, "onSingleTapUp: " + event.toString()); return true; } @Override public boolean onDoubleTap(MotionEvent event) { Log.d(DEBUG_TAG, "onDoubleTap: " + event.toString()); return true; } @Override public boolean onDoubleTapEvent(MotionEvent event) { Log.d(DEBUG_TAG, "onDoubleTapEvent: " + event.toString()); return true; } @Override public boolean onSingleTapConfirmed(MotionEvent event) { Log.d(DEBUG_TAG, "onSingleTapConfirmed: " + event.toString()); return true; }}
Detecting a Subset of Supported Gestures
如果你只是想处理一部分 gesture,你可以使用 GestureDetector.SimpleOnGestureListener 来代替 GestureDetector.OnGestureListener 。
GestureDetector.SimpleOnGestureListener中的on< TouchEvent >返回值全部都是false,这样你就可以单独覆盖那些你想要覆盖的方法。下面的代码片段就是只复写了,onFling(),和onDown()方法。
还有需要注意的一点是,不论你用不用 GestureDetector.OnGestureListener,接口,你在实现onDown()方法的时候最好return true ;因为你的任何一个手势,都是从onDown()方法发送的信息开始的。 如果你return false;( GestureDetector.SimpleOnGestureListener中的onDown()方法默认 return false;),那么你的系统会认为你想忽略掉剩下的手势,然后 GestureDetector.OnGestureListener 中其他的方法将不会再回调。
public class MainActivity extends Activity { private GestureDetectorCompat mDetector; @Override public 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"; @Override public boolean onDown(MotionEvent event) { Log.d(DEBUG_TAG,"onDown: " + event.toString()); return true; } @Override public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) { Log.d(DEBUG_TAG, "onFling: " + event1.toString()+event2.toString()); return true; } }}
原文地址
- Detecting Common Gestures
- Detecting Common Gestures
- Using Touch Gestures 》Detecting Common Gestures
- 8.1 Detecting Swipe Gestures
- 8.2 Detecting Rotation Gestures
- 8.5 Detecting Tap Gestures
- 8.6 Detecting Pinch Gestures
- 检测一般的手势Detecting Common Gestures——翻译自developer.android.com Training
- 8.4 Detecting Long Press Gestures
- 8.3 Detecting Panning and Dragging Gestures
- Gestures//手势
- Android Gestures
- Gestures(手势)
- Using Touch Gestures 》Handling Multi-Touch Gestures
- Detecting Types
- Android Gestures的使用方法
- Windows Touch Gestures Overview
- Xcode Key Bindings & Gestures
- 4. 28 C语言 数据类型,运算符 , sizeof
- 编译预处理
- 三菱PLC编程软件GX Developer操作方法
- Maven知识的学习
- 杂乱笔记-myday2
- Detecting Common Gestures
- in.clear()、cin.sync()和cin.ignor()的用法
- Objective-C的内省(Introspection)小结
- 4. 29 C语言 程序结构-初级
- 数据库 删除表原理
- xhost unable to open display解决方法
- 4. 30 C语言 程序结构-高级
- C#动态获取类的属性名和值
- 5. 04 C语言 数组,字符串