31

I have a strange problem with an AlphaAnimation. It is supposed to run repeatedly when an AsyncTask handler is called.

However, the first time the handler is called in the Activity, the animation won't start unless I touch the screen or if the UI is updated (by pressing the phone's menu button for example).

The strange part is that once the animation has run at least once, it will start without problem if the handler is called again.

Here's what the code looks like:

// AsyncTask handler
public void onNetworkEvent()
{
  this.runOnUiThread(new Runnable() {
    @Override
    public void run()
    {
      flashScreen(Animation.INFINITE);
    }
  });
}

// Called method
private void flashScreen(int repeatCount)
{
  final View flashView = this.findViewById(R.id.mainMenuFlashView);

  AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
  alphaAnimation.setRepeatCount(repeatCount);
  alphaAnimation.setRepeatMode(Animation.RESTART);
  alphaAnimation.setDuration(300);
  alphaAnimation.setInterpolator(new DecelerateInterpolator());
  alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation)
    {
      flashView.setVisibility(View.VISIBLE);
    }

    @Override
    public void onAnimationEnd(Animation animation)
    {
      flashView.setVisibility(View.GONE);
    }

    @Override
    public void onAnimationRepeat(Animation animation) { }
  });

  flashView.startAnimation(alphaAnimation);
}

I have noticed that runOnUIThread isn't necessary (same results occur if I don't use it), but I prefer keeping it as I'm not on the UI thread.

Any ideas on what could cause this?

rene
  • 41,474
  • 78
  • 114
  • 152
Jukurrpa
  • 4,038
  • 7
  • 43
  • 73
  • Too little code to understand what is your call sequence. Can you post the part of the around the execution of the Asynctask? Also, have you tried to log the entry to relevant methods to see what is going on? – ilomambo Apr 26 '13 at 14:50
  • Try delaying the animation by few seconds for the first time. This may be the case when are trying to run the animation before the view is ready. – Sagar Waghmare Apr 26 '13 at 14:51

5 Answers5

52

A little more research showed that my problem was the same a this question: Layout animation not working on first run

The flashView's visibility was set to GONE by default (causing the Animation not to start immediately as the View had never been rendered), so I just need to set it to INVISIBLE before calling flashView.startAnimation()

Community
  • 1
  • 1
Jukurrpa
  • 4,038
  • 7
  • 43
  • 73
30

If setting the View to VISIBLE won't work, as was in my case, it helped for me to call requestLayout() before starting the Animation, like so:

Animation an = new Animation() {
...   
view.requestLayout();
view.startAnimation(an);

In my case, my View was 0dip high which prevented onAnimationStart from being called, this helped me around that problem.

Zebaz
  • 1,545
  • 15
  • 11
  • 4
    On some version of Android even it doesn't work if initiate value of height or width of view is 0dip. In one of my cases I had to make initial value of 1dip and view wasn't visible because of margin. And this helped, animation worked on all version. – Oknesif Oct 15 '14 at 13:29
4

This worked for me:

view.setVisibility(View.VISIBLE);
view.startAnimation(animation);

I had to set the view to VISIBLE (not INVISIBLE, neither GONE), causing the view renderization needed to animate it.

Mario Velasco
  • 3,336
  • 3
  • 33
  • 50
0

That's not an easy one. Till you got a real answer : The animation start is triggered by onNetworkEvent. As we don't know the rest of the code, you should look there, try to change onNetworkEvent by an other event that you can easily identify, just to debug if the rest of the code is ok or if it's just the trigger that is responsible for it.

TheBeps
  • 36
  • 4
  • 1
    I can trigger the even easily, and it's working. Debugging shows that the `AnimationListener`'s `onAnimationStart` isn't called until I interact with the UI the first time. – Jukurrpa Apr 26 '13 at 14:31
  • @Jukurrpa does this issue apply to all Android versions or some specific version(s)? – Kai Apr 26 '13 at 14:34
  • @Kai I only have 4.1 and 4.2 devices to try it on, and it does it on both. – Jukurrpa Apr 26 '13 at 14:39
  • so onAnimationSart is not called... maybe you can try to put flashView.startAnimation(alphaAnimation); into public void run directly, just after flashScreen(Animation.INFINITE); – TheBeps Apr 26 '13 at 14:53
0

May be it will help someone, because previous answers not helped me.

My animation was changing height of view (from 0 to it's real height and back) on click - expand and collapse animations.

Nothing worked until i added listener and set visibility to GONE, when animation ends:

collapseAnim.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            view.setVisibility(View.GONE);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });

And when expand just set it to VISIBLE before animation:

 view.setVisibility(View.VISIBLE);
 view.startAnimation(expandAnim);
Vadim
  • 3,855
  • 2
  • 17
  • 22