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

25

我正在使用com.android.support:design:23.1.0库中的浮动操作按钮(Floating Action Button,FAB)组件来创建我的应用程序的FABs。

但是,当我使用fab.hide()隐藏按钮,并在点击按钮后尝试通过fab.show()使图标可见时,第一次加载新活动时没有fab的动画效果。这仅发生在加载新活动后的第一次。当我多次尝试隐藏和显示按钮时,它会正确地进行动画处理。

这里的问题是什么?如果在加载活动后立即获得动画效果,那将是非常好的。

Activity的Java代码:

    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();
            }
        }
    };

布局.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" />
4个回答

55

我曾遇到过同样的问题。在我的fab xml中,我设置了visibility="gone",然后我尝试通过fab.show()从代码中显示fab-但是第一次动画没有起作用。我将xml更改为visibility="invisible",问题得到解决。


4
应该接受这个答案。另外,即使您没有使用visibility="gone",也应该将其设置为visibility="invisible" - Kevin Kopf

8

最终解决了这个问题。我设计了一个新的类来处理带延迟的展开动画。在此处获取该类,请进行初始化,就搞定了。我发现这个动画与标准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;
        }
    });
}

4

最好的方法是在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"/>

0
根据文档:

android.support.design.widget.FloatingActionButton

public void show()

显示按钮。如果视图已经被布局,此方法将会动画显示按钮

因此,为了使其在第一次动画时可以编写自己的动画来使其动画化,当它当前没有被布局时进行动画化。
/**
 * 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());
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接