LibGDX,Scene2d - 在表格中居中缩放元素

时间:2017-04-24 12:53:52

标签: android libgdx scene2d

我无法在Scene2d表格单元格中获得缩放的滑块以进行集中绘制。 结果目前看起来像这样。 scaled slider is off center 产生它的代码如下所示。

    table = new Table();
    table.align(Align.center | Align.top);
    add = new TextButton("+1", skin);
    remove = new TextButton(" -1", skin);
    add.getLabel().setFontScale(2.0f);
    remove.getLabel().setFontScale(2.0f);


    slider = new Slider(1, 40, 1, false, skin);
    slider.setValue(1);
    Container<Slider> sliderContainer = new Container<Slider>(slider);
    sliderContainer.setTransform(true);
    sliderContainer.setScale(2);

    table.setWidth(USER_SCREEN_SIZE_WIDTH);
    table.add(remove).pad(BUTTON_PADDING);
    table.add(sliderContainer).width(slider.getWidth() * 2);
    table.add(add).pad(BUTTON_PADDING);

    table.pack();
    table.setPosition(USER_SCREEN_SIZE_WIDTH/2-bookShoppingTable.getWidth()/2,
        bookShoppingTable.getHeight()+BOTTOM_TABLE_OFFSET);

我尝试了以下解决方案:

sliderContainer.setOrigin(sliderContainer.getWidth() / 2,
    sliderContainer.getHeight() / 2);

table.add(sliderContainer).width(slider.getWidth() * 2).center();

但他们并没有改变任何事情。

1 个答案:

答案 0 :(得分:0)

请检查,我已尝试实施您的要求。

public class TestGame extends ApplicationAdapter{

    OrthographicCamera camera;
    Stage stage;

    @Override
    public void create() {

        camera = new OrthographicCamera();
        ExtendViewport extendViewport = new ExtendViewport(400, 640, camera);

        stage = new Stage(extendViewport);

        Image image = new Image(new TextureRegionDrawable(new TextureRegion(new Texture("badlogic.jpg"))));
        image.setSize(720, 480);

        stage.setDebugAll(true);

        Skin skin=new Skin(Gdx.files.internal("skin/uiskin.json"));

        Table table=new Table();
        table.setFillParent(true);
        stage.addActor(table);

        Slider slider = new Slider(1, 40, 1, false, skin);
        slider.setValue(1);
        Container<Slider> sliderContainer = new Container<Slider>(slider);
        sliderContainer.setTransform(true);

        sliderContainer.setOrigin(slider.getWidth()/2,slider.getHeight()/2);
        sliderContainer.setSize(slider.getWidth(),slider.getHeight());
        sliderContainer.setScale(2);

        TextButton textButton=new TextButton("+1",skin);
        TextButton textButton1=new TextButton("-1",skin);

        table.setWidth(400);
        table.add(textButton).padRight(80);
        table.add(sliderContainer).width(sliderContainer.getWidth());
        table.add(textButton1).padLeft(80);

        table.pack();
        Gdx.input.setInputProcessor(stage);

    }

    @Override
    public void render() {
        super.render();

        Gdx.gl.glClearColor(1,1,1,1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        stage.draw();
        stage.act();
    }

    @Override
    public void resize(int width, int height) {
        super.resize(width, height);
        stage.getViewport().update(width,height);
         Camera camera=stage.getCamera();
        camera.position.set(camera.viewportWidth/2.0f,camera.viewportHeight/2.0f,0);
    }
}

输出

enter image description here