动画展开/折叠的方法

摘要

本文介绍了Android中展开和折叠动画的实现方法。通过改变视图的高度,可以实现展开和折叠效果。

内容

如果你需要一个展开和折叠的动画效果,可以使用以下方法来实现:

 1fun View.expand() {
 2    visibility = View.VISIBLE
 3    val animate = TranslateAnimation(0f, 0f, -height.toFloat(), 0f)
 4    animate.duration = 200
 5    animate.fillAfter = true
 6    startAnimation(animate)
 7}
 8
 9fun View.collapse() {
10    val animate = TranslateAnimation(0f, 0f, 0f, -height.toFloat() )
11    animate.duration = 200
12    animate.fillAfter = true
13    startAnimation(animate)
14}

这两个扩展函数可以直接应用到任何视图上。expand()函数将指定的视图展开,collapse()函数将指定的视图折叠起来。这些函数使用了TranslateAnimation来实现动画效果。通过改变视图的垂直平移来实现展开和折叠的效果。

这些函数应该在视图的可见性发生变化时调用。比如,当用户点击一个按钮来展开或折叠视图时,可以通过调用expand()collapse()函数来实现动画效果。

总结

通过使用TranslateAnimation和视图的可见性,可以很容易地实现展开和折叠动画效果。你只需要调用相应的函数就可以实现这些效果。


相关文章推荐