LibGDX ImageButton图片不按比例缩放?

4

图片截图

红色正方形是按钮的边界,而图像保持在32x32像素的中心位置。我尝试使用button.getImage()将位置和大小设置为按钮的值,但似乎没有任何效果。


不算是答案,但我发现了一些内容:在按钮样式的可绘制对象上设置 setMinWidth() 和 setMinHeight() 可以将图像设置为按钮的边界,但无法正确缩放,如此图所示(http://i.imgur.com/tmwwbvI.png)。 - oracleCreeper
面对同样的问题 - Pablo
3个回答

5
只需使用普通的按钮Button,它也包含一个Drawable作为其背景,但该背景始终被拉伸以填充按钮。请参见 ImageButton的JavaDoc

......如果图像的大小与按钮相同,则可以使用没有任何子元素的按钮,其中Button.ButtonStyle.upButton.ButtonStyle.downButton.ButtonStyle.checked九宫格定义了图像。

请注意,这些实际上不需要是九宫格;任何Drawable都可以。


1
我刚刚从ImageButton转换到Button,而且它能正常工作!谢谢。 - Fredrik Metcalf

2

可能有点晚了,但你尝试过以下方法吗:

yourButton.getImage().setFillParent(true);

2
它调整了图像大小,但是它不再居中于按钮中。 - spectacularbob
我找到了如何设置大小并写下了答案。希望这可以帮助你! - Dávid Tóth

0

在 @xitnesscomplex 的答案基础上进行扩展:

您可以使用 yourButton.getImage().setFillParent(true); 来设置大小。

设置偏移量有点反直觉。

ImageButton.ImageButtonStyle style = new ImageButton.ImageButtonStyle();
style.imageUp = new TextureRegionDrawable(new Texture(Gdx.files.internal(btn.png")));
ImageButton yourButton = new ImageButton(style);
style.unpressedOffsetX = -new_brush.getWidth()/4.0f;
style.unpressedOffsetY = -new_brush.getHeight()/4.0f;
style.pressedOffsetX = -new_brush.getWidth()/4.0f;
style.pressedOffsetY = -new_brush.getHeight()/4.0f;
style.checkedOffsetX = -new_brush.getWidth()/4.0f;
style.checkedOffsetY = -new_brush.getHeight()/4.0f;
yourButton.getImage().setFillParent(true);

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