LibGDX - 固定比例缩放图像

4

我的LibGDX游戏有一个带有标志和一些按钮的启动屏幕。我无法弄清楚如何按比例缩放标志以避免扭曲。

以下是代码:

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();
}
2个回答

6

我发现你可以将此功能用于图片 - 它可以正常工作!

imageLogo.setScaling(Scaling.fit);

-2

您可以按如下方式缩放图像:

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);

谢谢您的回答,但是在我的问题中它没有起作用。 - mbo
不考虑实际宽高比以及第二次调用setHeight的错误——应该是setWidth。 - RichieHH
@Richie,谢谢。我已经纠正了打字错误。你说的“考虑实际宽高比”的意思是什么?我已经成功地在我的代码中应用了这种缩放方法。 - Gio
我被您最初的 typo 误导了,现在已经更正过了。 - RichieHH

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接