簡體   English   中英

全屏背景與屏幕大小無關並保持圖像縱橫比

[英]Full screen background regardless of screen size and keep image aspect ratio

我有一個 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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM