微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

全屏背景与屏幕大小无关并保持图像纵横比

如何解决全屏背景与屏幕大小无关并保持图像纵横比

我有一张 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);
}

如何实现?

谢谢

解决方法

使用 FillViewport 将通过裁剪图像的一部分来保持纵横比和全屏。

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。