1

I want to run animation set: fade out first, and then fade in, but is not working, this is code:

TextView iv_1 = findViewById(R.id.tv_1);
AnimationSet animSet = (AnimationSet) AnimationUtils
                       .loadAnimation(this, R.anim.fadeout_first);    // fadeout_first.xml
iv_1.setAnimation(animSet);

fadeout_first.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!--fade out first-->
    <alpha android:duration="2000"
           android:fromAlpha="1"
           android:toAlpha="0"
    />

    <!--and then fade in-->
    <alpha android:duration="2000"
           android:fromAlpha="0"
           android:toAlpha="1"
           android:startOffset="2000"/>
</set>

but when I change the order(fade in first, and then fade out), it is working well:

TextView iv_1 = findViewById(R.id.tv_1);
AnimationSet animSet = (AnimationSet) AnimationUtils
                       .loadAnimation(this, R.anim.fadein_first);  // fadein_first.xml
iv_1.setAnimation(animSet);

fadein_first.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!--fade in first-->
    <alpha android:duration="2000"
           android:fromAlpha="0"
           android:toAlpha="1"
    />
    <!--and then fade out-->
    <alpha android:duration="2000"
           android:fromAlpha="1"
           android:toAlpha="0"
           android:startOffset="2000"/>
</set>

why?

Guo
  • 1,761
  • 2
  • 22
  • 45

1 Answers1

1

Try this

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="false">

    <!--fade out first-->
    <alpha
        android:duration="2000"
        android:fromAlpha="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="0.0" />

    <!--and then fade in-->
    <alpha
        android:duration="1000"
        android:fromAlpha="0.0"
        android:startOffset="2000"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="1.0" />


</set>

JAVA CODE

 TextView iv_1 = findViewById(R.id.tv_1);
 AnimationSet animSet = (AnimationSet) AnimationUtils
                .loadAnimation(this, R.anim.fadeout_first);
 iv_1.setAnimation(animSet);
AskNilesh
  • 67,701
  • 16
  • 123
  • 163