如何阻止动画重复 - 布局动画

How to stop animation from repeating - Layout animation

我正在尝试在单击按钮时在相对布局内设置动画(淡出+弹出)几个文本视图。但是在所有文本视图完成动画之后,动画再次重复。有人可以帮我阻止它重复。下面是我的代码。

另外,如果需要任何其他资源,请告诉我。

Activity.kt(onCreate 方法内部)

createTextViews() // dynamic creation of textviews
animateView(relLayout,R.anim.pop_in_fadein,1)

//
// some other code
//

btnCancel1.setOnClickListener(View.OnClickListener {
                    animateView(relLayout,R.anim.pop_in_fadeout,0) //0 here is the direction of animation

                    val handler = Handler()
                    handler.postDelayed(Runnable { finish() }, 1150) 
// I'm trying to explicitly stop the animation by closing the activity which shouldn't be done 
                })

 private fun animateView(viewGroup: RelativeLayout?,resource:Int, direction:Int) {
        val animation = AnimationUtils.loadAnimation(this, resource)
        animation.startOffset = 750L

        val controller = LayoutAnimationController(animation)
        controller.delay = 0.1f

        // 0 for normal direction, 2 for random and 1 for reverse direction
        // the direction in which the child views are created
        controller.order = direction

        viewGroup?.setLayoutAnimation(controller)
        viewGroup?.startLayoutAnimation()
    }

pop_in_fade_out.xml

<?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
        android:interpolator="@android:anim/overshoot_interpolator">

        <scale
            android:duration="300"
            android:fromXScale="1.0"
            android:fromYScale="1.0"
            android:pivotX="50%"
            android:pivotY="50%"
            android:toXScale="0.0"
            android:toYScale="0.0" />

        <alpha
            android:duration="500"
            android:fromAlpha="1.0"
            android:toAlpha="0.0" />

    </set>

pop_in_fade_in.xml

<scale
    android:duration="300"
    android:fromXScale="0.0"
    android:fromYScale="0.0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="1.0"
    android:toYScale="1.0" />

<alpha
    android:duration="500"
    android:fromAlpha="0.0"
    android:toAlpha="1.0" />

在模拟器中 运行 你的代码之后,我认为 Animation 确实完成了,但是 Views 不断弹出,因为 View 动画没有进入最终阶段状态永久,除非您通过书面形式(在 Kotlin 代码中)提出请求

  animation.fillAfter = true

或通过将 android:fillAfter 作为属性添加到 res/anim

中的 xml 文件

另请参阅 android:fillAfter respectively setFillAfter()

上的文档