[英]Full screen background regardless of screen size and keep image aspect ratio
I have a 540x720 background image.我有一个 540x720 的背景图片。
I need to show it in full screen regardless of screen size and resolution (My game will run on desktop and on Android) and regardless of portrait/landscape mode.无论屏幕大小和分辨率如何(我的游戏将在桌面和 Android 上运行),无论纵向/横向模式如何,我都需要以全屏方式显示它。
I want to keep the aspect ratio of the image, if necessary by cropping the image.如有必要,我想通过裁剪图像来保持图像的纵横比。
I have already tried ScreenViewport and ExtendViewport but when resizing the window, the background is no longer full screen (for example if the window becomes horizontal) and letter-boxing occurs (showing white side bars).我已经尝试过 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);
}
How to achieve that?如何做到这一点?
Thanks谢谢
Use FillViewport
which will maintain aspect ratio and be full screen by cropping part of the image.使用FillViewport
,它将通过裁剪图像的一部分来保持纵横比并全屏显示。
See here https://gamefromscratch.com/libgdx-tutorial-part-17-viewports/请参阅此处https://gamefromscratch.com/libgdx-tutorial-part-17-viewports/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.