动态动画BottomSheet peekHeight

Amb*_*mbi 6 animation android kotlin bottom-sheet

有没有人设法动画BottomSheet peekHeight?

我目前正在做以下事情:

        bottom_sheet?.let {
            val behavior = BottomSheetBehavior.from(bottom_sheet)
            behavior.peekHeight = 500
            behavior.state = BottomSheetBehavior.STATE_COLLAPSED
        }
Run Code Online (Sandbox Code Playgroud)

该bottomsheet偷看高度改变,但我想有一个动画,当你从改变国家像STATE_COLLAPSEDSTATE_EXPANDED.

小智 5

我能够通过使用RxJava和每15毫秒运行一次的Interval运算符并在每次发生间隔时更改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
                })
Run Code Online (Sandbox Code Playgroud)


Adi*_*lil 5

为时已晚,但有人正在寻找更简单的解决方案 - Kotlin 和 ObjectAnimator。

以下将把底片 从 peekHeight 0dp动画 到 R.dimen.login_bottomSheet_peak_height 1000ms

ObjectAnimator.ofInt(bottomSheetBehavior, "peekHeight",
            resources.getDimension(R.dimen.login_bottomSheet_peak_height).toInt())
            .apply {
                duration = 1000
                start()
            }
Run Code Online (Sandbox Code Playgroud)