是否可以控制具有wrap_content
的视图的宽度动画?现在,我有一个TextView
,其右侧有障碍物。我还有另一个与此障碍相关的背景View
。当TextView
更改文本时,它会闪烁到新的宽度,但是我想用一个动画师为该宽度设置动画,在其中我可以控制时间,内插器等。我知道可以设置类似animateLayoutChanges
的东西效果是不可定制的。
答案 0 :(得分:1)
您可以将ObjectAnimator
和AnimatorSet
一起用于动画。
在extension
中有一个kotlin
中的view
,您要在其上应用动画。
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()
}