android设备与垂直轴的角度

标签 android orientation device

我正在开发一个应用程序,我需要检索设备与垂直轴(指向地球中心的轴)之间的角度。

到目前为止,我找到的所有文档和教程都不是很确定。

能否请您解释一下我该怎么做,或者提供一个指向清晰教程的链接以帮助我找到解决此问题的方法?

最佳答案

首先,我创建了一个 SensorEventListener 实现

private SensorEventListener sensorEventListener = 
    new SensorEventListener() {

    /** The side that is currently up */
    //private Side currentSide = null;
    //private Side oldSide = null;
    private float azimuth;
    private float pitch;
    private float roll;

    public void onAccuracyChanged(Sensor sensor, int accuracy) {}

    public void onSensorChanged(SensorEvent event) {            
        azimuth = event.values[0];     // azimuth
        pitch = event.values[1];     // pitch
        roll = event.values[2];        // roll
        //code to deal with orientation changes;
        //pitch is the angle between the vertical axis and the device's y axis (the one from the center of the device to its top)
    } 
};

然后,我将此监听器注册到方向传感器

SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
Sensor sensor;
List<Sensor> sensors = sensorManager.getSensorList(
        Sensor.TYPE_ORIENTATION);
if (sensors.size() > 0) {
    sensor = sensors.get(0);
    sensorManager.registerListener(
            sensorEventListener, sensor, 
            SensorManager.SENSOR_DELAY_NORMAL);
} else {
    //notify the user that there's no orientation sensor
}

关于android设备与垂直轴的角度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7261732/

相关文章:

java - 我可以对不同 Activity 中的两个不同 ListView 使用相同的适配器吗?

android - 如何在 Android 中拖动图库中的图像时避免选择器

android - Recyclerview scrollToPosition 不适用于 NestedScrollView 吗?到底有没有

ios - 如何将 "force" View 在设备横向加载/显示时纵向显示?

docker - Mac Os X 下的移动设备连接到 Docker

linux - 发现我的 USB 扬声器(设备)在哪里

android - 如何直接从 Intent 打开字体大小设置?

android - 如何处理来自移动设备的视频方向

android - 尝试在 android 设备上调试我的应用程序时获取 "Launch error: Failed to connect to remote VM. Connection timed out"

iphone - 如何更改 iPhone 左右相机 Overlayview 方向横向?