~/snippets/AndroidAnimationUtils
Published on

Animation Utils

292 words2 min read
<!-- fade_in_anim.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:duration="300"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />
</set>
<!-- fade_out_anim.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:duration="500"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" />
</set>
object XAnimationUtils {

    fun fadeInView(
        view: View, listener: Animation.AnimationListener = object : Animation.AnimationListener {
            override fun onAnimationStart(animation: Animation?) {}
            override fun onAnimationEnd(animation: Animation?) {}
            override fun onAnimationRepeat(animation: Animation?) {}
        }
    ) {
        view.apply {
            visibility = View.VISIBLE
            animation?.cancel()
            startAnimation(
                AnimationUtils.loadAnimation(view.context, R.anim.fade_in_anim).apply {
                    setAnimationListener(listener)
                })
        }
    }

    fun fadeInView(
        view: View,
        inDuration: Long,
        listener: Animation.AnimationListener = object : Animation.AnimationListener {
            override fun onAnimationStart(animation: Animation?) {}
            override fun onAnimationEnd(animation: Animation?) {}
            override fun onAnimationRepeat(animation: Animation?) {}
        }
    ) {
        view.apply {
            visibility = View.VISIBLE
            animation?.cancel()
            startAnimation(
                AnimationUtils.loadAnimation(view.context, R.anim.fade_in_anim).apply {
                    setAnimationListener(listener)
                    duration = inDuration
                }
            )
        }
    }

    fun fadeOutView(view: View) {
        when (view.visibility) {
            View.VISIBLE -> {
                view.apply {
                    animation?.cancel()
                    startAnimation(
                        AnimationUtils.loadAnimation(view.context, R.anim.fade_out_anim).apply {
                            setAnimationListener(object : Animation.AnimationListener {
                                override fun onAnimationStart(p0: Animation?) {}

                                override fun onAnimationEnd(p0: Animation?) {
                                    visibility = View.GONE
                                }

                                override fun onAnimationRepeat(p0: Animation?) {}
                            })
                        })
                }
            }
            View.GONE -> {
            }
            View.INVISIBLE -> {
            }
        }
    }

    fun fadeOutView(view: View, listener: Animation.AnimationListener) {
        when (view.visibility) {
            View.VISIBLE -> {
                view.apply {
                    animation?.cancel()
                    startAnimation(
                        AnimationUtils.loadAnimation(view.context, R.anim.fade_out_anim).apply {
                            setAnimationListener(listener)
                        })
                }
            }
            View.GONE -> {
            }
            View.INVISIBLE -> {
            }
        }
    }

    fun fadeOutView(view: View, inDuration: Long, listener: Animation.AnimationListener) {
        when (view.visibility) {
            View.VISIBLE -> {
                view.apply {
                    visibility = View.VISIBLE
                    animation?.cancel()
                    startAnimation(
                        AnimationUtils.loadAnimation(view.context, R.anim.fade_out_anim).apply {
                            setAnimationListener(listener)
                            duration = inDuration
                        }
                    )
                }
            }
            View.GONE -> {
            }
            View.INVISIBLE -> {
            }
        }
    }
}