简体   繁体   中英

Android Studio Libgdx Sprite/Texture Transparency

I need to change the transparency of a texture/sprite and I really have no idea how to do it. Any help?

I see two solutions here - one is to change transparency directly in sprite:

    Sprite sprite = new Sprite( yourTexture );              
    Sprite otherSprite = new Sprite( yourOtherTexture );

    sprite.setAlpha(0.1f); // 0f is full transparent when 1f is full visible

    ...

    batch.begin();

    sprite.draw( batch ); //this one will be almost transparent
    otherSprite.draw( batch ); //this one will be normal

    batch.end();

you can always manipulate with Sprite color directly:

    sprite.setColor( sprite.getColor.r, sprite.getColor.g, sprite.getColor.b, 0.1f);

but it seems to be a silly way for me.

The second one which I recommend is to wrap your Sprite into Scene2d Actor class like for example Image . Then you can still change aplha directly but you are also able to use Scene2d Actions mechanism, manipulating actor's alpha smoothly (like alpha changing animation step by step).

Both Scene2d and Actions are to wide to describe it here but I encourage you to read about it here:

I added this to my GameRenderer.java:

private void drawTapTapChuggy() {
    batcher.setColor(1, 1, 1, 0.5f);
    batcher.draw(AssetLoader.tapTapChuggy, 28, 89, (83 * 0.9f), (52 * 0.9f));
    batcher.setColor(1, 1, 1, 1.0f);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM