【问题标题】:Libgdx | Scene2d | Set background color of table?Libgdx |场景2d |设置表格的背景颜色?
【发布时间】:2016-12-29 03:36:12
【问题描述】:

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

【问题讨论】:

  • 您应该在纹理图集中保留一个纯白色纹理区域,该区域可用于创建纯色 TextureRegionDrawables。将白色 TextureRegionDrawable 设置为表格的背景。

标签: java android libgdx box2d scene2d


【解决方案1】:

你可以这样做:

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);

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

【讨论】:

    【解决方案2】:

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

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

    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);
    

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

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

    现在使用实例的 setColor(red, green, blue, alpha) 方法,然后使用 setBackground(Drawable drawable) 方法将实例传递给 libGDX 表。

    这并不意味着对所有人都是完美的解决方案——根据需要进行修改。

    备份:

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 来吧。你必须提出解决方案。
      【解决方案4】:

      对于那些要求提供示例代码的人,这里有一个简单的实现。 (我刚刚发现了BaseDrawable,事实证明它非常适合这种情况!)

      
       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);
          }
      }
      

      像这样使用它:

      
          // 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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-25
        • 2019-08-30
        • 2012-06-06
        • 2019-10-21
        • 2010-11-21
        相关资源
        最近更新 更多