1.配置环境
implementation 'com.android.support:support-dynamic-animation:27.1.1' 2.创建SpringAnimation对象 (参数类型依次为 作用View , 动画类型 , 最终位置相对自己)
val anim = SpringAnimation(childView, SpringAnimation.TRANSLATION_Y, 0f) 效果图:

练习demo:
class MainActivity : AppCompatActivity() {
private var isShow = false
private val animString = "Hello World!"
private val showAnims = mutableListOf<SpringAnimation>()
private val hideAnims = mutableListOf<SpringAnimation>()
private val height by lazy { getHeiget() }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initView()
initListener()
}
private fun initView() {
animString.forEach {
val childView = TextView(this@MainActivity)
childView.textSize = 36f
childView.text = it.toString()
llRootView.addView(childView)
childView.translationY = height.toFloat()
val childViewShowAnim = SpringAnimation(childView, SpringAnimation.TRANSLATION_Y, 0f)
showAnims.add(childViewShowAnim)
childViewShowAnim.spring.stiffness = SpringForce.STIFFNESS_VERY_LOW
childViewShowAnim.spring.dampingRatio = SpringForce.DAMPING_RATIO_MEDIUM_BOUNCY
val childViewHideAnim = SpringAnimation(childView, SpringAnimation.TRANSLATION_Y, height.toFloat())
hideAnims.add(childViewHideAnim)
childViewShowAnim.spring.stiffness = SpringForce.STIFFNESS_MEDIUM
childViewShowAnim.spring.dampingRatio = SpringForce.DAMPING_RATIO_LOW_BOUNCY
}
}
private fun initListener() {
btStart.setOnClickListener {
if (isShow) setHideAnim() else setShowAnim()
isShow = !isShow
}
}
private fun setShowAnim() {
showAnims.forEachIndexed { index, anim ->
llRootView.postDelayed({
anim.start()
}, index * 80L)
}
}
private fun setHideAnim() {
for (i in hideAnims.size - 1 downTo 0) {
llRootView.postDelayed({
hideAnims[i].start()
}, i * 60L)
}
}
private fun getHeiget(): Int {
val wm = this.getSystemService(Context.WINDOW_SERVICE)
val p = Point()
if (wm is WindowManager) {
wm.defaultDisplay.getSize(p)
}
return p.y
}
}
本文介绍了SpringAnimation的使用,包括如何创建SpringAnimation对象,并提供了实际操作的效果图和练习demo。

3164

被折叠的 条评论
为什么被折叠?



