【问题标题】:Libgdx (Android) - ImageButton has wrong size (scale)Libgdx (Android) - ImageButton 的大小(比例)错误
【发布时间】:2018-01-02 03:00:25
【问题描述】:

我对 ImageButton(s) 的大小有疑问,按钮未拉伸/缩放,因为设备的屏幕尺寸为 1920x1080,按钮图片为 342x129。 (它看起来比原始图片看起来更小)。 当我通过这个 game.batch.draw(playBtn, ...); 它工作正常,但我需要 ImageButtons。怎么了 ? 注意:宽度 = 480,高度 = 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();
    }

【问题讨论】:

  • 你想完成什么?你想让按钮填满整个屏幕吗?还是保持比例但填充宽度?还是填充宽度但不是一直到边缘?
  • 我不知道怎么解释,但我想达到这个(我需要在不同的屏幕分辨率上以与图片相同的方式显示):imgur.com/kiSoerr

标签: java android libgdx scale imagebutton


【解决方案1】:

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

这行没有意义:

playBtn.setSize(playBtn.getWidth(), playBtn.getHeight());

【讨论】:

  • 是的,我知道我什么都试过了,所以有些线条可能没有感觉,我想保持原始尺寸但有良好的比例(它有原始尺寸,但由于屏幕分辨率高于 480x800它必须缩放/拉伸,但我不知道如何)
  • @Noro96 那么为什么不像你那样设置它呢? playBtn.setSize((float)(WIDTH * someValue), (float) (HEIGHT * someValue));
  • 因为 title 是 Texture 而 playBtn 是 ImageButton,(纹理被正确缩放/拉伸)但 playBtn 不是(因为不同的类型)所以我也需要它以某种方式缩放/拉伸它......跨度>
【解决方案2】:

我会推荐在设计 UI 时使用非常方便的 Table。您在将角色添加到表格时指定角色的大小。

由于我不在家,我现在无法尝试,但它看起来像:

@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);
}   

编辑:

另一种选择是设置您放在图像按钮样式中的可绘制对象的最小尺寸:

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);

【讨论】:

  • 不工作....它正在改变尺寸,但没有将原始尺寸缩放到更大的屏幕,我想至少我可以做一些数学公式来计算和改变不同屏幕分辨率的尺寸,但是如果有类似于 ImageButtons 的 setProjectionMatrix 的东西,我会很高兴
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-09
相关资源
最近更新 更多