简体   繁体   English

为什么我的 FAB animation 会出现 Material BottomAppBar 的屏幕?

[英]why is my FAB animation going out of screen with Material BottomAppBar?

So i got this working and it's great所以我得到了这个工作,它很棒

在此处输入图像描述

Then i tried to set the parent FAB on the right with:然后我尝试在右侧设置父FAB:

binding.bottomAppBar.fabAlignmentMode = BottomAppBar.FAB_ALIGNMENT_MODE_END

It works but now the parent FAB animation goes out of screen.它可以工作,但现在父 FAB animation 退出屏幕。

在此处输入图像描述

Here is a better look of what is going on:下面是对正在发生的事情的更好了解:

在此处输入图像描述

Open animation:打开 animation:

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<rotate
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="45"
    android:duration="300" />

Close Animation:关闭 Animation:

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<rotate
    android:fromDegrees="45"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="0"
    android:duration="300"
    />

Activity XML:活动 XML:

    <androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bottomAppBar"
        android:layout_gravity="bottom"
        style="@style/Widget.MaterialComponents.BottomAppBar.Colored"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:fabAlignmentMode="end"/>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/addFav"
        app:layout_anchor="@id/bottomAppBar"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

How can i fix this?我怎样才能解决这个问题? The child FABs animations work as intended.子 FAB 动画按预期工作。

Here is what's happening.这是正在发生的事情。

  1. When the XML is inflated, the FAB is at the center.当 XML 充气时,FAB 位于中心。 So pivotX is now center of the screen.所以pivotX现在是屏幕的中心。
  2. You set fabAlignmentMode programmatically in Java/Kotlin after inflation.您在膨胀后以编程方式在 Java/Kotlin 中设置fabAlignmentMode
  3. When rotation starts, pivotX is still using the value from inflation point and hence using old value.当旋转开始时, pivotX仍然使用膨胀点的值,因此使用旧值。
  4. The FAB is rotated about the center of its previous location and hence goes out of screen. FAB 围绕其先前位置的中心旋转,因此离开屏幕。 Notice the curve in the motion of the FAB in the second GIF.请注意第二个 GIF 中 FAB 的运动曲线。

Setting the alignment in XML using fabAlignmentMode should resolve your issue.使用 fabAlignmentMode 在 XML 中设置fabAlignmentMode应该可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM