Smooth text change in android animation

前端 未结 7 2009
刺人心
刺人心 2020-12-11 00:29

I want to add animation in android text view so that when a text changes it should change smoothly and slowly. Like, fade in or fade out when the text changes. Is it possibl

7条回答
  •  离开以前
    2020-12-11 01:14

    This is my extension for fadOut fadIn a new text.

    // TextViewExtensions
    
    fun TextView.setTextAnimation(text: String, duration: Long = 300, completion: (() -> Unit)? = null) {
        fadOutAnimation(duration) {
            this.text = text
            fadInAnimation(duration) {
                completion?.let {
                    it()
                }
            }
        }
    }
    
    // ViewExtensions
    
    fun View.fadOutAnimation(duration: Long = 300, visibility: Int = View.INVISIBLE, completion: (() -> Unit)? = null) {
        animate()
                .alpha(0f)
                .setDuration(duration)
                .withEndAction {
                    this.visibility = visibility
                    completion?.let {
                        it()
                    }
                }
    }
    
    fun View.fadInAnimation(duration: Long = 300, completion: (() -> Unit)? = null) {
        alpha = 0f
        visibility = View.VISIBLE
        animate()
                .alpha(1f)
                .setDuration(duration)
                .withEndAction {
                    completion?.let {
                        it()
                    }
                }
    }
    
    • Example :

    textView. setTextWithAnimation("Hello world", 500)

提交回复
热议问题