0

目标是制作一个简单的 2d 箭头指南针应用程序,就像这里的iPhone 3D Programming 一书中的应用程序一样,它可以平滑地旋转以响应方向变化。

AndroidManifest.xml(订阅方向更改)

<activity android:name="android.app.NativeActivity"
 android:label="@string/app_name"
 android:configChanges="orientation|keyboardHidden">

AndroidApp.cpp(获取旋转角度)

void AndroidApp::handleCmd (int32_t cmd) {
switch (cmd) {
    case APP_CMD_CONFIG_CHANGED:
       // Your code here
    break;
  }
}

RenderingEngine.cpp(存储角度以在渲染场景时旋转箭头)

    void RenderingEngine::OnRotate(DeviceOrientation orientation)
    {
       float angle = 0;

       switch (orientation) {
          case DeviceOrientationLandscapeLeft:
             angle = 270;
             break;

          case DeviceOrientationPortraitUpsideDown:
             angle = 180;
             break;

          case DeviceOrientationLandscapeRight:
             angle = 90;
             break;
       }

       m_desiredAngle = angle;
    }
4

1 回答 1

0

If you want to make the rotation smooth you should use compass instead of orientation change event.

I suggest checking out the documentation for NativeActivity. There is an example with accelerometer:

// Prepare to monitor accelerometer
engine.sensorManager = ASensorManager_getInstance();
engine.accelerometerSensor = ASensorManager_getDefaultSensor(engine.sensorManager,
        ASENSOR_TYPE_ACCELEROMETER);
engine.sensorEventQueue = ASensorManager_createEventQueue(engine.sensorManager,
        state->looper, LOOPER_ID_USER, NULL, NULL);

Probably there is more info in Android NDK documentation which you can find in <ndk>/docs directory (source).

于 2012-05-21T18:45:33.093 回答