Java 如何使 ProgressBar 在 LibGDX 中工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24434570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I make a ProgressBar work in LibGDX?
提问by adrianoubk
I'm trying to understand how to use a ProgressBar in LibGDX.
我试图了解如何在 LibGDX 中使用 ProgressBar。
I have created the bar but I don't know how to make it works. I want to duplicate the knob in order to fill the bar(background line) in 60 seconds. I know how to manage about the time, but there is no method in ProgressBar's class to fill the bar with the knob. At least, I haven't seen it (or I don't understand how, possibly). Here is my code:
我已经创建了酒吧,但我不知道如何使它工作。我想复制旋钮以在 60 秒内填充条形(背景线)。我知道如何管理时间,但是 ProgressBar 的类中没有方法可以用旋钮填充栏。至少,我还没有看到它(或者我不明白,可能)。这是我的代码:
ProgressBar's code:
ProgressBar 的代码:
skin = new Skin();
Pixmap pixmap = new Pixmap(10, 10, Format.RGBA8888);
pixmap.setColor(Color.WHITE);
pixmap.fill();
skin.add("white", new Texture(pixmap));
textureBar = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("barGreen_horizontalMid.png"))));
barStyle = new ProgressBarStyle(skin.newDrawable("white", Color.DARK_GRAY), textureBar);
bar = new ProgressBar(0, 10, 0.5f, false, barStyle);
bar.setPosition(10, 10);
bar.setSize(290, bar.getPrefHeight());
bar.setAnimateDuration(2);
stage.addActor(bar);
I know that I can move the knob with the method setValue(float)
. But what I want is to fill bar with the knob's texture. Here is a bar's screenshot and the knob.
我知道我可以使用方法移动旋钮setValue(float)
。但我想要的是用旋钮的纹理填充栏。这是一个酒吧的屏幕截图和旋钮。
Can anyone help me to understand this? Thanks in advance.
任何人都可以帮助我理解这一点吗?提前致谢。
回答by AcarX
Try to use setValue( float value) function. Also set stepsize and min/max values before that.
尝试使用 setValue(float value) 函数。还要在此之前设置步长和最小/最大值。
回答by user3920617
I was having the same problem, and finally found out.
我也遇到了同样的问题,终于知道了。
You have to set a style for the knobBeforeattribute to achieve what you want. Try this:
您必须为KnobBefore属性设置样式才能实现您想要的效果。尝试这个:
barStyle = new ProgressBarStyle(skin.newDrawable("white", Color.DARK_GRAY), textureBar);
barStyle.knobBefore = barStyle.knob;
bar = new ProgressBar(0, 10, 0.5f, false, barStyle);
Hope this helps!
希望这可以帮助!
回答by Sheraz Ahmad Khilji
When your using Screens, you don't have the built in ProgressBaravailable so instead you can use a ShapeRendererfor drawing your ProgressBar like this:
当您使用屏幕时,您没有可用的内置ProgressBar,因此您可以使用ShapeRenderer来绘制您的 ProgressBar,如下所示:
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.ui.ProgressBar;
import com.badlogic.gdx.scenes.scene2d.ui.ProgressBar.ProgressBarStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.TimeUtils;
public class LoadingScreen implements Screen {
private MyGame mGame;
private BitmapFont bf_loadProgress;
private long progress = 0;
private long startTime = 0;
private ShapeRenderer mShapeRenderer;
private OrthographicCamera camera;
private final int screenWidth = 800, screenHeight = 480;
public LoadingScreen(Game game) {
mGame = (MyGame) game;
bf_loadProgress = new BitmapFont();
bf_loadProgress.setScale(2, 1);
mShapeRenderer = new ShapeRenderer();
startTime = TimeUtils.nanoTime();
initCamera();
}
private void initCamera() {
camera = new OrthographicCamera();
camera.setToOrtho(false, screenWidth, screenHeight);
camera.update();
}
@Override
public void show() {
// TODO Auto-generated method stub
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
showLoadProgress();
}
/**
* Show progress that updates after every half second "0.5 sec"
*/
private void showLoadProgress() {
long currentTimeStamp = TimeUtils.nanoTime();
if (currentTimeStamp - startTime > TimeUtils.millisToNanos(500)) {
startTime = currentTimeStamp;
progress = progress + 10;
}
// Width of progress bar on screen relevant to Screen width
float progressBarWidth = (screenWidth / 100) * progress;
mGame.getBatch().setProjectionMatrix(camera.combined);
mGame.getBatch().begin();
bf_loadProgress.draw(mGame.getBatch(), "Loading " + progress + " / " + 100, 10, 40);
mGame.getBatch().end();
mShapeRenderer.setProjectionMatrix(camera.combined);
mShapeRenderer.begin(ShapeType.Filled);
mShapeRenderer.setColor(Color.YELLOW);
mShapeRenderer.rect(0, 10, progressBarWidth, 10);
mShapeRenderer.end();
if (progress == 100)
moveToMenuScreen();
}
/**
* Move to menu screen after progress reaches 100%
*/
private void moveToMenuScreen() {
mGame.setScreen(new MenuScreen(mGame));
dispose();
}
@Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
@Override
public void hide() {
// TODO Auto-generated method stub
}
@Override
public void dispose() {
bf_loadProgress.dispose();
mShapeRenderer.dispose();
}
}
This draws a progressbar at the bottom of the screen and also show you the progress in form of text.
这会在屏幕底部绘制一个进度条,并以文本形式显示进度。
Hope this helps :)
希望这可以帮助 :)