Share via


感應器方向

來自加速度計陀螺儀指南針傾斜計方向感應器類別的感應器資料由其參考軸定義。 這些軸由裝置的參考系定義,並隨著使用者轉動裝置而旋轉。 如果您的應用程式支援自動旋轉並在使用者旋轉裝置時重新調整自身方向以適應裝置,則您必須在使用之前調整旋轉的感應器資料。

重要 API

顯示方向與裝置方向

若要了解感應器的參考軸,您必須區分顯示方向與裝置方向。 顯示方向是畫面上顯示的方向文字和影像,而裝置方向則是裝置的實際定位。

注意

正 z 軸會從裝置畫面延伸出來,如下圖所示。 Z-axis for laptop

在下圖中,裝置和顯示方向都在橫向 中 (顯示的感應器軸是橫向特有的)。

下圖顯示橫向的顯示和裝置方向。

Display and device orientation in Landscape

下圖顯示 LandscapeFlipped 的顯示和裝置方向。

Display and device orientation in LandscapeFlipped

此最終圖表顯示橫向顯示方向,而裝置方向為 LandscapeFlipped

Display orientation in Landscape while the device orientation is LandscapeFlipped

您可以使用 GetForCurrentView 方法搭配 CurrentOrientation 屬性,透過 DisplayInformation 類別查詢方向值。 然後,您可以藉由比較 DisplayOrientations 列舉來建立邏輯。 請記住,針對您支援的每個方向,您必須支援將參考軸轉換成該方向。

橫向優先與直向優先裝置

製造商同時生產橫向優先和直向優先裝置。 參考框架會因橫向優先裝置 (例如桌電和筆電) 和直向優先裝置 (例如手機和一些平板) 而有所不同。 下表顯示橫向優先和直向優先裝置的感應器軸。

方向 橫向優先 直向優先
橫向 Landscape-first device in Landscape orientation Portrait-first device in Landscape orientation
直向 Landscape-first device in Portrait orientation Portrait-first device in Portrait orientation
LandscapeFlipped Landscape-first device in LandscapeFlipped orientation Portrait-first device in LandscapeFlipped orientation
PortraitFlipped Landscape-first device in PortraitFlipped orientation Portrait-first device in PortraitFlipped orientation

裝置廣播顯示器和無頭裝置

某些裝置能夠將顯示器廣播到另一部裝置。 例如,您可以採用平板電腦,並將顯示器廣播到將採用橫向的投影機。 在此案例中,請務必記住裝置方向是以原始裝置為基礎,而不是呈現顯示器的裝置方向。 因此加速度計會報告平板電腦的資料。

此外,某些裝置沒有顯示器。 對於這些裝置,這些裝置的預設方向是縱向。

顯示方向和指南針標題

指南針標題取決於參考軸,因此會隨著裝置方向而變更。 您可以根據此表進行補償 (假設使用者面向北)。

顯示方向 指南針標題的參考軸 朝北時,API 指南針標題 (橫向優先) 朝北時,API 指南針標題 (縱向優先) 指南針標題補償 (橫向優先) 指南針標題補償 (直向優先)
橫向 -Z 0 270 標題 (標題 + 90) % 360
直向 Y 90 0 (標題 + 270) % 360 標題
LandscapeFlipped Z 180 90 (標題 + 180) % 360 (標題 + 270) % 360
PortraitFlipped Y 270 180 (標題 + 90) % 360 (標題 + 180) % 360

修改指南針標題,如表格所示,以正確顯示標題。 以下程式碼片段示範如何執行此操作。

private void ReadingChanged(object sender, CompassReadingChangedEventArgs e)
{
    double heading = e.Reading.HeadingMagneticNorth;
    double displayOffset;

    // Calculate the compass heading offset based on
    // the current display orientation.
    DisplayInformation displayInfo = DisplayInformation.GetForCurrentView();

    switch (displayInfo.CurrentOrientation)
    {
        case DisplayOrientations.Landscape:
            displayOffset = 0;
            break;
        case DisplayOrientations.Portrait:
            displayOffset = 270;
            break;
        case DisplayOrientations.LandscapeFlipped:
            displayOffset = 180;
            break;
        case DisplayOrientations.PortraitFlipped:
            displayOffset = 90;
            break;
     }

    double displayCompensatedHeading = (heading + displayOffset) % 360;

    // Update the UI...
}

使用加速計和陀螺儀顯示方向

下表會轉換加速計和陀螺儀資料以顯示方向。

參考座標軸 X Y Z
橫向 X Y Z
直向 Y -X Z
LandscapeFlipped -X -y Z
PortraitFlipped -y X Z

下列程式碼範例會將這些轉換套用至陀螺儀。

private void ReadingChanged(object sender, GyrometerReadingChangedEventArgs e)
{
    double x_Axis;
    double y_Axis;
    double z_Axis;

    GyrometerReading reading = e.Reading;  

    // Calculate the gyrometer axes based on
    // the current display orientation.
    DisplayInformation displayInfo = DisplayInformation.GetForCurrentView();
    switch (displayInfo.CurrentOrientation)
    {
        case DisplayOrientations.Landscape:
            x_Axis = reading.AngularVelocityX;
            y_Axis = reading.AngularVelocityY;
            z_Axis = reading.AngularVelocityZ;
            break;
        case DisplayOrientations.Portrait:
            x_Axis = reading.AngularVelocityY;
            y_Axis = -1 * reading.AngularVelocityX;
            z_Axis = reading.AngularVelocityZ;
            break;
        case DisplayOrientations.LandscapeFlipped:
            x_Axis = -1 * reading.AngularVelocityX;
            y_Axis = -1 * reading.AngularVelocityY;
            z_Axis = reading.AngularVelocityZ;
            break;
        case DisplayOrientations.PortraitFlipped:
            x_Axis = -1 * reading.AngularVelocityY;
            y_Axis = reading.AngularVelocityX;
            z_Axis = reading.AngularVelocityZ;
            break;
     }

    // Update the UI...
}

顯示方向與裝置方向

OrientationSensor 資料必須以不同的方式變更。 將這些不同的方向視為相對於 z 軸逆時針旋轉,因此我們需要反轉旋轉以恢復使用者的方向。 對於四元數資料,我們可以使用 Euler 的公式來定義具有參考四元數的旋轉,也可以使用參考旋轉矩陣。

Euler's formula

若要取得您想要的相對方向,請將參考物件乘以絕對物件。 請注意,這個數學不是通勤的。

Multiply the reference object against the absolute object

在前面的表達式中,絕對物件由感應器資料傳回。

顯示方向 逆時針旋轉繞 Z 參考四元數 (反向旋轉) 參考旋轉矩陣 (反向旋轉)
橫向 0 1 + 0i + 0j + 0k [1 0 0
0 1 0
0 0 1]
直向 90 cos(-45⁰) + (i + j + k)*sin(-45⁰) [0 1 0
-1 0 0
0 0 1]
LandscapeFlipped 180 0 - i - j - k [1 0 0
0 1 0
0 0 1]
PortraitFlipped 270 cos(-135⁰) + (i + j + k)*sin(-135⁰) [0 -1 0
1 0 0
0 0 1]

另請參閱

整合動作與方向感應器