【问题标题】:How do I calculate angle from roll in landscape mode for Windows Phone 8如何计算 Windows Phone 8 横向模式下的滚动角度
【发布时间】:2013-07-26 17:29:00
【问题描述】:

所以,我一直在测试 Windows Phone 8 的传感器 API。对于我的应用,我使用了 Combined Motion (http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202984(v=vs.105).aspx)。

在纵向模式下一切正常,我将滚动参数(我跳过偏航和俯仰)转换为度数,我可以在屏幕上以度数打印出角度。如果我将手机放在面前并逆时针旋转,我会看到度数从 0 变为 -20、-30 等等。

但是,当我将其翻转到横向模式时,我得到了错误的角度。在对偏航、俯仰和滚动(阅读:飞机)以及它如何应用于手机(Android、WP)进行了一些阅读之后,我意识到滚动角只能在以下范围内被信任 [ -90,90] 度。打印出滚动值后,这是我看到的,它确认了范围(点击链接以显示图像:http://i.stack.imgur.com/CPqG1.png

这意味着我无法区分 -45 度和 -135 度,因为滚动值是相同的。

所以,我的问题是:当我顺时针(或逆时针)转动它时,我该怎么做才能让我的应用打印绝对角度(从 0 到 360)?

肯定有办法确定(在横向模式下)手机是否与地板平行?

我正在使用 VS2012/C#/Windows Phone 8 SDK。

感谢任何指向正确方向的指针,也许我需要复习我的高中数学?

【问题讨论】:

标签: windows-phone-7 landscape angle motion


【解决方案1】:

要确定纵向/横向方向,您可以使用页面上的 OrientationChanged 事件。

首先在页面初始化时确定方向:

private PageOrientation lastOrientation;

// Constructor
public MainPage()
{
    InitializeComponent();

    lastOrientation = this.Orientation;

    if ((lastOrientation == PageOrientation.Landscape) ||
        (lastOrientation == PageOrientation.LandscapeLeft) ||
        (lastOrientation == PageOrientation.LandscapeRight))
    {
         DoLandscapeCalculation();
    }
    else if ((lastOrientation == PageOrientation.Portrait) ||
             (lastOrientation == PageOrientation.PortraitDown) ||
             (lastOrientation == PageOrientation.PortraitUp))
    {
         DoPortraitCalculation();
    }

    ...

然后注册 OrientationChanged 事件并采取相应措施:

private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
{

    if ((e.Orientation == PageOrientation.Landscape) ||
        (e.Orientation == PageOrientation.LandscapeLeft) ||
        (e.Orientation == PageOrientation.LandscapeRight))
    {
        //Landscape
        DoLandscapeCalculation();
    }
    else if ((e.Orientation == PageOrientation.Portrait) ||
             (e.Orientation == PageOrientation.PortraitUp) ||
             (e.Orientation == PageOrientation.PortraitDown))
    {
         //Portrait
         DoPortraitCalculation();
    }

    // Extra work here!

    ...

    lastOrientation = e.Orientation;
}

希望这是你的开始。

问候,

【讨论】:

  • 感谢vidonism,我很清楚Orientation 事件。我的问题涉及您调用 DoPortraitCalculation() 和 DoLandscapeCalculation() 的方法的内容。我从 Motion API 获得的滚动值不会改变,它们是相同的,无论方向如何。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多