如何阻止动画重复-版式动画 [英] How to stop animation from repeating - Layout animation
问题描述
我正在尝试在按钮单击时在相对布局内对几个文本视图进行动画处理(淡入+弹出).但是在完成所有文本视图的动画设置之后,动画将再次重复.有人可以帮我阻止它重复.下面是我的代码.
I'm trying to animate(fade out+pop-in) a couple of text views inside a relative layout on a button click. But after all the text views are done animating, the animation is repeating again. Can someone help me to stop it from repeating. Below is my code.
也请告知我是否需要其他资源.
Also please let me know if any other resources are required.
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
确实完成了,但View
却不断弹出,因为View
动画无法制作最终的州永久性居民,除非您通过书面形式(以Kotlin代码)要求
After running your code in the emulator I think the Animation
did finish but the View
s kept popping in because a View
animation does not make the final state permanent unless you request it by writing (in the Kotlin code)
animation.fillAfter = true
或通过将android:fillAfter
作为属性添加到res/anim中的xml文件
or by adding android:fillAfter
as attribute to the xml file in res/anim
另请参见 android:fillAfter 分别 setFillAfter()
这篇关于如何阻止动画重复-版式动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!