Search code examples
opengllibgdxtexturesrounded-corners

LibGDX - Rounded shaped texture


I was wondering if it possible to give a shape to a Texture in LibGDX.

In particular, I have a Texture and I want to make a button out of it. To do so, I wanted to give it a rounded corners shape.

In a nutshell, I have this :

enter image description here

and I want this :

enter image description here

I've already read some similar questions with any clear answer. Does anyone have experience this problem and found a smart solution ?


Solution

  • Create rounded texture image using bellow method and then add text to it.

    public static Texture createPixmapRoundCornerRect(Color color, int width,
                int height, int radius) {
            Pixmap pixmap = new Pixmap(width, height, Format.RGBA8888);
            pixmap.setColor(color);
            pixmap.fillCircle(radius, radius, radius);
            pixmap.fillCircle(width - radius, radius, radius);
            pixmap.fillCircle(width - radius, height - radius, radius);
            pixmap.fillCircle(radius, height - radius, radius);
            pixmap.fillRectangle(0, radius, width, height - (radius * 2));
    
            pixmap.fillRectangle(radius, 0, width - (radius * 2), height);
            Texture pixmaptex = new Texture(pixmap);
            pixmap.dispose();
            return pixmaptex;
        }