COCOS2DX 2.x Android gravity detection rotating screen

Source: Internet
Author: User

In the case of charging, when playing the phone, the screen generally needs a specific direction of rotation.

The functional implementation is divided into two parts: part according to the mobile phone gravity direction x, Y, Z to obtain the desired angle, on the other hand, depending on the rotation angle, set screen rotation direction

Calculates the angle around an axis by listening for the value of the phone relative to the x, Y, z direction. The x, y direction is parallel to the phone interface, and z is perpendicular to the phone interface.

This article takes the rotation around the y axis as an example, if you need to simply swap y and z around the z-axis, only these two cases are normally required.

The code draws on the Internet.

Package Com.gamemaster.orientation;import Android.hardware.sensor;import Android.hardware.sensorevent;import Android.hardware.sensoreventlistener;import Android.os.handler;public class Orientationsensorlistener 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 Rotatehandler; Public Orientationsensorlistener (Handler Handler) {rotatehandler = Handler;} public void onaccuracychanged (Sensor arg0, int arg1) {//TODO auto-generated method stub}public void Onsensorchanged (Senso        Revent 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 magnitude = x*x + z*z;       Don ' t trust the angle if the magnitude is small compared to the Y value if (magnitude * 4 >= y*y) {float oneeightyoverpi = 57.29577957855f;            float angle = (float) math.atan2 (-Z, X) * ONEEIGHTYOVERPI;            Orientation =-(int) math.round (angle);            Normalize to 0-359 range while (orientation >=) {orientation-= 360;            } while (Orientation < 0) {orientation + = 360; }} if (Rotatehandler!=null) {rotatehandler.obtainmessage (888, orientation, 0). Sendtotarget ();}}

The second part sets the screen orientation according to the rotation angle.

Package Com.gamemaster.orientation;import;import Android.os.handler;import android.os.Message ; Import Android.util.log;public class Changeorientationhandler extends Handler {private Activity Activity;public Changeorientationhandler (activity AC) {super (); activity = AC;} @Overridepublic void Handlemessage (Message msg) {  if (msg.what==888) {int orientation = msg.arg1;if (orientation> 70&&orientation<135) {activity.setrequestedorientation (8);} else if (orientation>135&&orientation<225) {//activity.setrequestedorientation (9);} else if (orientation>225&&orientation<290) {activity.setrequestedorientation (0);} else if ((orientation>315&&orientation<360) | | | (orientation>0&&orientation<45)) {//activity.setrequestedorientation (1);}} Super.handlemessage (msg);}        }

Add outside OnCreate in cocos2dxactivity

Private Handler Handler;    Private Orientationsensorlistener listener;private Sensormanager sm;private sensor sensor;

Add inside the OnCreate function

Handler = new Changeorientationhandler (this);     SM = (Sensormanager) getsystemservice (context.sensor_service);    Sensor = Sm.getdefaultsensor (sensor.type_accelerometer);           Listener = new Orientationsensorlistener (handler);    Sm.registerlistener (listener, sensor, SENSORMANAGER.SENSOR_DELAY_UI);

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

COCOS2DX 2.x Android gravity detection rotating screen

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: 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.