LibGDX Background Image in FitViewport Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of LibGDX Background Image in FitViewport without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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

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.

What is the best way, to draw a screen filling background image, that also covers the area, where the black bars would be?

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

Thats how I set up my camera/viewport currently.

I then draw stuff on it with a SpriteBatch., 1, 1, 1);

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


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.draw(); //backStage first - we want it under stage

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

Read more about Viewports here:

We are here to answer your question about LibGDX Background Image in FitViewport - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji