我真的被屏幕方向逻辑困住了。
这是我的代码:
@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 slider 480x854 上尝试我的 LWP 时,一切正常,但仅在 LWP 预览模式下。例如,当我从 Landscape 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 以处理两种模式 - 纵向和横向?
提前致谢!
最佳答案
我有一个游戏有类似的问题,我在 list 文件的每个 Activity 中修复了这一行的问题:
<activity
....
android:configChanges="orientation"
... />
并使用方法:
@Override
public void onResumeGame() {
super.onResumeGame();
}
@Override
public void onPauseGame() {
super.onPauseGame();
}
希望能解决您的问题,此致。
关于java - Andengine LWP屏幕方向问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13938284/