3

I have a com.badlogic.gdx.scenes.scene2d.ui.Image that is built from a Pixmap. The Pixmap is only one pixel because I'm using it to build an Image that functions as a background that can fade in and out.

Pixmap pmap = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
pmap.setColor(1.0f, 1.0f, 1.0f, 1.0f);
pmap.drawPixel(0, 0);
bgImage = new Image(new Texture(pmap));
pmap.dispose();
bgImage.setSize(MyGame.VIRUAL_WIDTH, MyGame.VIRUAL_HEIGHT);
bgImage.getColor().a = 0.0f;
bgImage.addAction(Actions.sequence(Actions.fadeIn(1.0f),Actions.delay(3.0f),Actions.fadeOut(1.0f)));
stage.addActor(bgImage);

It works great but my concern is that the game might be paused while the actions are taking place. I assume the actions will continue upon resuming so I need to keep the same Image but the underlying Pixmap is not managed and therefore needs to be recreated upon resume. I'm having trouble figuring out how to reattach the Texture/Pixmap to the Image. Building the Pixmap itself is easy but getting an existing Image to use it is the problem.

2 Answers 2

5

My solution was as follows

Assume Pixmap pixmap = ...

TextureData texData = new PixmapTextureData(pixmap, Format.RGBA8888, false, false, true);
Texture texture = new Texture(texData);
Image image = new Image(texture);

The last flag in PixmapTextureData is "managed". It runs on mine.

You can check out the first portion of the following link to see what is managed and what is not. http://www.badlogicgames.com/wordpress/?p=1513

1
  • I believe you need to be using the nightly releases for this - I don't think think it's an option in the stable version. Mar 25, 2014 at 6:48
2

There is no setTexture method on an Image. Looking at the Image(Texture) constructor it wraps the texture in a drawable region:

this(new TextureRegionDrawable(new TextureRegion(texture)));

So you can do something similar and use the Image.setDrawable method to change the drawable object used. Something like this:

bgImage.setDrawable(new TextureRegionDrawable(new TextureRegion(new Texture(pmap))));

I think you can probably reuse the TextureRegion and all of its containers (so after generating a new pixmap, just wrap it in a Texture and push that into the saved TextureRegion from before the pause.

1
  • I wasn't creating or saving the TextureRegion but it seems to work if I do: ((TextureRegionDrawable)bgImage.getDrawable()).setRegion(new TextureRegion(new Texture(pmap)));
    – Chase
    Feb 3, 2014 at 16:58

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.