public class SensorRotation { private SensorManager sensorManager; private long lastUpdateTime; private int orientation = 1; private SensorEventListener sensorListener; private Activity activity; private boolean rightPosition = false; public SensorRotation instance; /** * * * @param context */ public SensorRotation(Context context) { sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); lastUpdateTime = 0; } /** * * * @param activity * @param listener */ public void register(Activity activity, SensorEventListener listener) { try { if (sensorListener != null) { sensorManager.unregisterListener(sensorListener); } this.sensorListener = listener; this.activity = activity; sensorManager.registerListener(sensorListener, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_GAME); Log.v("Activity = " + activity.getLocalClassName()); } catch (Exception e) { e.printStackTrace(); } } /** * */ public void unregister() { sensorManager.unregisterListener(sensorListener); Log.v("unregister"); } private boolean in(float value, int min, int max) { return (value > min && value < max); } private int getCurrentOrientation(SensorEvent event) { float y = event.values[1]; float z = event.values[2]; // . . if (in(z, 60, 100)) return 1; if (in(z, -20, 20) && y > 50) return 2; if (in(z, -20, 20) && y < -50) return 0; if (in(z, -100, -60)) return 3; return -1; } /** * * * @param event */ public boolean onSensorChanged(SensorEvent event) { if (System.currentTimeMillis() - lastUpdateTime < 500) return false; lastUpdateTime = System.currentTimeMillis(); orientation = getCurrentOrientation(event); if (activity.getClass() == Guitar.class) if (orientation == 0) rightPosition = true; if (activity.getClass() == Sing.class) if (orientation == 1) rightPosition = true; if (activity.getClass() == Drums.class) if (orientation == 2) rightPosition = true; if (activity.getClass() == Piano.class) if (orientation == 3) rightPosition = true; if (!rightPosition) return false; Intent intent = null; switch (orientation) { case 0: { Log.v("Orientation = " + orientation + ", launching Guitar"); intent = new Intent(activity, Guitar.class); break; } case 1: { Log.v("Orientation = " + orientation + ", launching Sing"); intent = new Intent(activity, Sing.class); break; } case 2: { Log.v("Orientation = " + orientation + ", launching Drums"); intent = new Intent(activity, Drums.class); break; } case 3: { Log.v("Orientation = " + orientation + ", launching Piano"); intent = new Intent(activity, Piano.class); break; } } if (intent != null) { activity.startActivity(intent); activity.finish(); return true; } return false; } }
次に、ゲームアクティビティの残りが継承されるアクティビティを作成します。 public abstract class TalentGameActivity extends BaseGameActivity implements SensorEventListener { @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } @Override public void onSensorChanged(SensorEvent event) { if (GameModel.getInstance().sensorRotation.onSensorChanged(event)) { // } } @Override public void onLoadComplete() { .... // instance SensorRotation.instance.sensorRotation.register(this, this); ..... } }
次に、すべてのゲームアクティビティを作成します。 180度逆さまにしなければならないアクティビティは、作成プロセス中に、以下で説明するようにカメラを180度逆さまにする必要があります。 public class Drums extends TalentGameActivity { @Override public Engine onLoadEngine() { super.onLoadEngine(); mCamera = new Camera(0, 0, Const.ENGINE_PORTRAIT_WIDTH, Const.ENGINE_PORTRAIT_HEIGHT); mCamera.setRotation(180); // 180 . mEngine = new Engine(new EngineOptions(true, ScreenOrientation.PORTRAIT, new FillResolutionPolicy(), mCamera)); // ScreenOrientation - //mEngine = new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new FillResolutionPolicy(), mCamera)); super.onLoadEngine(); return mEngine; } }
以上です。