设为首页 收藏本站
查看: 946|回复: 0

Windows phone 7 31天学习笔记 11:加速器

[复制链接]

尚未签到

发表于 2015-5-12 11:07:26 | 显示全部楼层 |阅读模式
  原文地址:http://www.jeffblankenburg.com/post/31-Days-of-Windows-Phone-7c-Day-11-Accelerometer.aspx
  上篇讨论了手机上的键盘。这次将会讨论硬件中的加速器,以及如何利用它提供的信息。
  什么是加速器
  由于缺少更好的定一个加速器是手机中能够获取3维(x,y,z)加速度以及感知坠落的设备。除了时间戳,这些数据都是按照G来计算的(1G=9.81m/s^2)。这意味着如果手机迎面在水平面上无阻力飞行,Z轴获取数据为-1,其他轴为0。示意图如下:
DSC0000.png
  如何从加速器中获取数据?
  感谢微软,这些很简单。唯一复杂的地方就是需要一个进程管理,但是这也比较简单。先面试我们需要做的。
  
       
  • 实例化一个Accelerometer   
  • 创建一个ReadingChanged()函数来监视数据变化。   
  • 将数据传给我们的页面进程。   
  • 在程序中使用数据。
  下面是例子中的代码,你看到我已经在页面中创建了文本块,所以我可以写他们的值。同样注意需要引用Microsoft.Devices.Sesors来获取加速器。
  
   Home Archive Contact  Subscribe Twitter LinkedIn
>
31 Days of Windows Phone | Day #11: Accelerometer
By Jeff Blankenburg11. October 2010 02:00
This post is Day #11 in a series called the 31 Days of Windows Phone.
Yesterday, we talked about the software-based keyboard on Windows Phone.  Today, we’re going to talk about the hardware-based accelerometer, and how we can take advantage of the information it provides.
What is an accelerometer?
For lack of a better definition, an accelerometer is a sensor in the Windows Phone devices that measures the acceleration on 3 axes (X, Y, Z), relative to freefall.  In addition to a timestamp, the values are expressed in G-forces (1G = 9.81 m/s2).  What this means is that if the phone is lying face-up on a perfectly flat surface, the Z axis would read –1.0, and the other two axes would read 0.  Here’s a quick illustration showing the different values (thanks to WindowsTeamBlog for the image):


How do I get values from the WP7 Accelerometer?
Thankfully, this is pretty simple.  The one major complication is that we have some thread management to handle, but it’s really simple stuff.  Here’s what we need to do:
Instantiate a new Accelerometer object.
Create a new ReadingChanged() event handler to monitor the changes in data.
Pass the results of that event back to our page’s thread (the event fires on a different thread).
Use the data values in our application.
Here’s the entirety of my MainPage.xaml.cs file for this example.  You’ll see that I have created three TextBlocks (XText, YText, and ZText) on my MainPage.xaml file, so that I can write the values to the screen.  Also notice that I had to add a new reference to Microsoft.Devices.Sensors to get access to the Accelerometer.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Devices.Sensors;
namespace Day11_Accelerometer
{
public partial class MainPage : PhoneApplicationPage
{
Accelerometer acc = new Accelerometer();
// Constructor
public MainPage()
{
InitializeComponent();
acc.ReadingChanged += new EventHandler(acc_ReadingChanged);
acc.Start();
}
void acc_ReadingChanged(object sender, AccelerometerReadingEventArgs e)
{
Deployment.Current.Dispatcher.BeginInvoke(() => ThreadSafeAccelerometerChanged(e));
}
void ThreadSafeAccelerometerChanged(AccelerometerReadingEventArgs e)
{
XText.Text = e.X.ToString("0.000");
YText.Text = e.Y.ToString("0.000");
ZText.Text = e.Z.ToString("0.000");
}
}
}
  额,模拟器不能够使加速器工作
  的确。如果你下载我的代码并且诧异为什么z轴值为-1,这是因为模拟器认为他在一个水平面上滑动。没有方法来模拟加速器数据。但是一些聪明的家伙发现了解决办法。我正在解密,我的确想写所有的方法,但是有太多的方法,我觉得最好让你知道原理胜过只是写代码。 下面是一些建议,你可以一试:

Reactive Extensions
  交互扩展是一个框架允许你模拟加速器。你没有真实的控制,实际上是他给你产生随机数据,这很简单,比起因为没有手机而停止开发,在MSDN官方网站可以看到更多:http://bit.ly/bdeaft

accelKit
  这是最酷的选项之一。他扩充你的照相头,允许你移动手机像现实中一样。我感到很酷的原因有两点:
  1 允许你测试动作反应
  2 使用扩展实现。很惊人的实现。
  代码详细见:http://bit.ly/9TfqaS

WiimoteLib
  Windows Phone中同样可以使用的类库,工作原理也很惊奇。基本原理就是:如果你有一个任天堂的Wii,你将会通过它来生成数据。Brain Peek是开发者,你可以下载代码:http://bit.ly/aUdEEW

Windows Mobile Unified Sensor API
  在特定的Windows phone 6.5上实现加速器交互鲜果。具体见:http://bit.ly/crHbW9

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-66201-1-1.html 上篇帖子: Windows 7的30个新特性大揭秘 下篇帖子: 转载:The IE Mobile Viewport on Windows Phone 7
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表