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.
Solved!
Turns out, if you use
[ImageView Object].setAlpha, and set it to0for instance, then when you runAlphaAnimation, it works between the boundaries of0and 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 toView.VISIBLEbefore you run your animation.Mission Accomplished.