Cocos2dx 2.x 安卓重力检测 旋转屏幕

前端之家收集整理的这篇文章主要介绍了Cocos2dx 2.x 安卓重力检测 旋转屏幕前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在充电的情况下,玩手机的时候,屏幕一般需要特定的旋转方向。

功能实现分成两部分:一部分根据手机重力方向X,Y,Z得出所需要的角度;另一方面根据旋转角度,设置屏幕旋转方向。

通过监听手机相对于X,Z方向的值,算出绕着某一轴的角度。X,Y方向分别平行于手机界面,Z垂直于手机界面。

本文以绕Y轴旋转为例,如需绕Z轴只需把Y和Z互换,一般情况下只会要求这两种情况。

代码借鉴网上的。


  1. package com.gamemaster.orientation;
  2.  
  3. import android.hardware.Sensor;
  4. import android.hardware.SensorEvent;
  5. import android.hardware.SensorEventListener;
  6. import android.os.Handler;
  7.  
  8. public class OrientationSensorListener implements SensorEventListener {
  9. private static final int _DATA_X = 0;
  10. private static final int _DATA_Y = 1;
  11. private static final int _DATA_Z = 2;
  12. public static final int ORIENTATION_UNKNOWN = -1;
  13. private Handler rotateHandler;
  14. public OrientationSensorListener(Handler handler) {
  15. rotateHandler = handler;
  16. }
  17.  
  18. public void onAccuracyChanged(Sensor arg0,int arg1) {
  19. // TODO Auto-generated method stub
  20.  
  21. }
  22.  
  23. public void onSensorChanged(SensorEvent event) {
  24. float[] values = event.values;
  25. int orientation = ORIENTATION_UNKNOWN;
  26. float X = -values[_DATA_X];
  27. float Y = -values[_DATA_Y];
  28. float Z = -values[_DATA_Z];
  29. float magnitude = X*X + Z*Z;
  30. // Don't trust the angle if the magnitude is small compared to the y value
  31. if (magnitude * 4 >= Y*Y) {
  32. float OneEightyOverPi = 57.29577957855f;
  33. float angle = (float)Math.atan2(-Z,X) * OneEightyOverPi;
  34. orientation = 90 - (int)Math.round(angle);
  35. // normalize to 0 - 359 range
  36. while (orientation >= 360) {
  37. orientation -= 360;
  38. }
  39. while (orientation < 0) {
  40. orientation += 360;
  41. }
  42. }
  43. if (rotateHandler!=null) {
  44. rotateHandler.obtainMessage(888,orientation,0).sendToTarget();
  45. }
  46.  
  47. }
  48.  
  49. }

第二部分 根据旋转角度,设置屏幕方向。
  1. package com.gamemaster.orientation;
  2.  
  3. import android.app.Activity;
  4. import android.os.Handler;
  5. import android.os.Message;
  6. import android.util.Log;
  7.  
  8. public class ChangeOrientationHandler extends Handler {
  9.  
  10. private Activity activity;
  11.  
  12. public ChangeOrientationHandler(Activity ac) {
  13. super();
  14. activity = ac;
  15. }
  16. @Override
  17. public void handleMessage(Message msg) {
  18. if (msg.what==888) {
  19. int orientation = msg.arg1;
  20. if (orientation>70&&orientation<135) {
  21. activity.setRequestedOrientation(8);
  22. }else if (orientation>135&&orientation<225){
  23. //activity.setRequestedOrientation(9);
  24. }else if (orientation>225&&orientation<290){
  25. activity.setRequestedOrientation(0);
  26. }else if ((orientation>315&&orientation<360)||(orientation>0&&orientation<45)){
  27. //activity.setRequestedOrientation(1);
  28. }
  29. }
  30. super.handleMessage(msg);
  31. }
  32. }

在Cocos2dxActivity中的onCreate外面添加

  1. private Handler handler;
  2. private OrientationSensorListener listener;
  3. private SensorManager sm;
  4. private Sensor sensor;

在onCreate函数里面添加
  1. handler = new ChangeOrientationHandler(this);
  2. sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
  3. sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  4. listener = new OrientationSensorListener(handler);
  5. sm.registerListener(listener,sensor,SensorManager.SENSOR_DELAY_UI);

猜你在找的Cocos2d-x相关文章