Dynamically animate BottomSheet peekHeight
I managed to animate peekHeight
changing using following code:
private fun changePeek(height: Int) {
behavior?.state = BottomSheetBehavior.STATE_HIDDEN
behavior?.peekHeight = height
behavior?.state = BottomSheetBehavior.STATE_COLLAPSED
}
Too late here but someone looking for a simpler solution - Kotlin and ObjectAnimator.
Following will animate the bottomsheet from peekHeight 0dp to R.dimen.login_bottomSheet_peak_height for 1000ms
ObjectAnimator.ofInt(bottomSheetBehavior, "peekHeight",
resources.getDimension(R.dimen.login_bottomSheet_peak_height).toInt())
.apply {
duration = 1000
start()
}
I was able to accomplish this by using RxJava and the Interval operator that runs every 15 milliseconds and changing the peekHeight every time the interval occurred.
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
})