Libgdx’s TexturePacker and NinePatches

Libgdx’s texture packer works great with packing ninepatches, but actually using the ninepatches took some figuring out. Hopefully the following information on how to use the packed ninepatches will save you some trouble.

  1. Once you have your ninepatch asset make sure you save it with a .9 before the extension (i.e. myImage.9.png)
  2. TexturePacker will detect the .9 extension when it packs the image and will remove the Ninepatch border and instead put the Ninepatch split information in the atlas file
  3. When you want to use the NinePatch in your code, instead of using TextureAtlas.findRegion(String region) you use:

One thought on “Libgdx’s TexturePacker and NinePatches

  1. Great. I was googling for a simple example that worked for me without skins, scene2d,etc. For me, to draw it I needed to add batch, like this

    ninepatch.draw(batch,20,300,150,200);
    inside of my public void render ()
    Otherwise android studio said
    error: no suitable method found for draw(int,int,int,int)
    ninepatch.draw(20,300,180,180);
    ^
    method NinePatch.draw(Batch,float,float,float,float) is not applicable
    This was using Libgdx 1.9.6 released March 2017
    Still, much easier to get my head around than reading the API.

    For anyone else, after drawing the ninepatch, I recommend
    https://romannurik.github.io/AndroidAssetStudio/nine-patches.html#&sourceDensity=320&name=example
    for making into 9patch format
    Then import that with other images into atlas using
    https://github.com/crashinvaders/gdx-texture-packer-gui
    which you download as a libgdx project and build into a desktop JAR from android studio

    Thanks for taking time to put a post, saved me hours of searching i think.

Leave a Reply

Your email address will not be published. Required fields are marked *