为什么浮动操作按钮在动画后消失?

Why is floating action button disappearing after animation?

我有一个包含多个元素的片段,其中一个是 fab,当我按下另一个 fab 时,我正试图制作一个 fab show/hide。我创建了自己的动画并在 OnClickListener 方法中设置了动画。

动画一结束就出问题了,因为出现的fab马上就消失了,所以我不能用,如果我强制fab上的show()方法,button出现的方法是这样的,但我想使用我自己的动画,自定义比例和持续时间等。

动画xml是这个:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:fromXScale="0.0"
        android:fromYScale="0.0"
        android:toXScale="0.8"
        android:toYScale="0.8"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="300"
        android:interpolator="@android:anim/linear_interpolator">
    </scale>

    <alpha
        android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:duration="300">
    </alpha>
</set>

在片段 onCreateView() 中,我的代码如下所示:

        FloatingActionButton fab1 = view.findViewById(R.id.fab1);
        FloatingActionButton fab2 = view.findViewById(R.id.fab2);
        boolean isVisible = false;
        fab1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!isVisible){
                    fab.startAnimation(AnimationUtils.loadAnimation(getContext(),R.anim.fab_open));
                    //this is for the button to regain clickable property, disabled by default in layout
                    fab.setClickable(true); 
                }
                else{
                    Toast.makeText(getContext(),"Fab already shown",Toast.LENGTH_SHORT).show();
                }
            }
        });

这是布局文件中的工厂:

<com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:clickable="true"
        app:layout_constraintEnd_toEndOf="@+id/textView5"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/erase_96px" />

我以前尝试过这种解决方案,但是对于其他类型的晶圆厂,也许这很重要,过去我用过它并且效果很好:

<android.support.design.widget.FloatingActionButton
        android:id="@+id/myFab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="24dp"
        android:layout_marginRight="24dp"
        android:layout_marginBottom="168dp"
        android:clickable="false"
        android:focusable="false"
        android:visibility="invisible"
        app:backgroundTint="@android:color/white"
        app:layout_anchor="@+id/ScrollViewCambio"
        app:layout_anchorGravity="right|bottom"
        app:layout_constraintBottom_toTopOf="@+id/eraseFab"
        app:layout_constraintEnd_toEndOf="@+id/eraseFab"
        app:layout_constraintStart_toStartOf="@+id/eraseFab"
        app:srcCompat="@drawable/custom25"
        android:tint="@color/colorAccent"/>

只需要更改一项 anim.setFillAfter(正确)。它将保持动画的最终状态。

请参考我下面的代码。

    final Animation anim = AnimationUtils.loadAnimation(getContext(),R.anim.fab_open);
    fab1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(!isVisible){
                        fab.startAnimation(anim);
                        //this is for the button to regain clickable property, disabled by default in layout
                        anim.setFillAfter(true);    
                        fab.setClickable(true); 
                    }
                    else{
                        Toast.makeText(getContext(),"Fab already shown",Toast.LENGTH_SHORT).show();
                    }
                }
            });