FitViewport 中的 LibGDX 背景图像

2022-01-12 00:00:00 android java libgdx

所以,我正在为即将推出的应用程序使用 LibGDX.

So, I'm using LibGDX for my upcoming App.

我使用 FitViewport 来确保 16:9 的纵横比.因此,除 16:9 以外的其他宽高比的播放器将在站点出现黑条.

I use a FitViewport to ensure the 16:9 aspect ratio. So players with other aspect ratios than 16:9 will have black bars at sites.

最好的方法是绘制一个屏幕填充背景图像,它也覆盖了黑条所在的区域?

    camera = new OrthographicCamera();
    viewport = new FitViewport(WIDTH, HEIGHT, camera);
    viewport.apply();
    camera.position.set(WIDTH / 2, HEIGHT / 2, 0);
    camera.update();

这就是我目前设置相机/视口的方式.

Thats how I set up my camera/viewport currently.

然后我用 SpriteBatch 在上面画东西.

I then draw stuff on it with a SpriteBatch.

Gdx.gl.glClearColor(1, 1, 1, 1);

这就是我目前至少将黑条的颜色更改为任何 RGB 颜色的方式.

That's how I currently at least change the color of the black bars, to any RGB color.

推荐答案

在我看来,最好的办法是创建第二个阶段,它自己的 Viewport 仅用于后台用途.第二个 Viewport 不应该是 FillViewport - 它会根据我的经验拉伸您的图形.我认为 ExtendViewport 在这种情况下会更好.

In my opinion, the best idea is to create a second stage and it's own Viewport only for background purposes. This second Viewport should not be FillViewport - it will strech your graphics from my experience. I think ExtendViewport is better in this case.

那么它应该是什么样子:

So how should it look:

    Stage stage, backStage;
    FitViewport viewport;
    ExtendViewport backViewport;

    ...

    stage = new Stage(); //this is your normal stage you have now
    stage.setViewport( yourFitViewport ); //here you are assingning fit viewport

    backViewport = new ExtendViewport( screenWidth, screenHeight );

    backStage = new Stage();
    backStage.setViewport( backViewport ); 

    ...
    //now add to backStage your background Image

    backStage.addActor( yourBackground );

现在只需在渲染方法中处理新阶段.

Now just handle new stage in the render method.

    backStage.act();
    stage.act();

    backStage.draw(); //backStage first - we want it under stage
    stage.draw();

并在更新中更新新的 Viewport 或像旧的一样渲染.就是这样.

And update new Viewport in update or render like your old one. That's all.

在此处阅读有关 Viewports 的更多信息:https://github.com/libgdx/libgdx/wiki/Viewports

Read more about Viewports here: https://github.com/libgdx/libgdx/wiki/Viewports

相关文章