对于那些要求提供示例代码的人,这里有一个简单的实现。 (我刚刚发现了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);