Libgdx(Android) - ImageButton尺寸错误(比例尺)

时间:2018-01-02 03:00:26

标签: java android libgdx scale imagebutton

我有ImageButton(s)大小的问题,按钮没有拉伸/缩放,因为设备的屏幕尺寸为1920x1080,按钮图片为342x129。 (它看起来比原始图片小)。 当我通过这个game.batch.draw绘制它(playBtn,...); 它工作正常,但我需要ImageButtons。怎么了 ? 注意:WIDTH = 480,HEIGHT = 800 图片:https://imgur.com/IJs4uPB

public MenuScreen(PlumberGame game) {
this.game = game;
camera = new OrthographicCamera();
viewport = new FitViewport(WIDTH,HEIGHT, camera);
stage =new Stage(viewport, spriteBatch);
background = new Texture("background.png");
title = new Texture("title.png");
camera.setToOrtho(false, WIDTH, HEIGHT);
}

   @Override
   public void show() {
   stage = new Stage(); //Set up a stage for the ui
    Gdx.input.setInputProcessor(stage); //Start taking input from the ui

    atlas = new TextureAtlas("ui/buttons.pack");
    skin = new Skin(atlas);

    table = new Table(skin);
    table.setBounds(0,0, WIDTH, HEIGHT);

    ImageButton.ImageButtonStyle playBtnStyle = new ImageButton.ImageButtonStyle();
    playBtnStyle.up = skin.getDrawable("playBtn");
    playBtnStyle.down = skin.getDrawable("playBtnOn");
    playBtn = new ImageButton(playBtnStyle);

    playBtn.setSize(playBtn.getWidth(), playBtn.getHeight());
    playBtn.getImage().setScaling(Scaling.fit);
    playBtn.invalidateHierarchy();
    playBtn.setPosition((float)(WIDTH * 0.15 ), (float) (HEIGHT * 0.5));
    stage.addActor(playBtn);
    }

   @Override
   public void render(float delta) {
   stage.getBatch().setProjectionMatrix(camera.combined);
   renderer.setProjectionMatrix(stage.getBatch().getProjectionMatrix());
    renderer.setTransformMatrix(stage.getBatch().getTransformMatrix());
    renderer.translate(WIDTH, HEIGHT, 0);
    stage.act(Gdx.graphics.getDeltaTime()); //Perform ui logic
    stage.getBatch().begin();
    stage.getBatch().draw(background, 0, 0, WIDTH,HEIGHT);
    stage.getBatch().draw(title, (float)(WIDTH * 0.12), (float) (HEIGHT * 0.75));

    stage.getBatch().end();
    stage.draw(); //Draw the ui
    }

    @Override
    public void resize(int width, int height) {
    stage.getViewport().update(width, height, true);
    table.invalidateHierarchy();
    table.setSize(width, height);
    camera.update();
    }

2 个答案:

答案 0 :(得分:0)

您正在以错误的方式设置playBtn大小。

这条线没有意义:

return redirect('login')->with(['lastUrl' => $request->url()]);

答案 1 :(得分:0)

我建议使用Table,这在设计UI时非常方便。在将actor添加到表中时指定actor的大小。

我现在无法尝试,因为我不在家,但它看起来像是:

@Override
public void show() {
    stage = new Stage(); //Set up a stage for the ui
    Gdx.input.setInputProcessor(stage); //Start taking input from the ui

    atlas = new TextureAtlas("ui/buttons.pack");
    skin = new Skin(atlas);

    table = new Table(skin);
    table.setBounds(0,0, WIDTH, HEIGHT);

    ImageButton.ImageButtonStyle playBtnStyle = new ImageButton.ImageButtonStyle();
    playBtnStyle.up = skin.getDrawable("playBtn");
    playBtnStyle.down = skin.getDrawable("playBtnOn");
    playBtn = new ImageButton(playBtnStyle);
    playBtn.setPosition((float)(WIDTH * 0.15 ), (float) (HEIGHT * 0.5));

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

    table.add(playBtn).expand().fillX().height(yourHeight);

    stage.addActor(table);
}   

修改

另一种方法是设置您在图像按钮样式中放置的drawable的最小尺寸:

ImageButton.ImageButtonStyle playBtnStyle = new ImageButton.ImageButtonStyle();
playBtnStyle.up = skin.getDrawable("playBtn");
playBtnStyle.down = skin.getDrawable("playBtnOn");

playBtnStyle.up.setMinWidth(yourWidth);
playBtnStyle.up.setMinHeight(yourHeight);
playBtnStyle.down.setMinWidth(yourWidth);
playBtnStyle.down.setMinHeight(yourHeight);