android - 动态地为 BottomSheet peekHeight 设置动画

标签 android animation kotlin bottom-sheet

有人设法为 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/

相关文章:

android - 无法在android studio中同步firebase

android - 使用 Ktor 反序列化 Json 数组的问题

android - 如何在 AnimatorSet playSequentially() 中的动画之间添加延迟?

WPF 加载动画

html - 只有 SCSS 的打字机效果(宽度动画)

serialization - Kotlin 中的单例序列化

android - 在 Kotlin 中是否有更干净的方法来设置日期时间

java - HttpUrlConnection 不起作用并显示 GET 和 POST 调用的不同状态代码

java - 电话号码链接不可点击android

android - 如何处理来自 Google Play 的待处理购买