Download - Motion and gesture in Android
Motion and GestureMotion and GestureMotion and GestureMotion and Gesturein Androidin Androidin Androidin Android
[email protected]@gmail.comwww.twitter.com/roger2yihttps://plus.google.com/
2011-10-20 Roger, UC
Motion EventMotion EventMotion EventMotion Event
• 系统通过MotionEvent传递触屏事件给应用
– View.onTrackballEvent(MotionEvent)
– View.onTouchEvent(MotionEvent)
• MotionEvent
– action code
– axis values (x, y)
2011-10-20 3
Action CodeAction CodeAction CodeAction Code
• Action Code指定了状态的变化
– ACTION_DOWN
– ACTION_UP
– ACTION_MOVE
– ACTION_CANCEL
– ACTION_POINTER_DOWN
– ACTION_PONTER_UP
2011-10-20 Roger, UC
Cont.Cont.Cont.Cont.
• 对于ACTION_POINTER_DOWN和ACTION_PONTER_UP,Action Code中还包含了该Pointer的Index数据
final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
2011-10-20 Roger, UC
Multi-Multi-Multi-Multi-touchtouchtouchtouch
• Android 2.2 (SDK-8) 加入了多点触摸的支持
– MotionEvent可以包含一个或多个Pointer的数据
– 每个Pointer在整个Motion(Down -> Move -> Up)的过程中会分配一个Id,作为这个Pointer的唯一标记
– 可以通过Pointer的Index获得Id或者相反
– getX,getY可以通过传入的Index参数获得多个Pointer的位置
2011-10-20 Roger, UC
Gesture DetectorGesture DetectorGesture DetectorGesture Detector
• Android 2.2 SDK提供了简单手势识别器用于识别若干常用的手势– Long Pressed– Fling (Flick)– Double Tap– Scale (Pinch)
• 应用可以提供自己的手势识别器支持更多的手势识别,比如Two Fingers Tap, Three Fingers Tap...
2011-10-20 Roger, UC
private class ScaleListener extendsScaleGestureDetector.SimpleOnScaleGestureListener {
@Override public boolean onScale(ScaleGestureDetector detector) { mScaleFactor *= detector.getScaleFactor(); // Don't let the object get too small or too large. mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));
invalidate(); return true; } }
... ...
public boolean onTouchEvent(MotionEvent ev) { // Let the ScaleGestureDetector inspect all events. mScaleDetector.onTouchEvent(ev);
2011-10-20 Roger, UC
应用应用应用应用自定义自定义自定义自定义手势手势手势手势的的的的支持支持支持支持
• Android提供了对自定义手势的比较完整的支持(1.6+),包括– 生成手势的轨迹数据(支持不连续的笔画输
入,但不支持多点),并保存到外部文件– 拦截用户的触屏输入,跟加载的手势数据(包
含多个手势)做匹配– 返回给应用可能匹配的手势
• 类似海豚浏览器的手势输入的功能,完全依赖于SDK提供的支持就可以实现
2011-10-20 Roger, UC
Gesture Gesture Gesture Gesture BuilderBuilderBuilderBuilder
• 模拟器自带一个Gesture Builder用于生成手势数据(源码在SDK Samples里面)
• 应用可以使用它来预先生成内置手势
• 应用也可以让用户生成新的手势
• 最后生成的手势数据可以通过GestureLibrary保存到外部文件中
2011-10-20 Roger, UC
Gesture Gesture Gesture Gesture LibraryLibraryLibraryLibrary
2011-10-20 Roger, UC
Cont.Cont.Cont.Cont.
• 创建手势的过程
– 显示一个GestureOverlayView,等待用户输入手势
– 当手势输入完毕后从GestureOverlayView获得Gesture对象
– 创建一个GestureLibrary(可以先加载旧的手势数据)
– 将新的Gesture对象加入该Library并保存
2011-10-20 Roger, UC
2011-10-20 Roger, UC
手势手势手势手势识别识别识别识别
• 手势识别的过程
– 显示一个GestureOverlayView,等待用户输入手势
– 在手势输入的过程中不断跟GestureLibrary中包含的手势数据进行匹配
– 如果结果达到一定的匹配度则认为手势识别成功
2011-10-20 Roger, UC
2011-10-20 Roger, UC
GestureListDemoGestureListDemoGestureListDemoGestureListDemo
2011-10-20 Roger, UC
ReferenceReferenceReferenceReference
• http://android-developers.blogspot.com/2009/10/gestures-on-android-16.html
• http://android-developers.blogspot.com/2010/06/making-sense-of-multitouch.html
• http://www.cnblogs.com/over140/archive/2010/12/08/1899839.html
The EndThe EndThe EndThe End
Thank you for your listeningYours Sincerely, Roger