Android:如何使用BottomAppBar为FAB创建垂直动画?

时间:2018-10-30 21:30:12

标签: android floating-action-button android-bottomappbar

com.android.support 包中支持库中的

BottomAppBar 小部件具有 isFabAttached 标志和方法。在 28.0.0-alpha1 版本中,它具有公共访问权限。但是发布稳定版本 28.0.0 后,该版本不再可用。

我使用bottomAppBar.isFabAttached = false进行动画分离,使用bottomAppBar.isFabAttached = true进行垂直附加动画。但是在更新到com.android.support:design:28.0.0之后,isFabAttached具有私有访问权限,因此动画无法正常工作。目前仅提供水平动画。

如何为com.android.support:design:28.0.0库的最后发行版制作如上图所示的fab按钮?

enter image description here


更新:问题已解决,我已经从BottomAppBar复制了代码,并在 android.support.design.bottomappbar <中创建了一个自定义视图: CustomBottomAppBar / strong>包并添加了下一个代码:

public void setFabAttached(boolean attached) {
    maybeAnimateAttachChange(attached);
    maybeAnimateMenuView(fabAlignmentMode, attached);
    this.fabAttached = attached;
}

private void maybeAnimateAttachChange(boolean targetAttached) {
    if (fabAttached == targetAttached || !ViewCompat.isLaidOut(this)) {
        return;
    }

    if (attachAnimator != null) {
        attachAnimator.cancel();
    }

    List<Animator> animators = new ArrayList<>();

    createCradleShapeAnimation(targetAttached, animators);
    createFabTranslationYAnimation(targetAttached, animators);

    AnimatorSet set = new AnimatorSet();
    set.playTogether(animators);
    attachAnimator = set;
    attachAnimator.addListener(
            new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    attachAnimator = null;
                }
            });
    attachAnimator.start();
}

0 个答案:

没有答案