简体   繁体   English

用于背景精灵的Android Alpha透明度无法达到预期效果

[英]Android alpha transparency for background sprite for fade in effect is not working as expected

I am using AndEngine-GLES2 and when i am trying to fade in the background for a new scene, the image is first displayed for a fraction of second and then it fades in as i needed. 我正在使用AndEngine-GLES2,当我尝试在背景中为新场景淡入淡出时,图像会先显示一秒钟,然后根据需要淡入淡出。 But that image being displayed without alpha transparency initially for a fraction of a second is making it look glitchy. 但是,最初显示的图像没有alpha透明性的时间仅为一秒,这使它看起来有些毛刺。

Here is my code to load the image 这是我的加载图像的代码

I am loading the sprite like, 我正在加载精灵,

bgSprite = new Sprite(0, 0, bgTextureRegion, LaunchGame.getSharedInstance().getEngine().getVertexBufferObjectManager());

And I am setting alpha transparency and fade in effects as 我设置Alpha透明度和淡入效果为

bgSprite.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
    final IEntityModifier iem = new AlphaModifier(1,0.0f,1.0f);
    iem.setAutoUnregisterWhenFinished(true);
    bgSprite.registerEntityModifier(iem);
    attachChild(bgSprite);

I tried to override the preDraw function of sprite but it dint help. 我试图重写sprite的preDraw函数,但它会有所帮助。 What could be the problem or any more data required? 可能是问题所在,还是需要更多数据? Or am I missing something stupid? 还是我想念一些愚蠢的东西?

Just add: 只需添加:

bgSprite.setAlpha(0.0f); 

before you attach your background. 在附加背景之前。 By default your sprite is attached with alpha (1.0) (thats why it shows up for a sec, and then the modifier is fired so instantly it goes to alpha(0.0f) and goes to 1.0f again slowly. 默认情况下,您的Sprite附加有alpha(1.0)(这就是为什么它会显示一秒钟,然后触发修改器的原因,因此立即将其转到alpha(0.0f),然后又缓慢变为1.0f。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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