1

我真的被屏幕方向逻辑困住了。

这是我的代码:

@Override
public EngineOptions onCreateEngineOptions() {
        this.cameraWidth = getResources().getDisplayMetrics().widthPixels;
        this.cameraHeight = getResources().getDisplayMetrics().heightPixels;
        this.camera = CameraFactory.createPixelPerfectCamera(this, this.cameraWidth / 2.0F, this.cameraHeight / 2.0F);
        this.camera.setResizeOnSurfaceSizeChanged(true);
        this.dpi = getResources().getDisplayMetrics().densityDpi;

        Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
        int rotation = display.getRotation();
        if (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) {
                screenOrientation = ScreenOrientation.LANDSCAPE_SENSOR;
        } else {
                screenOrientation = ScreenOrientation.PORTRAIT_SENSOR;
        }

        EngineOptions engineOptions = new EngineOptions(true,screenOrientation, new FillResolutionPolicy(), this.camera);
        engineOptions.getAudioOptions().setNeedsSound(true);
        return engineOptions;
}

@Override
public void onSurfaceChanged(final GLState pGLState, final int pWidth, final int pHeight) {
        super.onSurfaceChanged(pGLState, pWidth, pHeight);

        Log.i(TAG, "onSurfaceChanged " + "w: " + this.camera.getSurfaceWidth() + " h: " + this.camera.getSurfaceHeight());

        this.cameraWidth = this.camera.getSurfaceWidth();
        this.cameraHeight = this.camera.getSurfaceHeight();
        this.camera.setCenter(this.cameraWidth / 2.0F, this.cameraHeight / 2.0F);

}

当我在 AVD 3.7 FWVGA 滑块 480x854 上尝试我的 LWP 时,一切正常,但仅在 LWP 预览模式下。例如,当从横向 LWP 预览模式中按下“设置壁纸”按钮时,我的 LWP 移动到桌面的另一半时,我得到了半个黑屏。

另外,我注意到当我们从 Previos 模式返回到桌面时,不会调用 onCreateEngineOptions 方法。

此外,每次我在 LWP 中正确接收到 onSurfaceChanged 事件时。另外,我已经配置并且可以处理屏幕方向更改事件......但是如何将它应用于我的逻辑?

public BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent myIntent) {

        if (myIntent.getAction().equals(BROADCAST_CONFIGURATION_CHANGED)) {

            Log.d(TAG, "received->" + BROADCAST_CONFIGURATION_CHANGED);

            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
                Log.i(TAG, "LANDSCAPE_SENSOR");
            } else {
                Log.i(TAG, "PORTRAIT_SENSOR");
            }
        }
    }
}

如何正确设置 LWP 以处理纵向和横向两种模式?

提前致谢!

4

1 回答 1

1

我在游戏中遇到了类似的问题,我在清单文件中的每个活动中修复了这一行的问题:

  <activity
        ....
        android:configChanges="orientation"
        ... />

并使用以下方法:

        @Override
public void onResumeGame() {
super.onResumeGame();

}

@Override
public void onPauseGame() {
super.onPauseGame();

}

希望能解决你的问题,最好的问候。

于 2012-12-19T03:08:39.370 回答