fab.show() 在初始化新 activity 后第一次没有动画

fab.show() not animated first time after initializing new activity

我正在使用 com.android.support:design:23.1.0 库中的浮动操作按钮 (fab) 组件来生成我的应用程序的 fab。

但是我第一次使用 fab.hide() 加载新的 activity 并尝试在单击按钮后通过 fab.show() 使图标可见,没有工厂的动画。这仅在加载新 activity 后第一次发生。当我多次尝试隐藏和显示按钮时,它会正确设置动画。

这里有什么问题?在 activity 加载后立即将其动画化将是一种魅力。

Java 在 activity:

    fabSend = (FloatingActionButton) findViewById(R.id.fabSend);
    fabSend.hide();    


CompoundButton.OnCheckedChangeListener changeChecker = new CompoundButton.OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked){
//                FAB on
                fabSend.show();
            }   else {
//                FAB off
                fabSend.hide();
            }
        }
    };

Layout.xml

            <android.support.design.widget.FloatingActionButton
                android:id="@+id/fabSend"
                app:borderWidth="0dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end"
                android:layout_alignParentBottom="true"
                android:layout_marginRight="@dimen/fab_margin"
                android:layout_marginBottom="54dp"
                android:src="@drawable/ic_check_white_24dp" />

终于解决了这个问题。我设计了一个新的 class 来延迟显示动画。在这里抓住它,初始化它,你很高兴。我发现一个与标准 fab.show() 非常相似的动画,延迟 50 毫秒。

    public static void showFabWithAnimation(final FloatingActionButton fab, final int delay) {
    fab.setVisibility(View.INVISIBLE);
    fab.setScaleX(0.0F);
    fab.setScaleY(0.0F);
    fab.setAlpha(0.0F);
    fab.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            fab.getViewTreeObserver().removeOnPreDrawListener(this);
            fab.postDelayed(new Runnable() {
                @Override
                public void run() {
                    fab.show();
                }
            }, delay);
            return true;
        }
    });
}

根据文档:

android.support.design.widget.FloatingActionButton

public void show()

Shows the button. This method will animate the button show if the view has already been laid out.

所以,要让它在第一次动画时可以编写自己的动画,在它当前未布局时使其动画化

/**
 * Unlike {@link FloatingActionButton#show()} animates button even it not currently
 * laid out
 * @param fab fab to show
 */
@SuppressLint("NewApi")
public static void show(FloatingActionButton fab) {
    if (ViewCompat.isLaidOut(fab) ||
            Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        fab.show();
    } else {
        fab.animate().cancel();//cancel all animations
        fab.setScaleX(0f);
        fab.setScaleY(0f);
        fab.setAlpha(0f);
        fab.setVisibility(View.VISIBLE);
        //values from support lib source code
        fab.animate().setDuration(200).scaleX(1).scaleY(1).alpha(1)
                .setInterpolator(new LinearOutSlowInInterpolator());
    }
}

我遇到了同样的问题。在我的 fab xml 中,我有 visibility="gone",而不是我试图通过 fab.show() 从代码中显示 fab - 动画第一次没有工作。我已将 xml 更改为 visibility="invisible",问题已解决。

实现这一目标的最佳方法是在 XML 中将 Fab 的 scaleX 和 scaleY 设置为零。这是最简单的方法,而且还能使您的应用程序代码保持整洁。

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:scaleX="0"
    android:scaleY="0"
    android:visibility="invisible"/>