android-fragments 使用浮动动作按钮的材质容器变换动画工作不顺畅

igetnqfo  于 2022-11-13  发布在  Android
关注(0)|答案(1)|浏览(133)

我正在尝试打开一个片段,通过点击浮动动作按钮使用材料容器转换动画。我已经实现了动画,但它不是我预期的工作。

当从一个片段A导航到片段B时,当片段B被打开时,浮动动作按钮仍然可见(放大了一小段时间)。
我在动画中做了哪些更改,使从浮动动作按钮到片段B的过渡感觉更平滑?***或***当导航到片段B时,我如何隐藏放大的浮动动作按钮?以下是片段和布局的代码,供参考。

片段A布局:

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".ui.home.HomeFragment">

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/create_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:transitionName="my_transition"
    android:src="@drawable/ic_round_add_24"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/dimen_16"
    android:contentDescription="New Task"
</androidx.coordinatorlayout.widget.CoordinatorLayout>

片段A代码:

class FragmentA : Fragment() {

private var _binding: FragmentABinding? = null
private val binding get() = _binding!!

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    exitTransition = Hold()
}

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View {
    _binding = FragmentABinding.inflate(inflater)

    binding.createTask.setOnClickListener {
        val extras = FragmentNavigatorExtras(binding.createTask to "my_transition")
        findNavController().navigate(R.id.action_FragmentA_to_FragmentB,null,null,extras)
    }

    return binding.root
}

override fun onDestroyView() {
    super.onDestroyView()
    _binding = null
}

}

片段B布局:

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:transitionName="my_transition"
tools:context=".ui.edit.EditTaskFragment">

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="@dimen/toolbar_size"
    app:liftOnScroll="true">
    <FrameLayout
        android:layout_width="match_parent"
        android:paddingHorizontal="@dimen/dimen_16"
        android:layout_height="match_parent">
        <ImageButton
            android:id="@+id/close"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_baseline_close_24"
            android:background="?attr/selectableItemBackgroundBorderless"
            android:layout_gravity="start|center_vertical"
            app:tint="?attr/colorPrimary" />

        <com.google.android.material.button.MaterialButton
            android:id="@+id/save"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end|center_vertical"
            style="@style/Widget.Material3.Button.TonalButton"
            android:text="Save"
            android:minWidth="0dp"
            android:minHeight="0dp"/>
    </FrameLayout>
</com.google.android.material.appbar.AppBarLayout>

<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="@style/TextAppearance.Material3.HeadlineSmall"
        android:textAlignment="center"
        android:text="This is new Fragment"/>

</androidx.core.widget.NestedScrollView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

片段B代码:

class FragmentB : Fragment() {

private var _binding: FragmentBBinding? = null
private val binding get() = _binding!!

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    sharedElementEnterTransition = MaterialContainerTransform()
}

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View {
    _binding = FragmentBBinding.inflate(inflater)

    return binding.root
}

override fun onDestroyView() {
    super.onDestroyView()
    _binding = null
}

}
ygya80vv

ygya80vv1#

解决了问题!!只需将materialContainerTransform中的startContainerColor和endContainerColor参数设置为表面颜色,然后工厂过渡将更加平滑
将片段B中的sharedElementEnterTransition替换为:

sharedElementEnterTransition = MaterialContainerTransform().apply {
        startContainerColor = requireContext().getColorFromAttr(com.google.android.material.R.attr.colorSurface)
        endContainerColor = requireContext().getColorFromAttr(com.google.android.material.R.attr.colorSurface)
    }

相关问题