有没有人设法为BottomSheet peekHeight设置动画?
我目前正在执行以下操作:
bottom_sheet?.let {
val behavior = BottomSheetBehavior.from(bottom_sheet)
behavior.peekHeight = 500
behavior.state = BottomSheetBehavior.STATE_COLLAPSED
}
更改了底部的偷看高度,但我希望有一个动画,就像您将状态从STATE_COLLAPSED
更改为STATE_EXPANDED
一样。
答案 0 :(得分: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
})
答案 1 :(得分:2)
我设法使用以下代码更改peekHeight
更改动画:
private fun changePeek(height: Int) {
behavior?.state = BottomSheetBehavior.STATE_HIDDEN
behavior?.peekHeight = height
behavior?.state = BottomSheetBehavior.STATE_COLLAPSED
}
答案 2 :(得分:2)
@ cora32的答案是正确的;
我要在他们的答案中添加一些内容。如果您阻止隐藏底页(例如,使用app:behavior_hideable="false"
),则使用该代码将获得非法状态异常。
要在各种情况下为自己的peekHeight
制作动画,您可以执行以下操作:
BottomSheetBehavior.from(bottomSheetView)?.apply {
state = BottomSheetBehavior.STATE_EXPANDED
peekHeight = newHeight
state = BottomSheetBehavior.STATE_COLLAPSED
}
答案 3 :(得分:1)
我认为我已经通过使用ValueAnimator(C#)使它起作用了
private BottomSheetBehavior bsh;
public void AnimToNewHeight(int newHeight)
{
LinearLayout bottomSheet = (LinearLayout)FindViewById(Resource.Id.bottom_sheet);
BottomSheetBehavior behavior = BottomSheetBehavior.From(bottomSheet);
bsh = behavior;
ValueAnimator anim = new ValueAnimator();
anim.SetIntValues(bsh.PeekHeight,newHeight);
anim.SetDuration(300);
anim.Update += Anim_Update;
anim.Start();
}
private void Anim_Update(object sender, ValueAnimator.AnimatorUpdateEventArgs e)
{
float f = e.Animation.AnimatedFraction;
bsh.PeekHeight =(int)(e.Animation.AnimatedValue);
}
答案 4 :(得分:0)
在这里为时已晚,但是有人在寻找更简单的解决方案-Kotlin和ObjectAnimator。
以下内容将对底页进行动画处理 来自peekHeight 0dp 访问R.dimen.login_bottomSheet_peak_height 持续1000ms
Access-Control-Allow-Origin: https://localhost:3000