android - LibGDX - ImageButton - 设置图像背景

标签 android libgdx

据我了解,LibGDX 中的ImageButton 是一个包含图像的框架。是否可以设置框架的背景?

例如,我想使用按钮背景,并在该图像上应用图标。

当前代码

皮肤与背景:

"com.badlogic.gdx.scenes.scene2d.ui.ImageButton$ImageButtonStyle": {
    "default": {
        "imageDown": "button-down", 
        "imageUp": "button-up"
    }
}

创建图像按钮:

// Getting imageButtonStyle with "default" style, as it just has the background.
ImageButton.ImageButtonStyle imageButtonStyle = skin.get( "default", ImageButton.ImageButtonStyle.class );
ImageButton button = new ImageButton( imageButtonStyle );
// Set the image on the button to something.

背景图片。

static background Button image

覆盖图标的背景图片。

static background Button image with heart icon static background Button image with mail icon

感谢您的帮助。

最佳答案

诀窍是使用 2 种不同的可用样式。 ImageButtonStyle 来设置图标属性,但是由于ImageButtonStyle 扩展了ButtonStyle,所以背景属性在ButtonStyle 中设置。像这样的东西:

ImageButtonStyle style = new ImageButtonStyle();
style.up           = background;
style.down         = background;
style.checked      = background;
style.imageUp      = icon_up;
style.imageDown    = icon_down;
style.imageChecked = icon_checked;
style.unpressedOffsetY = -20; // to "not" center the icon
style.unpressedOffsetX = -30; // to "not" center the icon

ImageButton heartButton = new ImageButton(style);
ImageButton envelopeButton = new ImageButton(envelopeStyle);

类似的东西(对我来说,背景,icon_* 是可绘制的)。但这是最基本的,对我来说就像一个魅力。

关于android - LibGDX - ImageButton - 设置图像背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26429723/

相关文章:

java - BitmapFont 类没有 getBound(String) 方法

byte - Libgdx Pixmap 和 getFrameBufferPixels

在 ADT Mac Bundle 中创建第一个 Android 应用程序时出现 java.lang.NullPointerException

android - 无法使用runtime.exec重新启动设备

android - CollapsingToolbarlayout 中的工具栏问题

java - 如何减少 libgdx 中 2D Sprite 的高 RAM 使用率

java - Libgdx 在 android 中获取鼠标位置

java - 当设置边界太小时,Libgdx scene2d 触摸板获取 'inverted'

c# - 如何在 Unity Android 应用程序中使用 OAuth 2.0 获取访问 token ?

android - Xamarin.Android 是否有热重载替代方案?