Windows Phone Mango开发实践——Shake Library应用实例

 Windows Phone Mango开发实践——Shake Library应用实例

探索火星之Shake Mars

 

目录

概述——开创新领域    1

微软的"Dallas"    2

动手实践初探Dallas——探索火星之Shake Mars    2

重力加速度传感    3

Shake Library实现晃动检测的原理    3

Shake Library调用用法    4

Shake Library的配置参数    5

 

概述——开创新领域

《周易》曰:"云雷,屯。君子以经轮。"意思是说乌云骤起,雷声交动,象征着事业初创的艰苦时期。君子应铁肩担道义,负起经略天下的大义。移动互联时代的竞争是生态系统的角斗和资源整合能力的考验,作为移动互联网竞争的制高点——智能终端操作系统三足鼎立的局面已然形成,此时正是山雨欲来风起云涌之时,正是英雄开创事业之初。

微软的"Dallas"

《Windows Phone Mango开发实践》开篇Windows Phone的入门的应用程序——探索火星,是非常有趣的应用程序。探索火星应用程序正是与Windows Azure平台的Dallas提供的数据服务通讯,呈现美国国家航空航天局(National Aeronautics and Space Administration,NASA)提供的火星探测行动拍摄到的图像。

微软的"Dallas"是由Windows Azure 和SQL Azure构建的信息服务,能够让开发者与信息工作者在任何平台上使用优质的第三方数据集和内容。在我的《Windows Phone Mango开发实践》出版之后,"Dallas"项目(现为Windows Azure Marketplace)于近期做了升级。

