简体   繁体   English

Android设备物理旋转读数

[英]Android Device Physical Rotation readings

I'm trying to read out the physical orientation of the android device in comparison to a specific start-rotation, but I cannot seem to get a proper rotation out of it, no matter what Sensor I use. 与特定的启动旋转相比,我试图读取android设备的物理方向,但是无论我使用哪种传感器,我似乎都无法从中获得正确的旋转。 I initialize the sensor like this: 我像这样初始化传感器:

mySensorManager.GetDefaultSensor(SensorType.RotationVector);

I have tried reading the 'Values' of the SensorChanged event out as X, Y, Z on index 0, 1 and 2. This did give some numbers that changed as I rotated the device, however I can't seem to figure out how I can get the rotational value around a specific axis 我尝试读取SensorChanged事件的“值”,分别为索引0、1和2上的X,Y,Z。这确实给出了一些数字,这些数字在我旋转设备时发生了变化,但是我似乎无法弄清楚我可以获得围绕特定轴的旋转值

I'm developing on a Tablet in Portrait mode. 我正在以“人像”模式在平板电脑上进行开发。 This should still work, even though tablets are generally in Landscape mode. 即使平板电脑通常处于横向模式,这仍然应该可以使用。 That's why I'm led to believe this is why the readings it reports are not a simple 0,0,0 but more likely something like 90,0,0. 这就是为什么我被认为是这就是为什么它报告的读数不是简单的0,0,0而是更可能是90,0,0之类的原因。

It's not an easy problem to explain (partially because I'm not sure I even understand it completely), and as such an even more difficult problem to solve. 这不是一个容易解释的问题(部分原因是我不确定我是否完全理解它),因此这是一个更难解决的问题。 So basically I'm making this question in hopes that someone out there has encountered a similar scenario, and may be able to help me. 所以基本上我是在问这个问题,希望有人能遇到类似的情况,并可能对我有所帮助。

Or perhaps just a 3D vector math genius looking to spend a few minutes on a random question. 或者,也许只是3D矢量数学天才希望在随机问题上花费几分钟。

Note: 注意:

If the values reported by the device indeed DOES report different values, depending on whether it's a phone or tablet device, I'd really appreciate a solution that handles this, and outputs a value that's equal across all devices. 如果设备报告的值确实的确报告了不同的值,具体取决于它是电话还是平板设备,那么我将非常感激能够处理此问题的解决方案,并在所有设备上输出均相等的值。 Though any input is appreciated of course. 虽然任何输入当然是赞赏的。

So after a lot of digging around, and some porting of Java code, I managed to get it working using code from this blogpost: Using orientation sensors: Simple Compass sample 因此,经过大量研究和Java代码的一些移植,我设法使用此博客文章中的代码使它工作: 使用方向传感器:简单的Compass示例

Here's how my SensorChanged method ended up looking: 这是我的SensorChanged方法最终的外观:

    private float[] mGravity;
    private float[] mGeomagnetic;
    public void OnSensorChanged(SensorEvent e)
    {
        if (e.Sensor.Type == SensorType.Accelerometer)
            this.mGravity = e.Values.ToArray();
        if (e.Sensor.Type == SensorType.MagneticField)
            this.mGeomagnetic = e.Values.ToArray();
        if (this.mGravity != null && this.mGeomagnetic != null)
        {
            float[] R = new float[9];
            float[] I = new float[9];
            bool success = SensorManager.GetRotationMatrix(R, I, this.mGravity, this.mGeomagnetic);
            if (success)
            {
                var orientation = new float[3];
                SensorManager.GetOrientation(R, orientation);

                var rotation = new Vector3(orientation[0], orientation[1], orientation[2]);
                this.ReadingReceived?.Invoke(this, rotation);
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Studio 看不到我的物理设备 - Android Studio does not see my physical device Android Espresso 测试在物理设备上失败(PerformException) - Android Espresso test failed on physical device (PerformException) 如何在android中防止设备轮换的冲突动画? - How to prevent conflict animation with device rotation in android? 在物理设备上运行 android studio 应用程序“网络连接错误” - Running android studio app on physical device "ERROR IN NETWORK CONNECTION" 使用 appium java 长按 android 设备上的自定义物理按钮 - Long press a custom physical button on android device with appium java Android加速度计和磁力计同时读取 - Android Accelerometer and Magnetometer readings simultaneously Android颜色在我的物理设备上不起作用 - Android colors don't work on my physical device 我的 android 应用程序在我的虚拟设备上运行,但不在我的物理 android 设备上 - My android app is working on my virtual device, but not on my physical android device 如何在 ARcore 中获取我的 android 设备的旋转四元数? - How to get Rotation Quaternion of my android device in ARcore? 即使在Android中锁定设备旋转后,屏幕也会旋转 - Screen gets rotated even after locking device's rotation in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM