Rgfvfk Iff
Rgfvfk Iff

Reputation: 1609

Animate view width

Is it possible to control the width animation for a view that has wrap_content? Right now I have a TextView with a barrier to its right side. I have another background View that aligns to this barrier. When the TextView changes text it just flashes to the new width but I want to animate the width with an animator where I can control the timing, interpolator etc. I know that I can set something like animateLayoutChanges but the effect is not customisable.

Upvotes: 0

Views: 103

Answers (1)

Prithvi Bhola
Prithvi Bhola

Reputation: 3151

You can use ObjectAnimator along with AnimatorSet for the animation.

Here is an extension in kotlin for the view on which you want to apply the animation.

inline fun View.animateWidth(duration: Long = 1000, startDelay: Long = 0) {
    val scaleX = ObjectAnimator.ofFloat(this, "scaleX", /*Change width*/)
    //val scaleY = ObjectAnimator.ofFloat(this, "scaleY", /*Change width*/)
    val set = AnimatorSet()
    //set.playTogether(scaleX, scaleY)
    set.playTogether(scaleX)
    set.duration = duration
    set.startDelay = startDelay
    set.interpolator = LinearInterpolator()
    set.addAnimatorListener(
            onStart = { this.isClickable = false },
            onEnd = { this.isClickable = true }
    )
    set.start()
}

Upvotes: 1

Related Questions