Patch
Patch

Reputation: 2527

AlphaAnimation: Fade out works, but not fade in

I have the following code:

public boolean stopped = false;

public void fadeOut(final ImageView obj, final int time, int delay)
    {
        if(stopped)
        {
            stopped = false;
            anim = new AlphaAnimation(1.00f, 0.00f);
            anim.setDuration(time);
            if(delay > 0)
            {
                anim.setStartOffset(delay);
            }
            anim.setAnimationListener(new AnimationListener() {

                @Override
                public void onAnimationStart(Animation animation) {
                    // TODO Auto-generated method stub
                    Log.v("FADER", "Fading out. Duration: " + time + "ms.");

                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    // TODO Auto-generated method stub
                    Log.v("FADER", "Fading out finished.");
                    //obj.setAlpha(255);
                    stopped = true;
                }
            });
            obj.startAnimation(anim);
        }
    }

and this code works fine. My object (an ImageView) fades out beautifully. But when I run this:

public void fadeIn(final ImageView obj, final int time, int delay)
    {
        if(stopped)
        {
            stopped = false;
            anim = new AlphaAnimation(0, 1);
            anim.setDuration(time);
            if(delay > 0)
            {
                anim.setStartOffset(delay);
            }
            anim.setAnimationListener(new AnimationListener() {

                @Override
                public void onAnimationStart(Animation animation) {
                    // TODO Auto-generated method stub
                    Log.v("FADER", "Fading in. Duration: " + time + "ms.");

                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    // TODO Auto-generated method stub
                    Log.v("FADER", "Fading in finished.");
                    //obj.setAlpha(255);
                    stopped = true;
                }
            });
            obj.startAnimation(anim);
        }
    }

It doesn't work - there is no fading in. The ImageView stays completely transparent.

Why is this, any ideas?

And for those who are wondering, yes, I have declared and set the boolean stopped, so it's not the issue, because when I look at LogCat, it's printing the text as it runs the fadeIn() function.

Upvotes: 1

Views: 1961

Answers (2)

Patch
Patch

Reputation: 2527

Solved!

Turns out, if you use [ImageView Object].setAlpha, and set it to 0 for instance, then when you run AlphaAnimation, it works between the boundaries of 0 and the current alpha of the image.

So, if you want to keep the image invisible after fadeout, the solution is to use [ImageView Object].setVisibility(View.INVISIBLE), and just set it back to View.VISIBLE before you run your animation.

Mission Accomplished.

Upvotes: 1

Kai
Kai

Reputation: 15476

Is your view's visibility set to View.VISIBLE?

Upvotes: 0

Related Questions