Libgdx | Scene2d | Set background color of table?

2020-07-16 01:48发布

问题:

I am creating a menu (like the one is flappy bird, when you die it pops up over the play screen). I created a class that extends table, and I want to set the background of the table to white. Is there a way to do this?

回答1:

I see that the problem has already been solved, but there are others asking to see the code and I can't comment yet.

Here's an implementation of a similar solution, with the exception that a class will be made available for instantiation (so that the Table background color can be changed later easily):

https://www.snip2code.com/Snippet/2615417

BackgroundColor backgroundColor = new BackgroundColor("white_color_texture.png");
backgroundColor.setColor(2, 179, 228, 255); // r, g, b, a
table.setBackground(backgroundColor);

So, create an instance of the arbitrary BackgroundColor class (linked above) by giving the constructor the filename of a white PNG from your project resources (like what @Tenfour04 has mentioned in the comment above).

If you're not familiar with the latter part, then see the repo linked below, where an example of such PNG file can be found.

Now use the instance's setColor(red, green, blue, alpha) method, then pass the instance to the libGDX Table using the setBackground(Drawable drawable) method.

This is not meant to be a perfect solution for all — modify as needed.

Backup:

https://github.com/ronrihoo/libGDX-Table-Background-Color



回答2:

Solved the problem by using the setBackground(Drawable drawable) method for the table. I created an anonymous class of drawable, and created a sprite inside of that which is rendered in the draw method of the anonymous class.



回答3:

You can do it like this:

Pixmap bgPixmap = new Pixmap(1,1, Pixmap.Format.RGB565);
bgPixmap.setColor(Color.RED);
bgPixmap.fill();
textureRegionDrawableBg = new TextureRegionDrawable(new TextureRegion(new Texture(bgPixmap)));
Table table = new Table();
table.setBackground(textureRegionDrawableBg);

Remember to call dispose() on the texture and on the pixmap. `



回答4:

For those of you who have asked for some sample code, here's a simple implementation. (I've just discovered BaseDrawable, and it's proved to be wonderful for situations like this !)


 public static class ColorDrawable extends BaseDrawable {
    private float r, g, b, a;
    private Color savedBatchColor = new Color();

    public ColorDrawable(float r, float g, float b, float a) {
        this.r = r;
        this.g = g;
        this.b = b;
        this.a = a;
    }
    @Override
    public void draw(Batch batch, float x, float y, float width, float height) {
        // Save the batch colour as we are about to change it
        savedBatchColor.set(batch.getColor());
        batch.setColor(r, g, b, a);
        // Draw a white texture with the current batch colour
        batch.draw(Assets.blankWhite, x, y, width, height);
        batch.setColor(savedBatchColor);
    }
}

Use it like this:


    // Load a texture region from a texture atlas with a white image
    Assets.blankWhite = myTextureAtlas.findRegion("some_white_image");

    . . .

    // Create a new background drawable with the colour provided
    ColorDrawable background = new ColorDrawable(0.7f, 0.9f, 0.9f, 1f);
    table.setBackground(background);