简体   繁体   English

如何在android中处理视图动画?

[英]How can I handle view animation in android?

I'm in the process of learning Animation in android and I have several questions -我正在 android 中学习动画,我有几个问题 -

I have view that I animate based on some bool:我认为我基于一些布尔值制作动画:

mainFab.setOnClickListener {

        isOpen = ViewAnimations.rotate(binding.mainFab, !isOpen)

        if (isOpen) {
            ViewAnimations.apply {
                showMenu(binding.shareFab)
            }
        } else {
            ViewAnimations.apply {
                hideMenu(binding.shareFab)
            }}

}

ViewAnimations methods:视图动画方法:

fun rotateFab(view: View, isFabOpen: Boolean):  Boolean {
    view.animate()
        .rotation(if (isFabOpen) 1440f else 0f)
        .setDuration(2000)
        .setListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator?) {
                super.onAnimationEnd(animation)
                Log.d(TAG, "onAnimationEnd: ")
            }
        })
    return isFabOpen
}

fun showMenu(view: View) {
    view.visibility = View.VISIBLE
    view.alpha = 0f
    view.animate()
        .setDuration(2000)
        .setListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator?) {

                super.onAnimationEnd(animation)
            }
        })
        .alpha(1f)
        .start()
}

fun hideMenu(view: View) {
    view.animate()
        .setDuration(2000)
        .setListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator?) {
                view.visibility = View.GONE
                super.onAnimationEnd(animation)
            }
        }).alpha(0f)
        .start()

My questions are:我的问题是:

  1. In showMenu function, why my animation doesn't work properly without the empty listener?showMenu函数中,为什么我的动画在没有空侦听器的情况下无法正常工作? It's working fine at the first time, but from the second time and on it does animate the view, but then set the alpha to 0/ view to gone.第一次工作正常,但从第二次开始,它确实为视图设置了动画,但随后将 alpha 设置为 0/视图消失了。

  2. Why the animation still working without .start() ?为什么动画在没有.start()情况下仍然有效? Is it mandatory to use it?是否必须使用它?

  3. if I start animation by calling showMenu and then at the half way I'm calling hideMenu it just hide the view in very ugly way, there is a way to "reverse" the animation in more elegant way?如果我通过调用showMenu开始动画,然后在中途调用hideMenu它只是以非常丑陋的方式隐藏视图,有没有办法以更优雅的方式“反转”动画?

All yours problem is from over engineering.你所有的问题都来自过度工程。 What is doing the object ViewAnimations ?对象ViewAnimations在做什么?

About question :关于问题:

  1. There is can not be problem in empty listener.空侦听器不会有问题。 There is a problem somewhere in your logic.你的逻辑有问题。
  2. Without .start() animation can not be launched.没有.start()动画无法启动。 If animation is starting so there again a problem somewhere in your logic.如果动画正在开始,那么您的逻辑中的某处又会出现问题。
  3. ViewPropertyAnimator animation can perfect animate from semi-state to new state. ViewPropertyAnimator动画可以完美地从半状态到新状态的动画。 I recommend you to start write animation from the beginning to be confident in all your steps.我建议您从头开始编写动画,以便对您的所有步骤充满信心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM