android 陀螺仪

//初始化

    public static void init(Context context) {
        sensorManager = (SensorManager) context.getSystemService(SENSOR_SERVICE);
        //已弃用
        sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
        /*查看手机所有传感器类型
        List sensorList = sensorManager.getSensorList(Sensor.TYPE_ALL);
        for (Sensor s : sensorList){
            System.out.println(s.getName());
        }
        */
        Sensoreventlistener ll = new Sensoreventlistener();
        sensorManager.registerListener(ll, sensor, SensorManager.SENSOR_DELAY_NORMAL);

        //注册加速度传感器监听
        Sensor acceleSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sensorManager.registerListener(ll, acceleSensor, SensorManager.SENSOR_DELAY_NORMAL);
        //注册磁场传感器监听
        Sensor magSensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
        sensorManager.registerListener(ll, magSensor, SensorManager.SENSOR_DELAY_NORMAL);

    }
//事件监听
//加速度参数与磁场参数
    static private float[] gravity = null;
    static private float[] geomagnetic = null;

    static private long lastrefresh = System.currentTimeMillis();

    static class Sensoreventlistener implements SensorEventListener {

        @Override
        public void onSensorChanged(SensorEvent sensorEvent) {

            if (System.currentTimeMillis() - lastrefresh > 10) {
                lastrefresh = System.currentTimeMillis();
                try {
                    switch (sensorEvent.sensor.getType()) {
                        case Sensor.TYPE_ACCELEROMETER: //加速度传感器
                            gravity = sensorEvent.values.clone();
                            break;
                        case Sensor.TYPE_MAGNETIC_FIELD://磁场传感器
                            geomagnetic = sensorEvent.values.clone();
                            break;
                    }
                    //计算-方位角、俯仰角与翻滚角
                    if (gravity != null && geomagnetic != null) {
                        //方位角、俯仰角与翻滚角
                        float[] values = new float[3];
                        float[] R = new float[9];
                        if (SensorManager.getRotationMatrix(R, null, gravity, geomagnetic)) {
                            SensorManager.getOrientation(R, values);

                            //横屏
                            currentDegreeLandscape = (float) ((360f + values[0] * 180f / Math.PI) % 360);
                            //右上横屏
                            currentDegreeLandscape = (currentDegreeLandscape + 90) % 360;
                            //右下横屏
                            if (values[2] > 0) {
                                currentDegreeLandscape = (currentDegreeLandscape + 180) % 360;
                            }

                            //竖屏
                            currentDegreePortrait = (float) ((360f + values[0] * 180f / Math.PI) % 360);

                            //判断是否有翻转
                            //取绝对值
                            float v2 = (values[2] < 0) ? -values[2] : values[2];
                            if (v2 > Math.PI / 2) {
                                currentDegreePortrait = (currentDegreePortrait + 180) % 360;
                            }

                            System.out.println(values[2]);
                        }
                    }

                    for (int i = 0; i < listChange.size(); i++) {
                        if (listChange.get(i) != null) {
                            listChange.get(i).change();
                        }
                    }
                } catch (Exception ex) {
                    System.out.println(ex.toString());
                }
            }
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }
    }

//方位角--屏幕超下时,依然会这样定位数据,所以如果仰视角拍摄就可以会出现方向翻转

值域:-π~π

android 陀螺仪_第1张图片

//俯仰角

围绕以上图的东西方向轴进行转动,屏幕上方部分上翘为负,下方部分上翘为正,水平时为:0

屏幕上方部分在上垂直时:π/2,屏幕上方部分在上垂直时:-π/2,即值域为:-π/2~π/2,超过时会计算为翻转角

//滚转角

围绕以上图的南北方向轴转动,左侧向上为正,右侧向下为负,水平时为:0

屏幕左方部分向上垂直时:π/2,屏幕右方部分向上垂直时:-π/2,屏幕面水平向下时为π或者-π。

即值域为:-π~π

你可能感兴趣的:(android,java,开发语言)