Android LibGDX-具有固定比例的缩放图像
我的LibGDX游戏有一个带有徽标和一些按钮的开始屏幕。我不知道如何以固定的比例缩放徽标,使其不失真 这就是代码:Android LibGDX-具有固定比例的缩放图像,android,layout,libgdx,Android,Layout,Libgdx,我的LibGDX游戏有一个带有徽标和一些按钮的开始屏幕。我不知道如何以固定的比例缩放徽标,使其不失真 这就是代码: public StartScreen(int lastScore, InputListener inputListener) { super(); this.listener = inputListener; stage = new Stage(); Gdx.input.setInputProcessor(stage); Table tab
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();
}
您可以按如下方式缩放图像:
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);
我发现,你可以使用这个图像-工程罚款
imageLogo.setScaling(Scaling.fit);
谢谢您的回答,但在我的问题中它不起作用。考虑到实际的纵横比以及第二次调用setHeight的错误,它应该是setWidth。@Richie,Thx。我已经改正了错误。“考虑实际纵横比”是什么意思?我在代码中成功地应用了这种缩放方法。我被你最初的打字错误误导了。现在更正。