我对使用Libgdx的进度条的背景颜色有疑问。 为什么背景颜色是黑色的,即使我设置为RED或BLUE?
Here is my code:
public class HealthBar extends Actor {
private final float min = 0;
private float stepSize = 1;
private ProgressBar healthBar;
private ProgressBar.ProgressBarStyle barStyle;
private ProgressBar healthBarr;
public HealthBar(float width, float health) {
healthBar = new ProgressBar(min, health, stepSize,false , setSkin());
healthBar.setSize(width, 10);
healthBar.setPosition(getX(), getY());
healthBar.setAnimateDuration(.3f);
healthBar.setValue(1);
healthBar.setColor(Color.BLUE);
}
private ProgressBar.ProgressBarStyle setSkin() {
Color bgColor = new Color(100/256f, 100/256f,100/256f,100/256f);
Skin skin;
skin = new Skin();
Pixmap pixmap = new Pixmap(1, 10, Pixmap.Format.RGBA8888);
pixmap.fill();
skin.add("white", new Texture(pixmap));
ProgressBar.ProgressBarStyle barStyle;
barStyle = new ProgressBar.ProgressBarStyle(skin.newDrawable("white", bgColor), skin.newDrawable("white", Color.RED));
barStyle.knobAfter= barStyle.knob;
return barStyle;
}
@Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
healthBar.draw(batch, parentAlpha);
> //I think that the problem is with this draw method
}
}
当我将这个进度条作为孩子直接添加到舞台上时,根据指定的旋钮颜色绘制得很好,也许问题在于进步风格......有人能帮帮我吗?
答案 0 :(得分:1)
您可以为进度条指定透明色,因此在不同的背景上它会显示不同的颜色。我猜你的舞台背景是黑色的,这会使这种效果无效。
Color bgColor = new Color(
100 / 256f, // RED component
100 / 256f, // GREEN component
100 / 256f, // BLUE component
100 / 256f); // ALPHA component
要解决您的问题,请将alpha分量设置为1f,这意味着没有透明度(完全不透明)。
Color bgColor = new Color(
100 / 256f, // RED component
100 / 256f, // GREEN component
100 / 256f, // BLUE component
1f); // ALPHA component
答案 1 :(得分:0)
我使用上面的一些代码来弄清楚如何在libgdx中创建一个进度条,但是我遇到了与这个问题的作者相同的问题。
Color bgColor = new Color(100/256f, 100/256f,100/256f,100/256f);
Skin skin;
skin = new Skin();
Pixmap pixmap = new Pixmap(1, 10, Pixmap.Format.RGBA8888);
pixmap.setColor(bgColor); // set the color
pixmap.fill();
创建像素图时需要设置颜色,否则会使用默认值。默认情况下,您会获得“透明色”。
来自Pixmap来源:
public Pixmap (int width, int height, Format format) {
pixmap = new Gdx2DPixmap(width, height,
Format.toGdx2DPixmapFormat(format));
setColor(0, 0, 0, 0);
fill();
}
此构造函数默认设置黑色,alpha设置为0 - 完全透明。