5

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 ?

Roman Dev
  • 103
  • 2
  • 11
  • 3
    A Texture is always a rectangle, but you can put transparency in the source image (alpha channel) and draw it with blending enabled (in the SpriteBatch) to get the effect you're describing. I think this is so basic that it's taken for granted that everyone knows this, so it's never mentioned in any documentation or tutorials. – Tenfour04 Aug 31 '17 at 20:56
  • I know it of course -.- – Roman Dev Aug 31 '17 at 22:12
  • I was wondering if I could do this dynamically ! Because I need the rectangular Image for other users but I need it rounded to for the menu – Roman Dev Aug 31 '17 at 22:12
  • So a [9-patch](https://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch)? – genpfault Aug 31 '17 at 22:14
  • You can draw it with blending disabled to draw the full rectangle. – Tenfour04 Aug 31 '17 at 22:33

2 Answers2

3

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;
    }
arv
  • 250
  • 3
  • 15
0

This has been already answered here. You have to implement your own Texture which uses polygons to achieve what you are trying to do.

bitbrain
  • 603
  • 5
  • 12