Android sensor acquisition direction Summary

Source: Internet
Author: User

In 2.2, you can set the screen direction to reverse the horizontal screen: setrequestedorientation (8); because the system does not publish the setting of this parameter, but the screen_orientation_reverse_landscape parameter has been defined in the source code, however, the orientation of the screen cannot be fixed to reverse landscape screen or right landscape screen. When setting the screen direction to the Right horizontal screen, you must note that the activity needs to be set to Android: theme = "@ Android: style/theme. translucent ", otherwise, even if setrequestedorientation (8) is set, it will not be displayed as a Right horizontal screen.

Although the orientation of the screen can be obtained from the rotation angle, the screen orientation cannot be set to the Right horizontal screen at 2.1. The following is a record of the source code that gets the screen direction from the sensor based on the angle.

Sensor Monitoring:

Class subsensorlistener implements sensoreventlistener {Private Static final int _ data_x = 0; Private Static final int _ data_y = 1; Private Static final int _ data_z = 2; public static final int orientation_unknown =-1; private handler; Public subsensorlistener (handler) {This. handler = handler;} public void onaccuracychanged (sensor arg0, int arg1) {} public void onsensorchanged (sensoreven T event) {float [] values = event. values; int orientation = orientation_unknown; float x =-Values [_ data_x]; float y =-Values [_ data_y]; float Z =-Values [_ data_z]; float magnshould = x * x + y * Y; // don't trust the angle if the magnshould is small compared to the Y valueif (magn1_* 4> = z * z) {float oneeightyoverpi = 57.29577957855f; float angle = (float) math. atan2 (-y, x) * oneeightyoverpi; orientat Ion = 90-(INT) math. round (angle); // normalize to 0-359 rangewhile (orientation >=360) {orientation-= 360;} while (orientation <0) {orientation + = 360 ;}} if (handler! = NULL) {handler. obtainmessage (123, orientation, 0). sendtotarget ();}}

As the direction changes constantly, we need a handler to handle it. The following is how handler gets the direction through processing the rotation angle:

 
@ Overridepublic void handlemessage (Message MSG) {If (MSG. what == 123) {int orientation = MSG. arg1; If (orientation> 45 & Orientation <135) {// screen_orientation_reverse_landscapeactivity.setrequestedorientation (8);} else if (orientation> 135 & Orientation <225) {// increment (9);} else if (orientation> 225 & Orientation <315) {// screen_orientation_landscapeactivity.setrequestedorientation (0 );} else if (orientation> 315 & Orientation <360) | (orientation> 0 & Orientation <45) {// screen_orientation_portraitactivity.setrequestedorientation (1 );}}}

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.