3

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!

  • The question is, to which Handler are you posting this Runnable, and what does that Handler do. If that Handler is your main UI thread and it is busy, it will never get the chance to process your request. – class stacker Jan 31 '13 at 09:38
  • Oh wait a second, you're posting it to a View, so you're posting it to the main UI thread. So what does your main UI thread do? – class stacker Jan 31 '13 at 09:46
  • Yes I am using a view for posting and as I said, the request is being processed because the animation works. The only thing not working is the 'onAnimationEnd' of the listener. On the interface thread I just display a SupportMapFragment. – mirel.cocosila Jan 31 '13 at 09:53
  • You're right. I didn't fully get your Q. Are you sure `onAnimationEnd()` is not getting called at all (by means of debugging/logging), or do you conclude it's not called because you do not see its effect? – class stacker Jan 31 '13 at 10:05
  • :) Of course I am sure. I used the debugger and noticed that if I call the animation directly (not with `postDelayed`) everything works - even `onAnimationEnd`. – mirel.cocosila Jan 31 '13 at 10:15

3 Answers3

2

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.

lucian.pantelimon
  • 3,673
  • 4
  • 29
  • 46
0

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);
}

}

uzay95
  • 16,052
  • 31
  • 116
  • 182
0

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()

yahya
  • 4,810
  • 3
  • 41
  • 58