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

观书散遗帙,探古穷至妙。这篇文章主要讲述Libgdx(Android) - ImageButton尺寸错误(比例尺)相关的知识,希望能为你提供帮助。
我有ImageButton(s)大小的问题,按钮没有拉伸/缩放,因为设备的屏幕尺寸为1920x1080,按钮图片为342x129。 (它看起来比原始图片小)。当我通过这个game.batch.draw绘制它(playBtn,...); 它工作正常,但我需要ImageButtons。怎么了 ?注意:WIDTH = 480,HEIGHT = 800 picture: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 uiatlas = 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(); }

答案你正在以错误的方式设置playBtn大小。
这条线没有意义:
playBtn.setSize(playBtn.getWidth(), playBtn.getHeight());

另一答案我建议使用Table,这在设计UI时非常方便。在将表单添加到表时指定actor的大小。
我现在不能尝试,因为我不在家,但它看起来像:
@Override public void show() { stage = new Stage(); //Set up a stage for the ui Gdx.input.setInputProcessor(stage); //Start taking input from the uiatlas = 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); }

编辑:
【Libgdx(Android) - ImageButton尺寸错误(比例尺)】另一种方法是设置您在图像按钮样式中放置的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);


    推荐阅读