Get Android rotation angle on x axis

I am experimenting with some Android features. Right now, I'm trying to get the angle of rotation of the device, so when I display a bitmap image of a happy face on the canvas, it always looks β€œstraight”.

All I need for this is to get the angle of rotation of the X axis (I think), but I cannot find how to achieve it. I hope you help me. Thanks.

+4
source share
2 answers

Azimuth, feed and roll are available. Not sure which one refers to your "x axis," but I think it is an azimuth.

Time for useful links:

+2
source

Found the best answer! (Thank you, sorry for helping me get started!)

ORIENTATION is now deprecated and used for many resources. In addition, it works with reference to magnetic north, which does not give a pure rotation of the device, but in fact always indicates north.

Using ACCELEROMETER, you can calculate the angle of rotation of the device with the values ​​[0] and [1] as follows:

public void onSensorChanged(SensorEvent event) { // TODO Auto-generated method stub if (event.sensor != mSensor) return; aX= event.values[0]; aY= event.values[1]; //aZ= event.values[2]; angle = Math.atan2(aX, aY)/(Math.PI/180); } 

Perhaps this may help someone else in the future. Thank you very much!

+15
source

All Articles