红色正方形是按钮的边界,而图像保持在32x32像素的中心位置。我尝试使用button.getImage()将位置和大小设置为按钮的值,但似乎没有任何效果。
红色正方形是按钮的边界,而图像保持在32x32像素的中心位置。我尝试使用button.getImage()将位置和大小设置为按钮的值,但似乎没有任何效果。
Button
,它也包含一个Drawable
作为其背景,但该背景始终被拉伸以填充按钮。请参见 ImageButton
的JavaDoc:
......如果图像的大小与按钮相同,则可以使用没有任何子元素的按钮,其中
Button.ButtonStyle.up
、Button.ButtonStyle.down
和Button.ButtonStyle.checked
九宫格定义了图像。
请注意,这些实际上不需要是九宫格;任何Drawable
都可以。
可能有点晚了,但你尝试过以下方法吗:
yourButton.getImage().setFillParent(true);
在 @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);