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

时间:2018-04-12 14:19:15

标签: android kotlin android-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" />

1 个答案:

答案 0 :(得分:1)

在模拟器中运行代码之后,我认为Animation确实已完成,但View一直在弹出,因为View动画不会使最终状态成为永久状态,除非您提出请求通过写作(在Kotlin代码中)

  animation.fillAfter = true

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

中的xml文件中

另请参阅android:fillAfter上的文档setFillAfter()