如何在 java 中制作 Android class returns 设备角度

How to make an Android class in java that returns device angle

我正在尝试制作一个使用设备角度的应用程序。我想弄清楚如何制作一个 class 的函数,我可以在另一个文件中调用该函数,该文件将 return 设备 Y 角度。我以前从未使用过传感器,并且在尝试设置它们时遇到了麻烦。我将从中调用函数的文件是 运行 在前台。有谁知道如何执行此操作并可以共享一些代码?谢谢!

您可以尝试查看Android google CodeLabs。我也在网上找到了一个解决方案,我稍微修改了一下,但我不确定显示的数据是否总是正确的。此示例应用程序始终跟踪设备旋转,因此如果您想要 returns 旋转的功能,则必须稍微重构代码。

MainActivity.java:

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity
{
    private TextView textViewToDisplayRotation;
    private float[] acc = new float[3];
    private float[] mags = new float[3];
    private float[] values = new float[3];
    SensorManager sManager;
    private SensorEventListener mySensorEventListener = new SensorEventListener()
    {
        public void onAccuracyChanged(Sensor sensor, int accuracy)
        {
        }

        public void onSensorChanged(SensorEvent event)
        {
            switch (event.sensor.getType())
            {
                case Sensor.TYPE_MAGNETIC_FIELD:
                    mags = event.values.clone();
                    break;
                case Sensor.TYPE_ACCELEROMETER:
                    acc = event.values.clone();
                    break;
            }

            if (mags != null && acc != null)
            {
                float[] gravity = new float[9];
                float[] magnetic = new float[9];
                SensorManager.getRotationMatrix(gravity, magnetic, acc, mags);
                float[] outGravity = new float[9];
                SensorManager.remapCoordinateSystem(gravity,
                                                    SensorManager.AXIS_X,
                                                    SensorManager.AXIS_Z,
                                                    outGravity);
                SensorManager.getOrientation(outGravity, values);

                float azimuth = values[0] * 57.2957795f;
                float pitch = values[1] * 57.2957795f;
                float roll = values[2] * 57.2957795f;
                textViewToDisplayRotation.setText("X = " + azimuth + "\nY = " + pitch + "\nZ = " + roll);
                mags = null;
                acc = null;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        textViewToDisplayRotation = findViewById(R.id.textViewToDisplayRotation);
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        sManager.registerListener(mySensorEventListener,
                                  sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                                  SensorManager.SENSOR_DELAY_NORMAL);
        sManager.registerListener(mySensorEventListener,
                                  sManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
                                  SensorManager.SENSOR_DELAY_NORMAL);
    }
}

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textViewToDisplayRotation"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:textSize="40sp" />