getGameView
Get the Game View Widget
getGameView is used to obtain the View widget that the mini-game renders at the native layer.
The mini-game's screen is presented through a custom Surface (such as TextureView or SurfaceView). With this method, you can add the game screen to your Activity layout, Fragment, or any View container just like an ordinary Android View.
Method Declaration
java
/**
* Gets the mini-game View instance.
* * @return the View object rendering the game; may return null if the environment has not been initialized.
*/
View getGameView();Core Runtime Behavior
1. Calling Timing
Usually called after create() succeeds and the GAME_STATE_WAITING state is observed. At this point, the underlying rendering surface is ready.
2. View Properties
- Type: The returned object is a standard
android.view.View, on which you can setLayoutParams, opacity, animations, etc. - Uniqueness: One
GameHandleinstance corresponds to a uniqueGameView.
3. Lifecycle Binding
When destroy() is called, this View is destroyed and removed from its parent container automatically (or must be handled manually). It cannot be reused after being destroyed.
Example Code: Adding the Game to a Layout
java
// 1. Reserve a container (such as a FrameLayout) in the layout
FrameLayout container = findViewById(R.id.game_container);
// 2. Get the View when the state changes to WAITING or RUNNING
gameHandle.setGameStateListener(new SUDRTGameHandle.GameStateChangeListener() {
@Override
public void onStateChanged(int fromState, int toState) {
if (toState == SUDRTGameHandle.GAME_STATE_WAITING) {
// Switch to the main thread to operate the UI
runOnUiThread(() -> {
View gameView = gameHandle.getGameView();
if (gameView != null && gameView.getParent() == null) {
// 3. Add the game view to the container
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
);
container.addView(gameView, lp);
}
});
}
}
});Notes
- Thread restriction: Because UI operations are involved, adding or removing the object returned by
getGameView()must be performed on the Android main thread (UI Thread). - Duplicate-addition check: Before adding the View, it is recommended to check
gameView.getParent() == nullto avoidIllegalStateException: The specified child already has a parent.caused by multiple state callbacks. - Size adjustment: The game screen usually adapts automatically to the View's width and height. If you need to force a change to the game's resolution or aspect ratio, you should generally do so through the configuration options in
setGameStartOptionsrather than merely scaling the View.
