android - 平滑地设置 BottomSheet 窥视高度的动画

标签 android animation android-animation bottom-sheet

我正在尝试在layoutChange上设置BottomSheet的高度动画,但我无法让任何东西发挥作用。我尝试了几个教程,但没有成功。

目前高度刚刚确定。我正在使用自定义 BottomSheetDialogFragment()。 我尝试过 TransitionManager.beginDelayedTransition(sheetParent, AutoTransition()) ,其中 sheetParent 显然是我设置中最顶层的布局。它是一个coordinatorLayout。

这是我的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/sheetParent"
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
    android:id="@+id/bottomSheetBehavior"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

    <androidx.core.widget.NestedScrollView

        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/bottomSheetContent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <View
                android:id="@+id/drag_pill"
                android:layout_width="50dp"
                android:layout_height="5dp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="16dp"
                android:layout_marginEnd="16dp"
                android:background="@drawable/bottom_sheet_pill"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />


        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.core.widget.NestedScrollView>
</FrameLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

在普通 View 下方有更多 View ,我将其删除以使其更具可读性。我使用 .isVisible = true/false 扩展和最小化 Bottom Sheet 中的卡片。

我怎样才能顺利地制作动画?

最佳答案

您可以使用 ValueAnimator 在 View 中为您想要更改的任何类型的值设置动画。

例如,在您的情况下,为了平滑地设置 Bottom Sheet 的窥视高度动画,您可以在 BottomSheetDialogFragment() 中使用它:

Kotlin :

val behavior by lazy { (dialog as BottomSheetDialog?)?.behavior } 
val originalHeight = 500 // current height
val updatedHeight = 1000 // desired height

private fun updatePeekHeight(originalHeight: Int, updatedHeight: Int) {
       ValueAnimator.ofInt(originalHeight, updatedHeight).apply {
            addUpdateListener {
                behavior?.peekHeight = it.animatedValue as Int
            }
            duration = 300 // adjust the duration accordingly
            start()
       }
}

关于android - 平滑地设置 BottomSheet 窥视高度的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69890198/

相关文章:

linux - GTK 窗口运动动画?

javascript - 仅使用 JavaScript 创建跳跃动画

android - fragment 在移除和动画结束后闪烁

java - 如何从我的自定义适配器更新 ListView

android - 调试应用程序时发生 ANR 崩溃

java - 如何在不接听电话的情况下调用紧急电话?

android - 切换到轴控件到屏幕按钮控件时动画不起作用

没有 EditText 的 Android 键盘 InputType

android Lollipop 动画故障

android - 如何在点击按钮后以编程方式放大按钮或 View