fix orientation-change can not work normally (#16774)

* fix orientation-change can not work normally
This commit is contained in:
bofeng-song
2024-03-22 09:27:54 +08:00
committed by GitHub
parent 559bfc9467
commit 5da2f9b63a
9 changed files with 167 additions and 30 deletions
@@ -51,6 +51,8 @@
#include "base/StringUtil.h"
#include "engine/EngineEvents.h"
#include "paddleboat.h"
#include "platform/java/jni/JniImp.h"
#include "platform/interfaces/modules/Device.h"
#define ABORT_GAME \
{ \
@@ -531,6 +533,7 @@ public:
if (!_launched) {
_launched = true;
_androidPlatform->_rotation = getDeviceRotationJNI();
ISystemWindowInfo info;
info.width = ANativeWindow_getWidth(nativeWindow);
info.height = ANativeWindow_getHeight(nativeWindow);
@@ -626,13 +629,35 @@ public:
events::WindowEvent::broadcast(ev);
break;
}
case APP_CMD_CONFIG_CHANGED:
case APP_CMD_CONFIG_CHANGED: {
CC_LOG_INFO("AndroidPlatform: APP_CMD_CONFIG_CHANGED");
int rotation = getDeviceRotationJNI();
if (_androidPlatform->_rotation != rotation) {
CC_LOG_INFO("AndroidPlatform: orientation-change");
_androidPlatform->_rotation = rotation;
int orientation = static_cast<int>(Device::Orientation::PORTRAIT);
switch (rotation) {
case 0: // ROTATION_0
orientation = static_cast<int>(Device::Orientation::PORTRAIT);
break;
case 1: // ROTATION_90
orientation = static_cast<int>(cc::Device::Orientation::LANDSCAPE_RIGHT);
break;
case 2: // ROTATION_180
orientation = static_cast<int>(cc::Device::Orientation::PORTRAIT_UPSIDE_DOWN);
break;
case 3: // ROTATION_270
orientation = static_cast<int>(cc::Device::Orientation::LANDSCAPE_LEFT);
break;
}
cc::events::Orientation::broadcast(orientation);
}
// Window was resized or some other configuration changed.
// Note: we don't handle this event because we check the surface dimensions
// every frame, so that's how we know it was resized. If you are NOT doing that,
// then you need to handle this event!
break;
}
case APP_CMD_LOW_MEMORY: {
// system told us we have low memory. So if we are not visible, let's
// cooperate by deallocating all of our graphic resources.
@@ -68,5 +68,6 @@ private:
GameInputProxy *_inputProxy{nullptr};
android_app *_app{nullptr};
friend class GameInputProxy;
int _rotation{-1};
};
} // namespace cc
@@ -24,6 +24,7 @@
package com.cocos.lib;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.media.AudioManager;
@@ -45,6 +46,7 @@ import java.util.List;
public class CocosActivity extends GameActivity {
private static final String TAG = "CocosActivity";
private static final int INITIAL_ROTATION = -1;
private CocosWebViewHelper mWebViewHelper = null;
private CocosVideoHelper mVideoHelper = null;
@@ -52,6 +54,8 @@ public class CocosActivity extends GameActivity {
private List<CocosSurfaceView> mSurfaceViewArray;
private FrameLayout mRootLayout;
private int mRotation = INITIAL_ROTATION;
private native void onCreateNative();
@@ -172,6 +176,18 @@ public class CocosActivity extends GameActivity {
surfaceView.setVisibility(View.VISIBLE);
}
}
if (mRotation == INITIAL_ROTATION
&& getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE) {
//onConfigurationChange can be triggered at the mode of 'sensor or fullSensor'. Here only handles the sensorLandscape mode.
mRotation = CocosHelper.getDeviceRotation();
mSurfaceView.addOnLayoutChangeListener((v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
int rotation = CocosHelper.getDeviceRotation();
if (mRotation != rotation) {
mRotation = rotation;
this.onConfigurationChangedNative(this.getGameActivityNativeHandle());
}
});
}
}
@Override