• Sensor(ORIENTATION)


    package com.example.sensor01;
    
    import java.util.List;
    
    import android.hardware.Sensor;
    import android.hardware.SensorEvent;
    import android.hardware.SensorEventListener;
    import android.hardware.SensorManager;
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.widget.TextView;
    
    public class MainActivity extends Activity implements SensorEventListener{
        
        private SensorManager mSensorManager;
        private TextView textview;
        private float[] mags;
        private float[] accs;
        private float[] oris;
        private float[] rotate;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            textview = (TextView) findViewById(R.id.content);
                    
            mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
            //Sensor.TYPE_ORIENTATION 在新版本中已经放弃
            Sensor magsensor  = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
            Sensor accsensor  = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
            
            mSensorManager.registerListener(this, magsensor, SensorManager.SENSOR_DELAY_FASTEST);
            mSensorManager.registerListener(this, accsensor, SensorManager.SENSOR_DELAY_FASTEST);
            
            mags = new float[3];
            accs = new float[3];
            oris = new float[3];
            rotate = new float[9];
        }
    
        @Override
        public void onAccuracyChanged(Sensor arg0, int arg1) {
            // TODO Auto-generated method stub    
        }
    
        @Override
        public void onSensorChanged(SensorEvent event) {
            
            
            if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD){
                mags = event.values;//x轴地磁强度
            }
            
            if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
                accs = event.values;//x轴地磁强度
            }
            
            mSensorManager.getRotationMatrix(rotate, null, accs, mags);
            mSensorManager.getOrientation(rotate, oris);
            /*oris[0]      :azimuth 方向角,但用(磁场+加速度)得到的数据范围是(-180~180),也就是说,0表示正北,90表示正东,180/-180表示正南,-90表示正西。
            oris[1]      :pitch 倾斜角  即由静止状态开始,围绕X轴前后翻转
            oris[2]      :roll 旋转角 即由静止状态开始,围绕Y轴左右翻转*/
            double azimuth = Math.toDegrees(oris[0]);
            if (azimuth<0) {
                azimuth=azimuth + 360;
            }
            double pitch = Math.toDegrees(oris[1]);
            double roll = Math.toDegrees(oris[2]);
            
    
            textview.setText(String.valueOf(azimuth) + "   " 
            + String.valueOf(pitch) + "  " + String.valueOf(roll));                
        }
    }
  • 相关阅读:
    行行网电子书多线程爬取
    51CTO学院IT技术课程抓取
    chromeF12 谷歌开发者工具详解 Network篇
    pyspider无法启动webui
    Python告诉你今天最冷的地方在哪里
    matplotlib画图中文及负号显示问题
    数据分析案例
    seaborn.barplot用法
    Python大数据分析之数据分析
    python中yield的用法详解
  • 原文地址:https://www.cnblogs.com/Fredric-2013/p/4533683.html
Copyright © 2020-2023  润新知