Android设备的角度垂直轴 [英] android device angle to vertical axis

查看:115
本文介绍了Android设备的角度垂直轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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
}

这篇关于Android设备的角度垂直轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