1 Star 0 Fork 0

xiaow / AndroidPractiseCode

Create your Gitee Account
Explore and code with more than 8 million developers,Free private repositories !:)
Sign up
Clone or Download
AnimatorScrollView.kt 1.98 KB
Copy Edit Web IDE Raw Blame History
xiaowu authored 2021-01-07 16:39 . add animator framework.
package com.github.jxiaow.framework
import android.content.Context
import android.util.AttributeSet
import android.widget.ScrollView
import androidx.core.math.MathUtils.clamp
class AnimatorScrollView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : ScrollView(context, attrs, defStyleAttr) {
private lateinit var content: AnimatorLinearLayout
override fun onFinishInflate() {
super.onFinishInflate()
content = getChildAt(0) as AnimatorLinearLayout
}
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
val first = content.getChildAt(0)
first.layoutParams.height = height
}
override fun onScrollChanged(l: Int, t: Int, oldl: Int, oldt: Int) {
//监听滑动距离--child从下面冒出来多少距离
//需要一个百分比来执行动画,
//百分比为 滑出高度/child实际高度=百分比
//实际高度
for (i in 0 until content.childCount) {
val child = content.getChildAt(i)
val childHeight = child.height
if (child !is DisScrollInterface) {
continue
}
// 采用接口回调的方式,传递百分比
// child 距离 parent 顶部的距离
val childTop = child.top
// 滑出去的距离t
// 距离屏幕的高度
val absTop = childTop - t
if (absTop <= height) {
//child浮现的高度 = ScrollView的高度 - child离屏幕顶部的高度
val visibleGap: Int = height - absTop
//float ratio = child浮现的高度/child的高度
val ratio = visibleGap / childHeight.toFloat()
//确保ratio是在0~1的范围
child.onDisScroll(clamp(ratio, 0f, 1f))
} else {
child.onResetDisScroll()
}
}
}
}

Comment ( 0 )

Sign in to post a comment

Android
1
https://gitee.com/jxiaow/practiseCodeAndroid.git
git@gitee.com:jxiaow/practiseCodeAndroid.git
jxiaow
practiseCodeAndroid
AndroidPractiseCode
master

Search