Question

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!

Was it helpful?

Solution

A simple workaround that gets the result I wanted (that is to show the ImageView for a few seconds and then fade it out and set it to GONE):

Because postDelayed was the culprit in my previous attempt, I dropped it in favor of using Animation's setStartOffset(startOffset).

That means I simply delayed the animation start with the interval I used initially for postDelayed. I still don't know why postDelayed messed the listener of the animation.

OTHER TIPS

I have nested LinearLayouts and one is parentHolder another is menuHolder inside of parentHolder. When touched outside of parent holder, I'm hiding the inner one.

package com.fmc.component;

import com.fmc.marketing.tanitim.R;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.widget.LinearLayout;

public class NavigatorTest {

public void AnimeEt(LinearLayout llParentHolder) {
    final LinearLayout llHolder = (LinearLayout) llParentHolder.findViewById(R.id.llHolder);
    AnimationSet aset = new AnimationSet(true);
    AlphaAnimation alpha = new AlphaAnimation(1, 0);
    alpha.setStartOffset(500);
    alpha.setDuration(1000);
    aset.addAnimation(alpha);
    alpha.setAnimationListener(new 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) {
            llHolder.setVisibility(LinearLayout.GONE);
        }
    });
    llHolder.startAnimation(aset);
}

}

There might be someone still having this issue and not found a solution -even though reads a lot of stackoverflow answers- like me!

So my case was: I used animatorSet and

there was not a single view that i could call clearAnimation on, I wasn't calling my animation from backgroundThread -which you should never do that, btw- As a solution, i did call animatorSet.end() right before animatorSet.start()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top