17
Motion and Gesture Motion and Gesture Motion and Gesture Motion and Gesture in Android in Android in Android in Android Roger Roger Roger Roger [email protected] [email protected] www.twitter.com/roger2yi https://plus.google.com/

Motion and gesture in Android

  • Upload
    rogeryi

  • View
    1.739

  • Download
    7

Embed Size (px)

DESCRIPTION

Introduce motion and gesture programming in Android

Citation preview

Page 1: 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/

Page 2: Motion and gesture in Android

2011-10-20 Roger, UC

Motion EventMotion EventMotion EventMotion Event

• 系统通过MotionEvent传递触屏事件给应用

– View.onTrackballEvent(MotionEvent)

– View.onTouchEvent(MotionEvent)

• MotionEvent

– action code

– axis values (x, y)

Page 3: Motion and gesture in Android

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

Page 4: Motion and gesture in Android

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;

Page 5: Motion and gesture in Android

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的位置

Page 6: Motion and gesture in Android

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...

Page 7: Motion and gesture in Android

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);

Page 8: Motion and gesture in Android

2011-10-20 Roger, UC

应用应用应用应用自定义自定义自定义自定义手势手势手势手势的的的的支持支持支持支持

• Android提供了对自定义手势的比较完整的支持(1.6+),包括– 生成手势的轨迹数据(支持不连续的笔画输

入,但不支持多点),并保存到外部文件– 拦截用户的触屏输入,跟加载的手势数据(包

含多个手势)做匹配– 返回给应用可能匹配的手势

• 类似海豚浏览器的手势输入的功能,完全依赖于SDK提供的支持就可以实现

Page 9: Motion and gesture in Android

2011-10-20 Roger, UC

Gesture Gesture Gesture Gesture BuilderBuilderBuilderBuilder

• 模拟器自带一个Gesture Builder用于生成手势数据(源码在SDK Samples里面)

• 应用可以使用它来预先生成内置手势

• 应用也可以让用户生成新的手势

• 最后生成的手势数据可以通过GestureLibrary保存到外部文件中

Page 10: Motion and gesture in Android

2011-10-20 Roger, UC

Gesture Gesture Gesture Gesture LibraryLibraryLibraryLibrary

Page 11: Motion and gesture in Android

2011-10-20 Roger, UC

Cont.Cont.Cont.Cont.

• 创建手势的过程

– 显示一个GestureOverlayView,等待用户输入手势

– 当手势输入完毕后从GestureOverlayView获得Gesture对象

– 创建一个GestureLibrary(可以先加载旧的手势数据)

– 将新的Gesture对象加入该Library并保存

Page 12: Motion and gesture in Android

2011-10-20 Roger, UC

Page 13: Motion and gesture in Android

2011-10-20 Roger, UC

手势手势手势手势识别识别识别识别

• 手势识别的过程

– 显示一个GestureOverlayView,等待用户输入手势

– 在手势输入的过程中不断跟GestureLibrary中包含的手势数据进行匹配

– 如果结果达到一定的匹配度则认为手势识别成功

Page 14: Motion and gesture in Android

2011-10-20 Roger, UC

Page 15: Motion and gesture in Android

2011-10-20 Roger, UC

GestureListDemoGestureListDemoGestureListDemoGestureListDemo

Page 16: Motion and gesture in Android

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

Page 17: Motion and gesture in Android

The EndThe EndThe EndThe End

Thank you for your listeningYours Sincerely, Roger