【问题】
实现可见度为 gone 的 view ,高度从0变化到测量的高度;
如果view 可见度为 gone, 直接通过 view.getHeight();得到的是0;
虽然可以通过一个固定值来设置高度,但是如果高度设置的不准就会存在适配问题(应该有一种方法在gone的情况下也可以获取到view的高度)
import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ValueAnimator
import android.app.Activity
import android.graphics.Point
import android.util.Log
import android.view.View
object AnimatorUtil {
// 高度渐变的动画;
private fun animHeightToView(
v: View, start: Int, end: Int, isToShow: Boolean,
duration: Long
) {
val va = ValueAnimator.ofInt(start, end)
val layoutParams = v.layoutParams
va.addUpdateListener { animation ->
val h = animation.animatedValue as Int
layoutParams.height = h
v.layoutParams = layoutParams
v.requestLayout()
if (isToShow && h > 0) {
v.visibility = View.VISIBLE
}
Log.e("mgg","Animation Animating Current Value $h");
}
va.addListener(object : AnimatorListenerAdapter() {
override fun onAnimationStart(animation: Animator) {
super.onAnimationStart(animation)
}
override fun onAnimationEnd(animation: Animator) {
super.onAnimationEnd(animation)
if (!isToShow) {
v.visibility = View.GONE
}
}
})
va.duration = duration
va.start()
}
fun animHeightToView(
context: Activity,
v: View,
isToShow: Boolean,
duration: Long
) {
if (isToShow) {
// 显示:通过上下文对象context获取可见度属性为 gone 的 view 的高度;
val display = context.windowManager.defaultDisplay
val size = Point()
display.getSize(size)
v.measure(size.x, size.y)
val end = v.measuredHeight
animHeightToView(v, 0, end, isToShow, duration)
} else {
// 隐藏:从当前高度变化到0,最后设置不可见;
animHeightToView(v, v.layoutParams.height, 0, isToShow, duration)
}
}
}
参考
https://blog.csdn.net/ly1414725328/article/details/50916270
网友评论