【问题标题】:LibGDX ImageButton image not to scale?LibGDX ImageButton 图像不按比例缩放?
【发布时间】:2014-03-16 04:03:01
【问题描述】:

红色方块是按钮的边界,而图像仍以 32x32 像素为中心。我尝试使用 button.getImage() 将位置和大小设置为按钮的值,但似乎没有任何效果。

【问题讨论】:

  • 不是一个完整的答案,但我发现:在按钮样式的可绘制对象上设置 setMinWidth() 和 setMinHeight() 将图像设置为按钮的边界,但不能正确缩放,如 here 所示.
  • 面临同样的问题

标签: java resize libgdx imagebutton


【解决方案1】:

只需使用普通的Button。它还包含一个Drawable 作为其背景,但它总是被拉伸以填充按钮。见JavaDoc for ImageButton

...如果图像是按钮的大小,则可以使用没有任何子元素的 Button,其中Button.ButtonStyle.upButton.ButtonStyle.downButton.ButtonStyle.checked 九个补丁定义了图像。

请注意,这些实际上并不需要是九个补丁;任何Drawable 都可以。

【讨论】:

  • 我刚刚从 ImageButton 切换到 Button 并且可以正常工作!谢谢。
【解决方案2】:

可能超晚了,但你试过了吗:

yourButton.getImage().setFillParent(true);

【讨论】:

  • 它调整了图像的大小,但它不再在按钮中居中
  • 我想出了如何设置尺寸并写了一个答案。希望这会有所帮助!
【解决方案3】:

扩展@xitnesscomplex 的答案: 您可以使用yourButton.getImage().setFillParent(true); 设置大小。

设置偏移量有点违反直觉

ImageButton.ImageButtonStyle style = new ImageButton.ImageButtonStyle();
style.imageUp = new TextureRegionDrawable(new Texture(Gdx.files.internal(btn.png")));
ImageButton yourButton = new ImageButton(style);
style.unpressedOffsetX = -new_brush.getWidth()/4.0f;
style.unpressedOffsetY = -new_brush.getHeight()/4.0f;
style.pressedOffsetX = -new_brush.getWidth()/4.0f;
style.pressedOffsetY = -new_brush.getHeight()/4.0f;
style.checkedOffsetX = -new_brush.getWidth()/4.0f;
style.checkedOffsetY = -new_brush.getHeight()/4.0f;
yourButton.getImage().setFillParent(true);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-27
    • 1970-01-01
    • 2016-09-15
    相关资源
    最近更新 更多