遗憾的是在新的Windows Azure Marketplace中找不到了NASA提供的火星探测行动的图片,然而万幸的是Windows Azure Marketplace原有的NASA Mars的服务还有,也就是说探索火星的应用还可以正常运行。如此,索火星的Account Key成了绝版的稀缺资源。如果您没有运行探索火星的Account Key,只要您是《Windows Phone Mango开发实践》的读者或者喜欢探索火星应用的开发者,我愿意与您分享我的Windows Live ID和Account Key,前提条件是仅作为技术研究。您可以在博客园留言或者新浪微博(http://weibo.com/highcedar)私信告知您的Email,我将Account Key发送至您的邮箱。

在新的探索火星应用中新增了重力感应检测手机晃动的功能,当您轻轻摇晃手机时,应用会自动切换图片,如果您连续的摇晃,出现的效果就是月球车在火星上采集样本的动画。

探索火星应用的英文名称为SHAKE MARS,支持重力感应检测手机晃动和手指触控切换图片,在Windows Phone Marketplace的深层链接为http://www.windowsphone.com/en-US/apps/12bee48c-dee1-4cfb-b04e-135eb4e69b68

探索火星和Shake Library源程序和文档:https://skydrive.live.com/redir.aspx?cid=6cf62487c100b43d&resid=6CF62487C100B43D!508 ,运行前请向我索取Windows Live ID和Account Key,因可能涉及Account Key的隐私性,所以只能采取这种不是很方便的方式。再次重申我愿意与您分享我的Windows Live ID和Account Key,前提条件是仅作为技术研究。您可以在博客园留言或者新浪微博(http://weibo.com/highcedar)私信告知您的Email,我将Account Key发送至您的邮箱。

动手实践初探Dallas——探索火星之Shake Mars

创建应用程序以及与Windows Azure Marketplace通讯的部分在《Windows Phone Mango开发实践》第二章已经详细讲述,本文将重点讲述重力感应检测晃动的实现方法。

重力加速度传感

处理重力加速度变化的ReadingChanged 事件的事件为 AccelerometerReadingEventArgs ,其 X、Y、Z属性的内容值代表智能手机在 X 轴、Y 轴、和 Z 轴的加速方向,而不是三维空间的坐标,其单位为重力单位,即G (1G = 9.81 m/s2)。除了 X、Y、与 Z 三个属性以外,还有一个名称为 Timestamp 的属性,负责记录重力加速度读取数据的时间点。

请注意当智能型手机放在平坦的桌面上,而且正面朝上的时候,AccelerometerReadingEventArgs 类别的 Z 字段的内容值会是 -1.0,表示 Z 轴承受 -1G 的重力,而当智能型手机放在平坦的桌面上,而且正面朝下的时候,AccelerometerReadingEventArgs 类别的 Z 字段的内容值就会是 +1.0,表示 Z 轴承受 1G 的重力。下面是一个显示相应数值的对应图:

1 重力加速度数值对应图

Shake Library实现晃动检测的原理

Shake Library使用的加速​​度计检测手机在三维空间的运动:

  • Left-Right (X direction)左右(X方向)
  • Top-Bottom (Y direction)顶底(Y方向)
  • Forward-Backward (Z direction)前后(Z方向)

当您移动电话,在三维空间中,应用程序得到了很多的读数(传感器是相当嘈杂),实际得到的数据在一个特定的时间内是在手机上的相对重力的变化。

考虑到这一点,高性能的Shake Library,按照以下动摇手势检测过程处理数据:

  • 降噪
  • 划分为成"摇晃shake"和"保持still"的信号段
  • Shake方向分类

应用程序的传感器读数,得到三位坐标轴(X,Y和Z值)。为了确保我们所检测到的运动确实是手机摇晃的状态,而不是只是一个随机在一个方向的运动,我们需要计数多个间隔的运动,更重要的是,在矢量方向的变化。

例如,如果我们发现我们的晃动信号的主要方向是X,那么我们将检查的信号中的所有X轴的矢量值。

下图中,我们看到了一个真正的摇晃的状态,可以清楚地看出Z轴为晃动的主方向,重力加速度值在Z轴的正负方向来回变换。

图2 Z轴方向的晃动

如果您也对shake library感兴趣,可在App Hub上获取shake library代码、示例和文档等更多的信息 Windows Phone Shake Gesture Library Recipe page on AppHub

Shake Library调用用法

第一步:添加引用动摇手势库ShakeGestures.dll

第二步:添加一个using语句文件头

using ShakeGestures;

 

event第三步:注册ShakeGesture事件、设定参数和启动手机晃动检测

// register shake event

ShakeGesturesHelper.Instance.ShakeGesture += new EventHandler<ShakeGestureEventArgs>(Instance_ShakeGesture);

 

// optional, set parameters

ShakeGesturesHelper.Instance.MinimumRequiredMovesForShake = 2;

 

// start shake detection

ShakeGesturesHelper.Instance.Active = true;

 

第四步:ShakeGesture事件处理程序、

 

在探索火星的应用中当检测到手机晃动时,与Windows Azure Marketplace通讯获取下一张探索火星的图片。

private void Instance_ShakeGesture(object sender, ShakeGestureEventArgs e)

{

//_lastUpdateTime.Dispatcher.BeginInvoke(

// () =>

// {

// _lastUpdateTime.Text = DateTime.Now.ToString();

// CurrentShakeType = e.ShakeType;

// });

Deployment.Current.Dispatcher.BeginInvoke(delegate { GetNextImage(); });

}

 

ShakeGestureEventArgs持有ShakeType动摇的姿态方向的属性,用于标识。

 

Shake Library的配置参数

These parameters control various aspects of the gesture detection algorithm.By changing these parameters, you can change your application's sensitivity to shakes and to the duration of a shake.通过改变这些参数,你可以改变你的应用程序的灵敏度和晃动持续的时间。The following section describes the available parameters.以下部分描述了可用的参数。

ShakeMagnitudeWithoutGravitationThreshold

Description: Any vector that has a magnitude (after reducing gravitation force) bigger than this parameter value is considered as a shake vector.

Default value: 0.2

StillCounterThreshold

Description: This parameter determines how many consecutive still vectors are required to stop a shake signal.

Default value: 20 (about 400 msec)

StillMagnitudeWithoutGravitationThreshold

Description: This parameter determines the maximum allowed magnitude (after reducing gravitation) for a still vector to be considered.

Default value: 0.02

MaximumStillVectorsNeededForAverage

Description: The maximum number of still vectors needed to create a still vector average. Instead of averaging the entire still signal, we just look at the top recent still vectors. This is performed as runtime optimization.

Default value: 20

MinimumStillVectorsNeededForAverage

Description: The minimum number of still vectors needed to create a still vector average. Without enough vectors, the average won't be stable and thus will be ignored.

Default value: 5

MinimumShakeVectorsNeededForShake

Description: Determines the number of shake vectors needed in order to recognize a shake.

Default value: 10

WeakMagnitudeWithoutGravitationThreshold

Description: Shake vectors with a magnitude lower than this parameter won't be considered for gesture classification.

Default value: 0.2

MinimumRequiredMovesForShake

Description: Determines the number of moves required to get a shake signal.

Default value: 3

 

Windows Phone Mango开发实践

Windows Phone Mango Development Practice

作者: 雪松

posted @ 2011-11-14 21:02  Xuesong  阅读(2365)  评论(7编辑  收藏  举报