【问题标题】:Full screen background regardless of screen size and keep image aspect ratio不考虑屏幕大小的全屏背景并保持图像纵横比
【发布时间】: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


【解决方案1】:

使用FillViewport,它将通过裁剪部分图像保持纵横比并全屏显示。

看这里 https://gamefromscratch.com/libgdx-tutorial-part-17-viewports/

【讨论】:

    猜你喜欢
    • 2016-07-20
    • 2019-01-18
    • 1970-01-01
    • 2018-12-23
    • 2021-07-22
    • 2012-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多