【问题标题】:Android Device Orientation is Landscape but Sensors Behaves Like the Orientation is PortraitAndroid 设备方向是横向,但传感器的行为就像方向是纵向
【发布时间】:2013-06-26 12:49:50
【问题描述】:

我在 android 中遇到了方向问题。设备方向是我在 AndroidManifest.xml 文件中设置的横向。

android:screenOrientation="landscape"

此外,我将设备保持在横向状态,因此所有内容(布局、视图等)都处于我想要的横向模式。但只有当我以纵向方式握住设备时,它才会为我提供所需的价值。

我从互联网上修改了一些代码,它适用于 HTC 3d EVO、Nexus 7 和三星 Galaxy S3,但不适用于 Galaxy Tablet 10"。

我找到了这篇文章。 Android Orientation Sensor Different for Different Devices? 接受的答案建议使用 getRotation() 和 remapCoordinateSystem() 方法,但没有提及如何使用。

这就是我的结局。

        SensorManager.getRotationMatrix(Rmat, Imat, gData, mData);
        SensorManager.remapCoordinateSystem(Rmat, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, R2);
        // Orientation isn't as useful as a rotation matrix, but
        // we'll show it here anyway.
        SensorManager.getOrientation(R2, orientation);
        float incl = SensorManager.getInclination(Imat);

        FontTextView pitchText = (FontTextView)findViewById(R.id.pitch_text);

        if((int)(orientation[1]*90) <= -110 && !newQuestionIsActive) {
            newQuestionIsActive = true;
            pitchText.setText("Bring new question");
        }
        else if(Math.abs((int)(orientation[2]*90)) <= 200 && (int)(orientation[1]*90) >= -30 && newQuestionIsActive) {
            newQuestionIsActive = false;
            pitchText.setText("Not Correct!");
        }
        else if(Math.abs((int)(orientation[2]*90)) > 200 && (int)(orientation[1]*90) >= -30 && newQuestionIsActive) {
            newQuestionIsActive = false;
            pitchText.setText("Congratulations!");
        }

现在我应该如何在这段代码中使用 getRotation() ?并且可能会简要解释一下为什么提到的 3 种设备可以正常工作,但 Galaxy 平板电脑却不行。

关于这个问题 android 文档说;

最后,如果您的应用程序将传感器数据与屏幕上的数据相匹配 展示, 您需要使用 getRotation() 方法来确定屏幕旋转,然后使用 remapCoordinateSystem() 方法将传感器坐标映射到屏幕坐标。 即使您的清单指定仅纵向显示,您也需要这样做。

http://android-developers.blogspot.com/2010/09/one-screen-turn-deserves-another.html

【问题讨论】:

    标签: android orientation android-sensors device-orientation


    【解决方案1】:

    找到了解决办法。

    问题是由于一些较新设备的默认方向设置为横向,而其他设备的默认方向设置为纵向。因此,传感器管理器会做出相应的行为。为了让传感器管理器按预期工作,您需要检测设备的 defaultDisplayRotation 并更改 remapCoordinateSystem() 参数逻辑。

    我变了

    SensorManager.remapCoordinateSystem(Rmat, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, R2);
    

    int rotation = ((WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
    if(rotation == 0) // Default display rotation is portrait
        SensorManager.remapCoordinateSystem(Rmat, SensorManager.AXIS_MINUS_X, SensorManager.AXIS_Y, R2);
    else   // Default display rotation is landscape
        SensorManager.remapCoordinateSystem(Rmat, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, R2);
    

    现在可以了... 希望这对其他人有用!

    【讨论】:

    • 此解决方案中的 R2 是什么?
    • @hubatish R2 是新矩阵,您应该将其用作 SensorManager.getOrientation() api 而不是 Rmat 的输入矩阵。
    【解决方案2】:

    我不知道你的方式。但以下方式对我有用。

    int orientation = getResources().getConfiguration().orientation;
    

    如果设备处于landscape,则orientation 为1,如果设备处于portrait 模式,则为2。

    希望对你有所帮助。

    【讨论】:

    • 我使用这个 sn-p 来获取设备的方向,但它不能解决我的问题。问题出在某些设备上,传感器仅适用于纵向。它不会将其方向旋转到设备方向。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    相关资源
    最近更新 更多