动态动画 BottomSheet peekHeight
Dynamically animate BottomSheet peekHeight
有没有人设法为 BottomSheet peekHeight 设置动画?
我目前在做以下事情:
bottom_sheet?.let {
val behavior = BottomSheetBehavior.from(bottom_sheet)
behavior.peekHeight = 500
behavior.state = BottomSheetBehavior.STATE_COLLAPSED
}
bottomsheet peek 高度已更改,但我想要一个动画,例如当您将状态从 STATE_COLLAPSED
更改为 STATE_EXPANDED
时。
我能够通过使用 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
})
我设法使用以下代码为 peekHeight
变化设置动画:
private fun changePeek(height: Int) {
behavior?.state = BottomSheetBehavior.STATE_HIDDEN
behavior?.peekHeight = height
behavior?.state = BottomSheetBehavior.STATE_COLLAPSED
}
@cora32 的回答是正确的;
我只是要在他们的回答中添加一些内容。如果您阻止底部 sheet 隐藏(例如 app:behavior_hideable="false"
),使用该代码您将获得非法状态异常。
要让您的 peekHeight
在任何情况下都充满活力,您只需执行以下操作:
BottomSheetBehavior.from(bottomSheetView)?.apply {
state = BottomSheetBehavior.STATE_EXPANDED
peekHeight = newHeight
state = BottomSheetBehavior.STATE_COLLAPSED
}
我想我已经通过使用 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);
}
这里太晚了,但有人在寻找更简单的解决方案 - Kotlin 和 ObjectAnimator。
以下将为底页设置动画
从 peekHeight 0dp
到R.dimen.login_bottomSheet_peak_height
1000 毫秒
ObjectAnimator.ofInt(bottomSheetBehavior, "peekHeight",
resources.getDimension(R.dimen.login_bottomSheet_peak_height).toInt())
.apply {
duration = 1000
start()
}
有没有人设法为 BottomSheet peekHeight 设置动画?
我目前在做以下事情:
bottom_sheet?.let {
val behavior = BottomSheetBehavior.from(bottom_sheet)
behavior.peekHeight = 500
behavior.state = BottomSheetBehavior.STATE_COLLAPSED
}
bottomsheet peek 高度已更改,但我想要一个动画,例如当您将状态从 STATE_COLLAPSED
更改为 STATE_EXPANDED
时。
我能够通过使用 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
})
我设法使用以下代码为 peekHeight
变化设置动画:
private fun changePeek(height: Int) {
behavior?.state = BottomSheetBehavior.STATE_HIDDEN
behavior?.peekHeight = height
behavior?.state = BottomSheetBehavior.STATE_COLLAPSED
}
@cora32 的回答是正确的;
我只是要在他们的回答中添加一些内容。如果您阻止底部 sheet 隐藏(例如 app:behavior_hideable="false"
),使用该代码您将获得非法状态异常。
要让您的 peekHeight
在任何情况下都充满活力,您只需执行以下操作:
BottomSheetBehavior.from(bottomSheetView)?.apply {
state = BottomSheetBehavior.STATE_EXPANDED
peekHeight = newHeight
state = BottomSheetBehavior.STATE_COLLAPSED
}
我想我已经通过使用 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);
}
这里太晚了,但有人在寻找更简单的解决方案 - Kotlin 和 ObjectAnimator。
以下将为底页设置动画 从 peekHeight 0dp 到R.dimen.login_bottomSheet_peak_height 1000 毫秒
ObjectAnimator.ofInt(bottomSheetBehavior, "peekHeight",
resources.getDimension(R.dimen.login_bottomSheet_peak_height).toInt())
.apply {
duration = 1000
start()
}