Android设备的角度垂直轴角度、设备、Android

由网友(回忆很美 未来很长)分享简介:我开发的应用程序,其中我将要求来检索设备和垂直轴之间的角度(轴指向地球的中心)。I am developing an application where I would require to retrieve the angle between the device and the vertical axis (th...

我开发的应用程序,其中我将要求来检索设备和垂直轴之间的角度(轴指向地球的中心)。

I am developing an application where I would require to retrieve the angle between the device and the vertical axis (the axis pointing to the center of the Earth).

到目前为止,所有的文档和教程,我发现不是非常确凿的。

So far, all the documentations and tutorials I found were not very conclusive.

能否请您解释一下我怎样才能做到这一点,或给我提供一个链接到一个明确的教程,以帮助我找到解决这个问题呢?

Could you please explain me how can I do this or provide me with a link to a clear tutorial to help me find a solution to this problem?

推荐答案

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

First, I created a SensorEventListener implementation

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)
    } 
};

然后,我注册这个监听器的取向研究传感器

Then, I register this listener to an Orientaion Sensor

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
}
阅读全文

相关推荐

最新文章