【发布时间】:2021-07-15 09:38:36
【问题描述】:
我有一张 540x720 背景图片。
无论屏幕大小和分辨率如何(我的游戏将在桌面和 Android 上运行)以及纵向/横向模式,我都需要全屏显示。
如果需要,我想通过裁剪图像来保持图像的纵横比。
我已经尝试过 ScreenViewport 和 ExtendViewport,但是在调整窗口大小时,背景不再是全屏(例如,如果窗口变为水平状态)并且会出现信箱(显示白色边栏)。
public class MainMenuScreen implements Screen, InputProcessor {
final MyGame game;
TextureRegionDrawable textureRegionDrawableBackground = new TextureRegionDrawable(new TextureRegion(new Texture(game.backgroundFileName)));
Stage stageBackground = new Stage(new ScreenViewport());
// Stage stageBackground = new Stage(new ExtendViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight())));
Image image = new Image(textureRegionDrawableBackground);
image.setPosition(0, 0);
image.setHeight(Gdx.graphics.getHeight());
// makes the background as tall as the screen while maintaining its aspect ratio
image.setWidth(Gdx.graphics.getHeight() * textureRegionDrawableBackground.getRegion().getRegionWidth() / textureRegionDrawableBackground.getRegion().getRegionHeight());
stageBackground.addActor(image);
}
public void render(float delta) {
stageBackground.getViewport().apply();
stageBackground.draw();
}
public void resize(int width, int height) {
stageBackground.getViewport().update(width, height, true);
}
如何实现?
谢谢
【问题讨论】:
-
这怎么可能?如果屏幕的纵横比与背景的纵横比不同,您希望如何以相同的纵横比显示图像而没有两侧的空白?
标签: java android graphics libgdx fullscreen