gpt4 book ai didi

android - BottomSheet 动画持续时间

转载 作者:行者123 更新时间:2023-12-03 10:11:41 40 4
gpt4 key购买 nike

我正在为我的一种布局使用 BottomSheet View 。对于一个非常具体的情况,我需要 BottomSheet 的动画持续时间,它通过 setState(...) 方法在布局中滑动。它应该在 400 毫秒左右,但我不希望在这种情况下使用“神奇”数字。

最佳答案

BottomSheetBehavior 的持续时间由 setState 触发的动画或触摸事件取决于各种条件。

BottomSheetBehavior 内部使用 ViewDragHelper这决定了 computeAxisDuration() 内的动画持续时间私有(private)方法。这个持续时间取决于一些任意值,不能通过官方 API 更改:

public class ViewDragHelper {
...
private static final int BASE_SETTLE_DURATION = 256; // ms
private static final int MAX_SETTLE_DURATION = 6000; // ms
...
}

更改显示/隐藏动画时间的 hack 解决方案是...

  • 将整个 BottomSheetBehaviorViewDragHelper 类源代码复制到您的项目中(使用不同的包名称)
  • 在您的文件 BottomSheetBehavior.java 中 - 更改 import ViewDragHelper 以使用您的 ViewDragHelper 而不是原始的 Android API 类。
  • 在文件 ViewDragHelper.java 中 - 更改 computeAxisDuration 返回的持续时间以满足您的要求,例如正好返回 400 毫秒。
  • 使用您的 BottomSheetBehavior 类代替原来的类。

这是一个非常糟糕的解决方案,但它在我的项目中有效。详情如下。

文件ViewDragHelper.java

public class ViewDragHelper {
// Additional fields and methods defined.
private int mSettleDuration = BASE_SETTLE_DURATION;
public void setDurationSpeedFactor(final float factor) {
mSettleDuration = (int)(factor * BASE_SETTLE_DURATION);
}
private boolean mSkipAnimation = false;
public void setSkipAnimation(final boolean skipAnimation) {
this.mSkipAnimation = skipAnimation;
}
// Modified version of private function.
private int computeAxisDuration(int delta, int velocity, int motionRange) {
if (delta == 0) {
return 0;
}

final int width = mParentView.getWidth();
final int halfWidth = width / 2;
final float distanceRatio = Math.min(1f, (float) Math.abs(delta) / width);
final float distance = halfWidth + halfWidth
* distanceInfluenceForSnapDuration(distanceRatio);

int duration;
velocity = Math.abs(velocity);
if (velocity > 0) {
duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
} else {
final float range = (float) Math.abs(delta) / motionRange;
duration = (int) ((range + 1) * mSettleDuration);
}
return Math.min(duration, MAX_SETTLE_DURATION);
}
// Rest of original class body here...
}

文件 BottomSheetBehavior.java

// Comment out original ViewDragHelper and use above modified version.
//import androidx.customview.widget.ViewDragHelper;
import your.custom.ViewDragHelper;

public class BottomSheetBehavior<V extends View> extends CoordinatorLayout.Behavior<V> {

/**
* Allows to set up ViewDragHelper immediately or when it is created.
*/
public interface IDragHelperConfig {
void onDragHelperCreated(ViewDragHelper viewDragHelper);
}
private IDragHelperConfig iDragHelperConfig;
public void setIDragHelperConfig(IDragHelperConfig iDragHelperConfig) {
this.iDragHelperConfig = iDragHelperConfig;
if (viewDragHelper != null) {
iDragHelperConfig.onDragHelperCreated(viewDragHelper);
}
}
//...
public boolean onLayoutChild(...) {
if (viewDragHelper == null) {
viewDragHelper = ViewDragHelper.create(parent, dragCallback);
if (iDragHelperConfig != null) {
iDragHelperConfig.onDragHelperCreated(viewDragHelper);
}
}
}
//...
}

任何android View 初始化中的示例代码

    val params: CoordinatorLayout.LayoutParams = myLayout.layoutParams as CoordinatorLayout.LayoutParams
val behavior = BottomSheetBehavior<FrameLayout>(myLayout.context, null)
// Optionally, set the height of the bottom sheet when it is collapsed (usefull for experiments).
// behavior.peekHeight = 20

// Configure bottom sheet behavior.
behavior.setIDragHelperConfig {
// Set how many times to slow the speed of the animation
// (relative to original Android animation speed).
it.setDurationSpeedFactor(20f)

// Optionally, turn off animation
// Useful when entering a new activity and
// initial animation is not desired.
it.setSkipAnimation(false)
}
params.behavior = behavior

关于android - BottomSheet 动画持续时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51720891/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com