Class NinePatch


  • public class NinePatch
    extends java.lang.Object
    A 3x3 grid of texture regions. Any of the regions may be omitted. Padding may be set as a hint on how to inset content on top of the ninepatch (by default the eight "edge" textures of the nine-patch define the padding). When drawn the eight "edge" patches will not be scaled, only the interior patch will be scaled.

    NOTE: This class expects a "post-processed" nine-patch, and not a raw ".9.png" texture. That is, the textures given to this class should not include the meta-data pixels from a ".9.png" that describe the layout of the ninepatch over the interior of the graphic. That information should be passed into the constructor either implicitly as the size of the individual patch textures, or via the left, right, top, bottom parameters to NinePatch(Texture, int, int, int, int) or NinePatch(TextureRegion, int, int, int, int).

    A correctly created TextureAtlas is one way to generate a post-processed nine-patch from a ".9.png" file.

    • Constructor Detail

      • NinePatch

        public NinePatch​(com.badlogic.gdx.graphics.Texture texture,
                         int left,
                         int right,
                         int top,
                         int bottom)
        Create a ninepatch by cutting up the given texture into nine patches. The subsequent parameters define the 4 lines that will cut the texture region into 9 pieces.
        Parameters:
        left - Pixels from left edge.
        right - Pixels from right edge.
        top - Pixels from top edge.
        bottom - Pixels from bottom edge.
      • NinePatch

        public NinePatch​(TextureRegion region,
                         int left,
                         int right,
                         int top,
                         int bottom)
        Create a ninepatch by cutting up the given texture region into nine patches. The subsequent parameters define the 4 lines that will cut the texture region into 9 pieces.
        Parameters:
        left - Pixels from left edge.
        right - Pixels from right edge.
        top - Pixels from top edge.
        bottom - Pixels from bottom edge.
      • NinePatch

        public NinePatch​(com.badlogic.gdx.graphics.Texture texture,
                         com.badlogic.gdx.graphics.Color color)
        Construct a degenerate "nine" patch with only a center component.
      • NinePatch

        public NinePatch​(com.badlogic.gdx.graphics.Texture texture)
        Construct a degenerate "nine" patch with only a center component.
      • NinePatch

        public NinePatch​(TextureRegion region,
                         com.badlogic.gdx.graphics.Color color)
        Construct a degenerate "nine" patch with only a center component.
      • NinePatch

        public NinePatch​(TextureRegion region)
        Construct a degenerate "nine" patch with only a center component.
      • NinePatch

        public NinePatch​(TextureRegion... patches)
        Construct a nine patch from the given nine texture regions. The provided patches must be consistently sized (e.g., any left edge textures must have the same width, etc). Patches may be null. Patch indices are specified via the public members TOP_LEFT, TOP_CENTER, etc.
      • NinePatch

        public NinePatch​(NinePatch ninePatch)
      • NinePatch

        public NinePatch​(NinePatch ninePatch,
                         com.badlogic.gdx.graphics.Color color)
    • Method Detail

      • draw

        public void draw​(com.badlogic.gdx.graphics.g2d.Batch batch,
                         float x,
                         float y,
                         float width,
                         float height)
      • draw

        public void draw​(com.badlogic.gdx.graphics.g2d.Batch batch,
                         float x,
                         float y,
                         float originX,
                         float originY,
                         float width,
                         float height,
                         float scaleX,
                         float scaleY,
                         float rotation)
      • setColor

        public void setColor​(com.badlogic.gdx.graphics.Color color)
        Copy given color. The color will be blended with the batch color, then combined with the texture colors at draw time. Default is Color.WHITE.
      • getColor

        public com.badlogic.gdx.graphics.Color getColor()
      • getLeftWidth

        public float getLeftWidth()
      • setLeftWidth

        public void setLeftWidth​(float leftWidth)
        Set the draw-time width of the three left edge patches
      • getRightWidth

        public float getRightWidth()
      • setRightWidth

        public void setRightWidth​(float rightWidth)
        Set the draw-time width of the three right edge patches
      • getTopHeight

        public float getTopHeight()
      • setTopHeight

        public void setTopHeight​(float topHeight)
        Set the draw-time height of the three top edge patches
      • getBottomHeight

        public float getBottomHeight()
      • setBottomHeight

        public void setBottomHeight​(float bottomHeight)
        Set the draw-time height of the three bottom edge patches
      • getMiddleWidth

        public float getMiddleWidth()
      • setMiddleWidth

        public void setMiddleWidth​(float middleWidth)
        Set the width of the middle column of the patch. At render time, this is implicitly the requested render-width of the entire nine patch, minus the left and right width. This value is only used for computing the default total width.
      • getMiddleHeight

        public float getMiddleHeight()
      • setMiddleHeight

        public void setMiddleHeight​(float middleHeight)
        Set the height of the middle row of the patch. At render time, this is implicitly the requested render-height of the entire nine patch, minus the top and bottom height. This value is only used for computing the default total height.
      • getTotalWidth

        public float getTotalWidth()
      • getTotalHeight

        public float getTotalHeight()
      • setPadding

        public void setPadding​(float left,
                               float right,
                               float top,
                               float bottom)
        Set the padding for content inside this ninepatch. By default the padding is set to match the exterior of the ninepatch, so the content should fit exactly within the middle patch.
      • getPadLeft

        public float getPadLeft()
        Returns the left padding if set, else returns getLeftWidth().
      • getPadRight

        public float getPadRight()
        Returns the right padding if set, else returns getRightWidth().
      • getPadTop

        public float getPadTop()
        Returns the top padding if set, else returns getTopHeight().
      • getPadBottom

        public float getPadBottom()
        Returns the bottom padding if set, else returns getBottomHeight().
      • scale

        public void scale​(float scaleX,
                          float scaleY)
        Multiplies the top/left/bottom/right sizes and padding by the specified amount.
      • getTexture

        public com.badlogic.gdx.graphics.Texture getTexture()