Android LibGDX-ImageButton-使用背景设置图像
根据我的理解,Android LibGDX-ImageButton-使用背景设置图像,android,libgdx,Android,Libgdx,根据我的理解,LibGDX中的ImageButton是一个包含图像的框架。可以设置框架的背景吗 例如,我想使用一个按钮背景,并在该图像的顶部应用一个图标 现行代码 具有背景的蒙皮: "com.badlogic.gdx.scenes.scene2d.ui.ImageButton$ImageButtonStyle": { "default": { "imageDown": "button-down"
LibGDX
中的ImageButton
是一个包含图像的框架。可以设置框架的背景吗
例如,我想使用一个按钮背景,并在该图像的顶部应用一个图标
现行代码
具有背景的蒙皮:
"com.badlogic.gdx.scenes.scene2d.ui.ImageButton$ImageButtonStyle": {
"default": {
"imageDown": "button-down",
"imageUp": "button-up"
}
}
创建ImageButton:
// 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.
背景图像。
覆盖图标的背景图像。
感谢您的帮助。根据定义,按钮将有三种状态: imageDown-当鼠标单击按钮时 imageUp-当鼠标在按钮上释放时 imageChecked-鼠标悬停在按钮上时 在scene2d api中,还没有手动设置imageButton的背景图像的方法,但是如果您确实想要这样的图像,最好有一个图像数组和一个关于所需图像的索引,并使用sprite批处理进行渲染,例如:
Texture[] images;
int currentImage;
您可以实现自己的按钮类来扩展ImageButton。 然后,如果重载构造函数,可以将imageUp、imageDown和background的Drawables或纹理传递给它:
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
import com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable;
public class myButton extends ImageButton
{
public myButton(Texture texture_up, Texture texture_down, Texture background)
{
super(new SpriteDrawable(new Sprite(texture_up)),
new SpriteDrawable(new Sprite(texture_down)));
this.setBackground(new SpriteDrawable(new Sprite(background));
}
}
现在,您可以使用自己的Button类并按如下方式实例化它:
Texture textureUp = new Texture(Gdx.files.internal("data/image_up.png"));
Texture textureDown = new Texture(Gdx.files.internal("data/image_down.png"));
Texture background = new Texture(Gdx.files.internal("data/background.png"));
MyButton myButton = new MyButton(textureUp, textureDown, background);
也许玩一玩,你会发现你还能用它做什么。只需确保图像的分辨率正确即可。背景不必是图像。诀窍是使用两种不同的可用样式。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);
类似的东西(对我来说,回环,图标是可绘制的)。但这是最基本的,对我来说很有吸引力。我想我的困惑来自libgdxapi。带a,可上、下和检查。但是看看,上面写着“一个带有子图像的按钮来显示一个图像。”我的理解是,ImageButton在已经有3个可绘制状态的顶部有一个图像。不是这样吗?如果不是,那意味着什么?你测试过了吗?我尝试过这种方法,但效果不好。另一个更高投票率的答案似乎是可行的。