首先淡出,然后淡入,在Android中不起作用

时间:2018-05-05 04:00:32

标签: android android-animation

我想要运行动画集:首先淡出,然后淡入,但是无法正常工作,这是代码:

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>

但是当我更改订单时(先淡出,然后淡出),正常运作

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>

为什么呢?

1 个答案:

答案 0 :(得分:1)

试试这个

<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);