Get Android rotation angle in x-axis

前端 未结 2 1351
旧时难觅i
旧时难觅i 2020-12-16 06:59

I\'m experimenting with some Android functions. Right now, I\'m trying to get the rotation angle of the device, so when I show a happy face bitmap on a canvas, it always loo

相关标签:
2条回答
  • 2020-12-16 07:28

    Found a better answer! (Thanks to forgivegod for helping me starting this!)

    ORIENTATION is now deprecated and uses to many resources. Plus, it works with reference to the magnetic north, which gives not the pure rotation of the device, but actually always points to the north.

    By using ACCELEROMETER, one can calculate the rotation angle of the device with values [0] and [1] this way:

        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);
        }
    

    Maybe this can help someone else in the future. Thanks a lot!

    0 讨论(0)
  • 2020-12-16 07:39

    There is azimuth, pitch and roll available to you. Not sure which one relates to your "x-axis" but I think its azimuth.

    Time for helpful links:

    1. http://www.workingfromhere.com/blog/2009/03/30/orientation-sensor-tips-in-android/
    2. http://marakana.com/forums/android/examples/43.html
    3. http://marakana.com/static/tutorials/SensorDemo.zip
    0 讨论(0)
提交回复
热议问题