Libgdx | Scene2d | 设置表的背景颜色?

J D*_*Doe 5 java android box2d libgdx scene2d

我正在创建一个菜单(就像那个飞扬的小鸟,当你死的时候会弹出播放屏幕).我创建了一个扩展表的类,我想将表的背景设置为白色.有没有办法做到这一点?

sim*_*nes 7

你可以这样做:

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);
Run Code Online (Sandbox Code Playgroud)

请记住在纹理和像素图上调用 dispose()。`


Ron*_*hoo 6

我看到问题已经解决,但还有其他人要求查看代码,我还不能发表评论.

这是一个类似解决方案的实现,除了一个类可用于实例化(以便以后可以轻松更改表背景颜色):

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);
Run Code Online (Sandbox Code Playgroud)

因此,通过为构造函数提供项目资源中白色PNG的文件名来创建任意BackgroundColor类的实例(上面链接)(就像上面的注释中提到的@Tenfour04所提到的那样).

如果您不熟悉后一部分,请参阅下面链接的repo,其中可以找到此类PNG文件的示例.

现在使用实例的setColor(红色,绿色,蓝色,alpha)方法,然后使用setBackground(Drawable drawable)方法将实例传递给libGDX表.

这并不是一个完美的解决方案 - 根据需要进行修改.

备份:

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


J D*_*Doe 2

通过使用表格的 setBackground(Drawable drawable) 方法解决了该问题。我创建了一个可绘制的匿名类,并在其中创建了一个精灵,该精灵在匿名类的绘制方法中呈现。