【问题标题】:Why is floating action button disappearing after animation?为什么浮动动作按钮在动画后消失?
【发布时间】:2019-11-21 03:09:02
【问题描述】:

我有一个包含多个元素的片段,其中一个是晶圆厂,当我按下另一个晶圆厂时,我试图让晶圆厂显示/隐藏。我已经创建了自己的动画并在 OnClickListener 方法中设置了动画。

问题是在动画结束的时候出现的,因为出现的fab马上就消失了,所以我不能用,如果我强制fab上的show()方法,按钮就会按照方法的工作方式出现,但是我想使用自己的动画,自定义比例和持续时间等。

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

这是布局文件中的fab:

<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"/>

【问题讨论】:

    标签: android android-fragments android-animation floating-action-button


    【解决方案1】:

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

    请参考我下面的代码。

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

    【讨论】:

    • 谢谢,我在动画定义之后添加了 setFillAfter(true),因为我的应用程序比显示的示例稍微复杂一些,我多次使用动画,但它按预期工作.
    • 太好了,我很高兴该解决方案对您有所帮助:)
    猜你喜欢
    • 2018-01-31
    • 2015-03-11
    • 1970-01-01
    • 1970-01-01
    • 2015-07-09
    • 2015-11-08
    • 2015-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多