有人设法为 BottomSheet peekHeight 设置动画吗?
我目前正在做以下事情:
bottom_sheet?.let {
val behavior = BottomSheetBehavior.from(bottom_sheet)
behavior.peekHeight = 500
behavior.state = BottomSheetBehavior.STATE_COLLAPSED
}
Bottom Sheet 窥视高度已更改,但我想要一个动画,例如当您将状态从 STATE_COLLAPSED
更改为 STATE_EXPANDED
时。
最佳答案
我能够通过使用 RxJava 和 Interval 来完成这个任务。每 15 毫秒运行一次并在每次间隔发生时更改 peekHeight 的运算符。
val interpolator = AccelerateDecelerateInterpolator()
val refreshInterval = 20L
val refreshCount = 15L
val totalRefreshTime = (refreshInterval * refreshCount).toFloat()
val startingHeight = bottomSheetBehavior.peekHeight
animationDisposable = Observable.interval(refreshInterval, TimeUnit.MILLISECONDS)
.take(refreshCount)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ count: Long ->
if (show) {
val height = (startingHeight + (maxPeekHeight - minPeekHeight) *
interpolator.getInterpolation((count * refreshInterval) / totalRefreshTime)).toInt()
if (height > maxPeekHeight) {
bottomSheetBehavior.peekHeight = maxPeekHeight
} else {
bottomSheetBehavior.peekHeight = height
}
} else {
val height = (startingHeight - (maxPeekHeight - minPeekHeight) *
interpolator.getInterpolation((count * refreshInterval) / totalRefreshTime)).toInt()
if (height < minPeekHeight) {
bottomSheetBehavior.peekHeight = minPeekHeight
} else {
bottomSheetBehavior.peekHeight = height
}
}
}, { _: Throwable ->
//do something here to reset peek height to original
})
关于android - 动态地为 BottomSheet peekHeight 设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47080589/