Raphi
Raphi

Reputation: 203

Android View fades out but won't fade in

I created a really simple animation that is supposed to make an icon fade out then back in, then back out. (Just for fun)

When you tap the screen, I expect the icon to fade out in 50 milliseconds, then immediately fade back in, and immediately fade back out again.

However, when I tap, the icon fades out and does nothing more!

Here is the method:

public boolean onTouchEvent(MotionEvent event) {
        centralIcon = (ImageView) findViewById(R.id.centralIcon);
        float fullAlpha = centralIcon.getAlpha();
        centralIcon.animate().setDuration(50);
        centralIcon.animate().alpha(0);
        centralIcon.animate().alpha(fullAlpha);
        centralIcon.animate().alpha(0);
        return true;
    }

Why won't it fade back in?

Upvotes: 1

Views: 565

Answers (2)

AndroidNoob
AndroidNoob

Reputation: 2821

What you have specified is 3 animations that are supposed to happen one after the other, but the way that you have coded your animations they won't happen one after the other, all you are doing is returning the ViewPropertyAnimator associated with the view which is one instance, and overriding the alpha animation 3 times (as calling alpha() creates a new animation and cancels any more on the alpha property), thus only the final one will run.

You should create an animation set and offset the animations to start after the previous has finished (and perhaps more than 50 ms as that is very short! it will be barely noticeable).

This code should work:

    AnimationSet a = new AnimationSet(true);

    float fullAlpha = centralIcon.getAlpha();

    AlphaAnimation alpha[] = new AlphaAnimation[3];//array of 3 alpha animations

    alpha[0] = new AlphaAnimation(fullAlpha, 0);//first fade to 0
    alpha[0].setDuration(300);

     alpha[1] = new AlphaAnimation(0, fullAlpha);//then fade back to full
    alpha[1].setDuration(300);
    alpha[1].setStartOffset(300);//start after alpha[0] finishes

    alpha[2] = new AlphaAnimation(fullAlpha, 0);//then fade back to 0
    alpha[2].setDuration(300);
    alpha[2].setStartOffset(600);//start after alpha[0] and alpha[1] finish

    for(AlphaAnimation current : alpha)
        a.addAnimation(current);//add all the animations to the set

    a.setInterpolator(new LinearInterpolator());

    centralIcon.setAnimation(a);//attach the animation set to the view
    a.startNow();//start the animation set

Upvotes: 1

Douglas Zare
Douglas Zare

Reputation: 3316

From: http://developer.android.com/reference/android/view/ViewPropertyAnimator.html

public ViewPropertyAnimator alpha (float value)

Added in API level 12 This method will cause the View's alpha property to be animated to the specified value. Animations already running on the property will be canceled.


Part of the problem is in bold. You immediately cancel the first two alpha animations by calling alpha again. (You can test this by changing the target alpha values.) Even if this weren't the case, it just wouldn't make sense to expect the animations to behave the way you want. animate().alpha(value) doesn't do what you assumed.

If you want the view to start fading back in after a period like 50 milliseconds, you need to specify that delay somehow. There are many ways to make this sequence happen. For example, you can make an AnimationSet with offsets you specify on the later animations (as AndroidNoob answered). You can add an AnimationListener to listen for the end of the animation to start the next. You can make another thread which runs a sequence of animations on the UI thread.

Upvotes: 1

Related Questions