android 动态添加View展示隐藏动画

1、工具类

enum class AnimationState {

    STATE_SHOW, //显示
    STATE_HIDE, //隐藏

    STATE_SHOW_UP,
    STATE_HIDE_UP,

    STATE_SHOW_DOWN,
    STATE_HIDE_DOWN,

    STATE_SHOW_LEFT,
    STATE_HIDE_LEFT,

    STATE_SHOW_RIGHT,
    STATE_HIDE_RIGHT
}
object AnimationUtils {

    fun showOrHideAnimation(view: View, state: AnimationState, duration: Long) {

        view?.let {
            it.post {
                val viewHeight = it.measuredHeight + 50
                val viewWidth = it.measuredWidth + 50

                var start = 0f
                var end = 0f
                when (state) {
                    AnimationState.STATE_SHOW,
                    AnimationState.STATE_SHOW_UP,
                    AnimationState.STATE_SHOW_DOWN,
                    AnimationState.STATE_SHOW_LEFT,
                    AnimationState.STATE_HIDE_RIGHT -> {
                        //显示
                        end = 1f
                        it.visibility = View.VISIBLE
                    }
                    else -> {
                        //隐藏
                        start = 1f
                        it.visibility = View.INVISIBLE
                    }
                }

                //动画集合
                val animationSet = AnimationSet(true)

                //渐变动画
                val alphaAnimation = AlphaAnimation(start, end)
                alphaAnimation.duration = duration
                alphaAnimation.fillAfter = true


                //添加渐变动画
                animationSet.addAnimation(alphaAnimation)
                //添加平移动画
                getTranslateAnimation(
                    state,
                    viewHeight.toFloat(),
                    viewWidth.toFloat()
                )?.let { translateAnimation ->
                    animationSet.addAnimation(translateAnimation)
                }

                //添加监听器
                animationSet.setAnimationListener(object : Animation.AnimationListener {
                    override fun onAnimationStart(p0: Animation?) {

                    }

                    override fun onAnimationEnd(p0: Animation?) {

                    }

                    override fun onAnimationRepeat(p0: Animation?) {
                        it.clearAnimation()
                    }
                })

                it.animation = animationSet
                animationSet.start()
            }

        }


    }


    /**
     * 获取平移动画
     */
    private fun getTranslateAnimation(
        state: AnimationState,
        viewHeight: Float,
        viewWidth: Float
    ): TranslateAnimation? {
        var translateAnimation: TranslateAnimation? = null
        when (state) {
            AnimationState.STATE_SHOW_UP -> translateAnimation =
                TranslateAnimation(0f, 0f, viewHeight, 0f) //平移动画  从0,100,平移到0,0
            AnimationState.STATE_HIDE_UP -> translateAnimation =
                TranslateAnimation(0f, 0f, 0f, viewHeight) //平移动画  从0,0,平移到0,100


            AnimationState.STATE_SHOW_DOWN -> translateAnimation =
                TranslateAnimation(0f, 0f, -viewHeight, 0f) //平移动画  从0,-100,平移到0,0
            AnimationState.STATE_HIDE_DOWN -> translateAnimation =
                TranslateAnimation(0f, 0f, 0f, -viewHeight) //平移动画  从0,0,平移到0,-100


            AnimationState.STATE_SHOW_LEFT -> translateAnimation =
                TranslateAnimation(-viewWidth, 0f, 0f, 0f) //平移动画  从-100,0,平移到0,0
            AnimationState.STATE_HIDE_LEFT -> translateAnimation =
                TranslateAnimation(0f, -viewWidth, 0f, 0f) //平移动画  从0,0,平移到-100,0


            AnimationState.STATE_SHOW_RIGHT -> translateAnimation =
                TranslateAnimation(viewWidth, 0f, 0f, 0f) //平移动画  从100,0,平移到0,0
            AnimationState.STATE_HIDE_RIGHT -> translateAnimation =
                TranslateAnimation(0f, viewWidth, 0f, 0f) //平移动画  从0,0,平移到0,100
        }
        return translateAnimation
    }

}

2、使用

显示动画

AnimationUtils.showOrHideAnimation(textView, AnimationState.STATE_SHOW_LEFT, 1000)

隐藏动画

AnimationUtils.showOrHideAnimation(textView, AnimationState.STATE_HIDE_LEFT, 1000)

你可能感兴趣的:(Android,Learning,android,kotlin)