mbo
mbo

Reputation: 4701

LibGDX - Scale Image with fixed ratio

My LibGDX game has a start screen with a logo and some buttons. I can't figure out how to scale the logo with fixed ratio to not distort it.

That's the code:

public StartScreen(int lastScore, InputListener inputListener) {
    super();
    this.listener = inputListener;
    stage = new Stage();
    Gdx.input.setInputProcessor(stage);

    Table table = new Table();
    table.setFillParent(true);
    table.setBackground(new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("background.png")))));

    Image imageLogo = new Image();
    imageLogo.setDrawable(new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("title.png")))));

    // Here's the problem, the image is not scaled correctly...
    table.add(imageLogo).center().colspan(2);
    table.row();

    // Some Buttons
    table.add(button1).colspan(2).padBottom(30);
    table.row();
    table.add(button2).padBottom(30);
    table.add(button3).padBottom(30);
    table.row();
    table.add(button4).colspan(2);

    stage.addActor(table);

    //table.debug();
}

Upvotes: 4

Views: 4785

Answers (2)

Gio
Gio

Reputation: 3340

You can scale your image as follows:

Image imageLogo = new Image();
imageLogo.setDrawable(new TextureRegionDrawable(
        new TextureRegion(new Texture(Gdx.files.internal("title.png")))));

// 0.9f scales to 90% of the original height and width
final float heightScaleFactor = 0.9f;
final float widthScaleFactor = 0.9f;

imageLogo.setHeight(imageLogo.getHeight() * heightScaleFactor);
imageLogo.setWidth(imageLogo.getWidth() * widthScaleFactor);

Upvotes: -2

mbo
mbo

Reputation: 4701

I found out that you can use this with images - works fine!

imageLogo.setScaling(Scaling.fit);

Upvotes: 6

Related Questions