Android – libgdx – imagebutton – set image and background

According to my understanding, the imagebutton in libgdx is a frame containing images. Can I set the background of the frame?

For example, I want to use a button background and apply an icon at the top of the image

Current code

Skin and background:

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

To create an 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.

Background picture

Icon overlaid with background image

Thank you for your help

resolvent:

The trick is to use two different styles. Imagebuttonstyle is used to set the icon properties, but since imagebuttonstyle extends buttonstyle, the background properties are set in buttonstyle. It is like:

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

Such things (for me, background, icon_ * are drawable). But this is basic knowledge, which is like a charm to me

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>