0
点赞
收藏
分享

微信扫一扫

Android 自定义View之展开收起的Layout,劲爆

吃面多放酱 2022-01-12 阅读 54

//遍历计算高度

for (index in 0 until childCount) {

//这个地方实际使用中除了measuredHeight,以及margin等,也要计算在内

if (index == 0) {

firstChildHeight = getChildAt(index).measuredHeight

+getChildAt(index).marginTop + getChildAt(index).marginBottom

+this.paddingTop + this.paddingBottom

}

//实际使用时或包括padding等

allChildHeight += getChildAt(index).measuredHeight + getChildAt(index).marginTop + getChildAt(index).marginBottom

//最后一条的时候 加上当前view自身
的padding

if (index == childCount - 1) {

allChildHeight += this.paddingTop + this.paddingBottom

}

}

// 根据是否展开设置高度

if (isOpen) {

setMeasuredDimension(

widthMeasureSpec,

firstChildHeight + ((allChildHeight - firstChildHeight) * animPercent).toInt()

)

} else {

setMeasuredDimension(

widthMeasureSpec,

allChildHeight - ((allChildHeight - firstChildHeight) * animPercent).toInt()

)

}

}

}

onMeasure里面也是分了两个步骤的:

  • 遍历计算高度

//遍历计算高度

for (index in 0 until childCount) {

//这个地方实际使用中除了measuredHeight,以及margin等,也要计算在内

if (index == 0) {

firstChildHeight = getChildAt(index).measuredHeight

+getChildAt(index).marginTop + getChildAt(index).marginBottom

+this.paddingTop + this.paddingBottom

}

//实际使用时或包括padding等

allChildHeight += getChildAt(index).measuredHeight + getChildAt(index).marginTop + getChildAt(index).marginBottom

//最后一条的时候 加上当前view自身的padding

if (index == childCount - 1) {

allChildHeight += this.paddingTop + this.paddingBottom

}

}

来看第一个if判断,记录了第一个子view的高度,这里需要注意,除了measuredHeightmargin也要算上,而且父view的内边距padding也要加上,因为如果父view的padding很大的话,收起时view可能会显示不出来的。

然后就是总高度的计算,道理同上。

来看最后一个if判断,同样总高度计算完之后也要加上父view的上下padding,才是完整的高度。

第一个判断可以理解为收起状态的高度,第二个判断可以理解为展开状态的高度。

  • 展开收起逻辑

// 根据是否展开设置高度

if (isOpen) {

setMeasuredDimension(

widthMeasureSpec,

firstChildHeight + ((allChildHeight - firstChildHeight) * animPercent).toInt()

)

} else {

setMeasuredDimension(

widthMeasureSpec,

allChildHeight - ((allChildHeight - firstChildHeight) * animPercent).toInt()

)

}

因为第一个子view是保留显示的,所以在计算的时候都需要减去第一个子view的高度,就是剩余高度

剩余高度可以很简单的计算出来,但是如何在显示的时候不突兀呢。

这里加一个动画,根据动画的执行进度来计算。

展开:第一个子view的高度 + 剩余高度 × 0到1的Float动画值

收起:总高度 - 剩余高度 × 0到1的Float动画值

动画

写一个方法控制展开收起,并在展开收起的时候执行动画。

fun toggle(): Boolean {

isOpen = !isOpen

startAnim()

return isOpen

}

/**

  • 执行动画的时候 更改 animPercent 属性的值 即从0-1

*/

@SuppressLint(“AnimatorKeep”)

private fun startAnim() {

//ofFloat,of xxxX 根据参数类型来确定

//1,动画对象,即当前view。2.动画属性名。3,起始值。4,目标值。

val animator = ObjectAnimator.ofFloat(this, “animPercent”, 0f, 1f)

animator.duration = 500

animator.start()

}

并修改我们的动画参数:

/**

  • 动画值改变的时候 请求重新布局

*/

private var animPercent: Float = 0f

set(value) {

field = value

requestLayout()

}

set value的时候调用requestLayout(),重新执行onMeasure

调用

  • xml

<com.yechaoa.customviews.expand.ExpandLinearLayout

android:id="@+id/ell"

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:background="#f5f5f5"

android:orientation=“vertical”

android:padding=“10dp”>

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:padding=“10dp”

android:text="@string/app_name"

android:textColor="@android:color/holo_red_dark"

android:textSize=“20sp” />

</com.yechaoa.customviews.expand.ExpandLinearLayout>

  • 代码

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:padding=“10dp”

android:text="@string/app_name"

android:textColor="@android:color/holo_red_dark"

android:textSize=“20sp” />

</com.yechaoa.customviews.expand.ExpandLinearLayout>

  • 代码
举报

相关推荐

0 条评论