4

screenshot of image

The red square is the button's boundary, while the image remains centered at 32x32px. I've tried using button.getImage() to set position and size to the button's values, but it doesn't seem to have any effect.

oracleCreeper
  • 199
  • 1
  • 3
  • 10
  • Not quite an answer, but something I found: setting setMinWidth() and setMinHeight() on the button's style's drawables sets the image to the button's boundaries, but does not scale properly, as seen [here](http://i.imgur.com/tmwwbvI.png). – oracleCreeper Mar 17 '14 at 03:24
  • Facing the same problem – Pablo Aug 11 '14 at 09:14

3 Answers3

5

Just use a regular Button. It also contains a Drawable for its background, but that one is always stretched to fill the button. See the JavaDoc for ImageButton:

... If the image is the size of the button, a Button without any children can be used, where the Button.ButtonStyle.up, Button.ButtonStyle.down, and Button.ButtonStyle.checked nine patches define the image.

Note that these don't actually need to be nine patches; any Drawable will do.

Thomas
  • 174,939
  • 50
  • 355
  • 478
2

Probably super late but have you tried:

yourButton.getImage().setFillParent(true);
Neerkoli
  • 2,423
  • 3
  • 16
  • 21
xitnesscomplex
  • 37
  • 1
  • 10
0

Extending on the answer of @xitnesscomplex: You can use yourButton.getImage().setFillParent(true); to set the size.

To set an offset is somewhat counter-intuitive

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);
Dávid Tóth
  • 2,788
  • 1
  • 21
  • 46