Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LibGDX - ImageButton - Setting Image With Background

Tags:

android

libgdx

From my understanding, the ImageButton within LibGDX is a frame containing an image. Is it possible to set the background of the frame?

For example, I would like to use a Button background, and apply an Icon on top of that image.

Current Code

Skin with the background:

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

Creating the 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 image.

static background Button image

Background image with icon overlayed.

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

Thank you for your help.

like image 254
Advice-Dog Avatar asked Dec 06 '22 00:12

Advice-Dog


1 Answers

The trick is using the 2 different Styles available. ImageButtonStyle to set the icon attributes, but since ImageButtonStyle extends ButtonStyle, the background attributes are set in ButtonStyle. Something 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);

Something like that (for me, backround, icon_* are Drawable). But that's the basics and worked like a charm for me.

like image 148
Peter R Avatar answered Dec 10 '22 12:12

Peter R