bvv
bvv

Reputation: 1903

Why NinePatch dont work in LibGDX?

I have a backround for message item, I want to make it stretched depending on the text. But when i make this:

    Drawable drawableButtonMessageUp = new TextureRegionDrawable(new TextureRegion(new Texture("data/message_my_g.9.png")));
    ButtonStyle buttonStyleMessage = new ButtonStyle();
    buttonStyleMessage.up = drawableButtonMessageUp;
    Button buttonMessage = new Button(buttonStyleMessage);
    Label labelMessage = new Label("", new LabelStyle(game.fonts[0], new Color(0, 0, 0, 1)));
    buttonMessage.add(labelMessage);  

Background does not NinePath, it strached like a normal picture: My result: enter image description here

I want (example): enter image description here

Backround for item: enter image description here

I checked the nine patch picture in Android SDK, and this work good, how i can do this in libGDX?

Upvotes: 0

Views: 437

Answers (1)

clearlyspam23
clearlyspam23

Reputation: 1694

It's because you're not using a NinePatch, you're using a TextureRegion

you probably are going to want to change the line

Drawable drawableButtonMessageUp = new TextureRegionDrawable(new TextureRegion(new Texture("data/message_my_g.9.png")));

to something like

Drawable drawableButtonMessageUp = new NinePatchDrawable(new NinePatch(new Texture("data/message_my_g.9.png"), 1, 1, 1, 1));

Note that the number "1" here is a placeholder. You may need to play around with those numbers to get the ninepatch to look right.

For more information, please take a look at the documentation, particularly the section on instantiating ninepatches

Upvotes: 2

Related Questions