Android obtains basic three-axis knowledge

Source: Internet
Author: User

Android obtains basic three-axis knowledge

 

 

Basic knowledge

 

 

Rotation Angle around the Z axis: Azimuth, I call it the north-facing corner (the north-facing direction of the compass)

Rotation Angle around the X axis: Pitch.

Rotation Angle around the Y axis: Roll, I call it the left and right corner.

 

Solution 1: Magnetic Field Sensor + acceleration sensor (recommended)

Step 1: Obtain sensor system services

Private SensorManager sensorManager; initialize protected void onCreate (Bundle savedInstanceState) {sensorManager = (SensorManager) getSystemService (Context. SENSOR_SERVICE );}

Step 2: Initialize the three-axis variable of acceleration and the three-axis variable of the Magnetic Field Sensor

 

 

private float[] accelerometerValues;private float[] magnetFieldValues;

 

Step 3: rewrite the listener

Private final SensorEventListener sensorEventListener = new SensorEventListener () {@ SuppressWarnings (deprecation) @ Overridepublic void onSensorChanged (SensorEvent event) {// method stub automatically generated by TODO if (event. sensor. getType () = Sensor. TYPE_ACCELEROMETER) {accelerometerValues = event. values;} if (event. sensor. getType () = Sensor. TYPE_MAGNETIC_FIELD) {magnetFieldValues = event. values ;}}

Step 4: register the sensor to the listener

 
@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main);/* Get the system's sensor Service */sensorManager = (SensorManager) getSystemService (Context. SENSOR_SERVICE); getSensorService ();}


 
private void getSensorService() {//int rate=SensorManager.SENSOR_DELAY_FASTEST;int rate=SensorManager.SENSOR_DELAY_NORMAL;Sensor accelerometerSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);sensorManager.registerListener(sensorEventListener,accelerometerSensor, rate);Sensor magnetSensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);sensorManager.registerListener(sensorEventListener, magnetSensor,rate);}

Step 5: Obtain the current direction (MS at a scheduled time)

 

@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main);/* Get the system's sensor Service */sensorManager = (SensorManager) getSystemService (Context. SENSOR_SERVICE); getSensorService (); Timer updateTimer = new Timer (gForceUpdate); updateTimer. scheduleAtFixedRate (new TimerTask () {@ Overridepublic void run () {// method stub automatically generated by TODO if (accelerometerValues! = Null & magnetFieldValues! = Null) {float [] values = new float [3]; float [] R = new float [9]; SensorManager. getRotationMatrix (R, null, accelerometerValues, magnetFieldValues); SensorManager. getOrientation (R, values); values [0] = (float) Math. toDegrees (values [0]); // values [1] = (float) Math. toDegrees (values [1]); // values [2] = (float) Math. toDegrees (values [2]); // left and right corner/* Insert the three-axis direction into the database */}}, 0,200 );}

 

Solution 2: Use discarded virtual orientation Sensors

 

Step 1: Obtain sensor system services

 

Private SensorManager sensorManager; initialize protected void onCreate (Bundle savedInstanceState) {sensorManager = (SensorManager) getSystemService (Context. SENSOR_SERVICE );}

 

Step 2: Initialize the direction variable

 

 

private float x_orientation = -999;private float y_orientation = -999;private float z_orientation = -999;
Step 3: rewrite the listener

 

 

Private final SensorEventListener sensorEventListener = new SensorEventListener () {@ SuppressWarnings (deprecation) @ Overridepublic void onSensorChanged (SensorEvent event) {// method stub automatically generated by TODO if (event. sensor. getType () = Sensor. TYPE_ORIENTATION) {orientationValues = event. values; z_orientation = event. values [0]; // north-facing corner x_orientation = event. values [1]; // y_orientation = event. values [2]; // left and right corner }}@ Overridepublic void onAccuracyChanged (Sensor sensor, int accuracy) {// method stub automatically generated by TODO }};

 

Step 4: register the Sensor

 

 
@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main);/* Get the system's sensor Service */sensorManager = (SensorManager) getSystemService (Context. SENSOR_SERVICE); getSensorService ();}

 

 
private void getSensorService() {//     int rate=SensorManager.SENSOR_DELAY_FASTEST;       int rate=SensorManager.SENSOR_DELAY_NORMAL;       @SuppressWarnings(deprecation)       Sensor orientationSensor = sensorManager              .getDefaultSensor(Sensor.TYPE_ORIENTATION);       sensorManager.registerListener(sensorEventListener,orientationSensor,              rate);    }

 

Step 5: Get the current direction-set the direction for MS

@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main);/* Get the system's sensor Service */sensorManager = (SensorManager) getSystemService (Context. SENSOR_SERVICE); getSensorService (); Timer updateTimer = new Timer (gForceUpdate); updateTimer. scheduleAtFixedRate (new TimerTask () {@ Overridepublic void run () {// method stub automatically generated by TODO if (x_orientation! =-999 & y_orientation! =-999 & z_orientation! =-999) {/* Insert the three-axis direction to the database */}}, 0,200 );}

Note: You need to register the sensor in onResume () and cancel the sensor listening in onDestroy (), onDestroy (), and onPause ().

@ Overrideprotected void onResume () {// method stub automatically generated by TODO super. onResume (); getSensorService () ;}@ Overrideprotected void onDestroy () {// TODO Auto-generated method stubsuper. onDestroy (); sensorManager. unregisterListener (sensorEventListener) ;}@ Overrideprotected void onPause () {// method stub automatically generated by TODO super. onPause (); sensorManager. unregisterListener (sensorEventListener) ;}@ Overrideprotected void onDestroy () {// TODO Auto-generated method stubsuper. onDestroy (); sensorManager. unregisterListener (sensorEventListener );}













 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.