I am trying to use a splash screen to cover the Google Map loading delay (for Maps v2).
I want to use an ImageView in a FrameLayout that should fade out after a few seconds and be able to override the onAnimationEnd to hide the splash screen’s ImageView.
When starting the animation without a delay, onAnimationEnd is being called appropriately:
new Animations().animateAlphaForLayout(splashLayout, 2000);
The problem is when I try to start the animation with postDelayed the onAnimationEnd does not get called at all:
splashLayout.postDelayed(new Runnable() {
@Override
public void run() {
new Animations().animateAlphaForLayout(splashLayout, 2000);
}
}, 3000);
The Animations class’ code is this:
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LayoutAnimationController;
public class Animations {
public void animateAlphaForLayout(final ViewGroup viewGroup, int duration) {
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(1.0f, 0.0f);
animation.setRepeatCount(0);
animation.setFillAfter(true);
animation.setDuration(duration);
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
viewGroup.setVisibility(View.GONE);
}
});
set.addAnimation(animation);
LayoutAnimationController controller = new LayoutAnimationController(set, 0.0f);
viewGroup.setLayoutAnimation(controller);
}
}
Does anyone know a way to do this for Android 2.3 and higher (API 10+)?
Thank you!
A simple workaround that gets the result I wanted (that is to show the
ImageViewfor a few seconds and then fade it out and set it toGONE):Because
postDelayedwas the culprit in my previous attempt, I dropped it in favor of usingAnimation‘ssetStartOffset(startOffset).That means I simply delayed the animation start with the interval I used initially for
postDelayed. I still don’t know whypostDelayedmessed the listener of the animation.