创建用于控制动画的自定义动画类是一种好习惯吗?

时间:2019-06-09 09:27:39

标签: android android-animation

Android studio 3.4.1

创建用于控制动画的自定义动画类

我具有以下用于启动,停止和检查动画是否正在运行的界面

interface AnimationController {
    fun start()
    fun isRunning(): Boolean
    fun stop()
}

实现

class AnimationControllerImp : AnimationController {

    private companion object {
        const val START_DELAY = 500L
        const val DURATION = 400L
    }

    private val animationSet by lazy {
        AnimatorSet()
    }

    override fun isRunning() = animationSet.isRunning

    override fun stop() {
        animationSet.end()
    }

    override fun start() {
        val textAnimator = ValueAnimator.ofObject(ArgbEvaluatorCompat(), 0, 1)
        textAnimator.addUpdateListener { animation ->
            /* do some animation here */
        }

        val backgroundAnimator = ValueAnimator.ofFloat(0, 1)
        backgroundAnimator.addUpdateListener { animation ->
            /* do some animation here */
        }

        animationSet.startDelay = START_DELAY
        animationSet.duration = DURATION
        animationSet.interpolator = AccelerateInterpolator()
        animationSet.playTogether(textAnimator, backgroundAnimator)
        animationSet.start()
        animationSet.addListener(object: Animator.AnimatorListener {
            override fun onAnimationRepeat(animation: Animator?) {}

            override fun onAnimationEnd(animation: Animator?) {

            }

            override fun onAnimationCancel(animation: Animator?) {

            }

            override fun onAnimationStart(animation: Animator?) {

            }    
        })
    }
}

然后客户可以按以下方式使用此类

fun startPlayTogether() {
    val animationController: AnimationController = AnimationControllerImp()
    if(animationController.isRunning) {
        animationController.stop()    
    }
    else {
        animationController.start()
    }
}

对于自定义动画类来说,这是一个好的设计吗?

0 个答案:

没有答案