Orientation Sensor or Direction Sensor in Android Studio

Orientation or Direction sensor in Android Studio:

Orientation Sensor or Direction Sensor in Android Studio- In this article, I am going to show you how to use the orientation or direction sensor to make a simple compass application in android studio.


To say that another commonly used sensor in Android should be the direction sensor. Use of direction sensor in android the scene is wider than other sensors. It can accurately determine the rotation angle of the mobile phone in all directions. Use this from these angles, useful tools such as compass and horizon can be written. The racing game where the device controls the direction is also done using the direction sensor in android. So let’s still take a look first How to use the direction sensor.

Usage of Direction sensor in android:

Through the study of the previous two articles light sensor and accelerometer sensor, I believe you have a good idea about the usage of the direction sensor in android. Yes, we need to Get an instance of Sensor used to represent the direction sensor in android, as shown below:

Then in the onSensorChanged() method, through the values ​​array of SensorEvent, you can get all the sensor values ​​output. The orientation sensor or direction in android will record the rotation angle of the phone in all directions, as shown in the below Figure.

Direction sensor in android

Among them, values[0] records the rotation angle of the phone around the Z-axis, and values[1] records the rotation of the phone around the X-axis Angle, values[2] records the rotation angle of the phone around the Y-axis.

It looks beautiful, doesn’t it? But unfortunately, Android has long abandoned Sensor.TYPE_ORIENTATION.



For this type of sensor, although the code is still valid, it is no longer recommended. In fact, Android gets hand’s direction and angle of the machine’s rotation are calculated by the accelerometer sensor and the geomagnetic sensor. This is also the purpose of Android.

First, we need to obtain instances of the accelerometer sensor and the geomagnetic sensor respectively, and register listeners for them, such as Shown below:

Since the accuracy requirements of the direction sensor in android are usually relatively high, here we increase the update rate of the sensor output information. It’s a bit higher, using SENSOR_DELAY_NORMAL.

Next, you can get the values ​​array of SensorEvent in the onSensorChanged() method, and record them separately The output value of the acceleration sensor and the geomagnetic sensor. Then pass these two values ​​to the SensorManager You can get an R array containing the rotation matrix in the getRotationMatrix() method, as shown below:

The first parameter R is a float array with a length of 9, the rotation calculated by the getRotationMatrix() method The data will be assigned to this array. The second parameter is a rotation moment used to convert the geomagnetic vector into gravitational coordinates Array, usually specified as null. The third and fourth parameters are the output of the acceleration sensor and the geomagnetic sensor, respectively values.

 After getting the R array, you can then call the getOrientation() method of SensorManager to calculate the phone The rotation data is as follows:

values ​​is a float array with a length of 3, the rotation data of the phone in all directions will be stored in this number In the group. Among them, values[0] records the rotation of the mobile phone around the Z-axis as see in the above Figure, and values[1] records the phone’s circumference. The arc of rotation around the X-axis, values[2] records the arc of rotation of the phone around the Y-axis.

Note that the data calculated here are all in radians, so if you want to convert them into angles, you need to adjust Use the following method:

Well, the basic usage is like this, let’s take a practical operation.


Make a simple Compass Application in Android Studio:

I believe it is not difficult for you to complete a simple compass program It’s up. In fact, the realization principle of the compass is not complicated, we only need to detect the rotation angle of the mobile phone around the Z-axis, and then Just deal with this value.

Click on the new project

Direction sensor in android

Then select empty activity and click next button.

Direction sensor in android

Then set the application name in my case I set “CompassDemo”, and press the finish button.

Direction sensor in android


As you can see our project is created successfully.

Direction sensor in android

Direction Sensor Programming:

Java Code:

Now modify the code in MainActivity.java as follows:



program explanation:

The code is not very long, I believe it should be effortless for you to understand. In the onCreate() method, we obtained Examples of acceleration sensors and geomagnetic sensors and registered listeners for them. Then in the onSensorChanged() side In the method, if the current SensorEvent contains an acceleration sensor, assign the values ​​array to accelerometerValues ​​array, if the current SensorEvent contains a geomagnetic sensor, assign the values ​​array The value is given to the magneticValues ​​array. Note that you must call the clone() method of the values ​​array when assigning values, not However, accelerometerValues ​​and magneticValues ​​will point to the same reference.

Next, we created an R array of length 9 and a values ​​array of length 3 and then called The getRotationMatrix() method assigns values ​​to the R array, and then calls the getOrientation() method to assign values ​​to the values ​​array, then The values ​​already contain the radian of the phone’s rotation in all directions. Among them, values[0] means that the phone rotates around the Z-axis Here we call the Math.toDegrees() method to convert it into angles and print them out.

Now run the program and rotate the phone around the Z-axis, the angle of rotation will continue to be displayed in LogCat Printed out, as shown below in Figure.

Direction sensor in android

The value range of values[0] is -180 degrees to 180 degrees, where ±180 degrees means true south direction, and 0 degrees means true north -90 degrees indicates the direction of due west, and 90 degrees indicates the direction of east.

Although we have obtained these values ​​so far, it is obvious that we want to use them to determine the current direction of the mobile phone. It’s a nerve-wracking thing, so we have to find a way to visually display the current direction. Undoubtedly, the most intuitive way of course is to display through the compass and pointer, so let’s continue to improve the CompassDemo project.

Here I have design two pictures in adobe Photoshop 7.0, compass.png, and needle.png,

Direction sensor in android

which are used as the background and Pointer, as shown in the Figures:

 

Direction sensor in android

 

 

Direction sensor in android

Download these images and paste into drawable folder in android studio

Direction sensor in android

Direction sensor in android


XML Code:

Now modify the code in activity_main.xml as follows:

As you can see, we put two ImageView controls in the RelativeLayout and let these two controls CenterHorizontal display, the pictures displayed in ImageView are the background and pointer of the compass respectively. Next, modify the code in MainActivity as follows:


complete java code:


Program explanation:

Here we first get an instance of ImageView in the onCreate() method, which is used to display the compass Background image. Then in the onSensorChanged() method to use the rotation animation technology, we created An instance of RotateAnimation, and six parameters are passed to its construction method, the first parameter represents the starting angle of rotation Degree, the second parameter indicates the end angle of rotation, and the last four parameters are used to specify the center point of rotation. Here we take from The rotation angle obtained from the sensor is reversed and passed to RotateAnimation, and the center point of rotation is designated as the compass back The center of the scene image, and then call the startAnimation() method of ImageView to perform the rotation animation.

Okay, there are so many codes for compass but this is just a simple program, now we re-run the program, and then rotate the phone at will, the background of the compass will also rotate with it, as shown in the below video.

 

In this way, a simple compass application is created in android studio.

Recommended For You

About the Author: Fawad

My name is Shahzada Fawad and I am a Programmer. Currently, I am running my own YouTube channel "Expertstech", and managing this Website. My Hobbies are * Watching Movies * Music * Photography * Travelling * gaming and so on...

Leave a Reply

%d bloggers like this: