Android BottomSheet弹跳动画

发布于 2025-01-16 22:51:39 字数 3871 浏览 2 评论 0 原文

输入图像此处描述

我想在执行 STATE_EXPANDED 或 STATE_COLLAPSED 时弹出效果,如上图所示,

我想要应用 BounceInterpolator 的动画。

下面的代码是我做的一个例子。

Activity_main.xml

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

<com.google.android.material.button.MaterialButton
    android:id="@+id/main_button"
    android:layout_width="200dp"
    android:layout_height="60dp"
    android:layout_gravity="center_horizontal"
    android:text="click me !!"/>

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/main_bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:background="@color/design_default_color_primary"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
    app:behavior_hideable="false"
    app:behavior_peekHeight="50dp">

    <TextView
        android:id="@+id/main_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"
        android:layout_marginTop="10dp"
        android:text="BottomSheetTitle!!"
        android:textColor="@color/white"
        app:layout_constraintTop_toTopOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

MainActivity.kt

class MainActivity : AppCompatActivity() {

val mainButton: MaterialButton by lazy { findViewById(R.id.main_button) }
val bottomSheet: ConstraintLayout by lazy { findViewById(R.id.main_bottom_sheet) }
val bottomSheetBehavior: BottomSheetBehavior<ConstraintLayout> by lazy { BottomSheetBehavior.from(bottomSheet) }

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    mainButton.setOnClickListener {
        when (bottomSheetBehavior.state) {
            (BottomSheetBehavior.STATE_COLLAPSED) -> {
                bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
            }
            (BottomSheetBehavior.STATE_EXPANDED) -> {
                bottomSheetBehavior.state = BottomSheetBehavior.STATE_COLLAPSED
            }
        }
    }
}
}

在此处输入图像描述

没有跳出。

有没有应用弹跳动画的解决方案?

1--------------------------------1

我使用下面的代码解决了

我认为这个解决方案不好

答案如果您有更好的解决方案,请随时

bottomSheetBehavior.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
        override fun onStateChanged(bottomSheet: View, newState: Int) {
            if (newState == BottomSheetBehavior.STATE_SETTLING) {
                bottomSheet.animate()
                    .translationY(0f)
            }
            if (newState == BottomSheetBehavior.STATE_EXPANDED) {
                bottomSheet.animate()
                    .setDuration(200)
                    .translationY(20f)
            }
            if (newState == BottomSheetBehavior.STATE_COLLAPSED) {
                bottomSheet.animate()
                    .setDuration(200)
                    .translationY(-20f)
            }
        }

        override fun onSlide(bottomSheet: View, slideOffset: Float) {
        }
    })

在此处输入图像描述

enter image description here

I want to bounce effect when doing STATE_EXPANDED or STATE_COLLAPSED like above image

I want an animation with BounceInterpolator applied.

The code below is an example I made.

activity_main.xml

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

<com.google.android.material.button.MaterialButton
    android:id="@+id/main_button"
    android:layout_width="200dp"
    android:layout_height="60dp"
    android:layout_gravity="center_horizontal"
    android:text="click me !!"/>

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/main_bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:background="@color/design_default_color_primary"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
    app:behavior_hideable="false"
    app:behavior_peekHeight="50dp">

    <TextView
        android:id="@+id/main_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"
        android:layout_marginTop="10dp"
        android:text="BottomSheetTitle!!"
        android:textColor="@color/white"
        app:layout_constraintTop_toTopOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

MainActivity.kt

class MainActivity : AppCompatActivity() {

val mainButton: MaterialButton by lazy { findViewById(R.id.main_button) }
val bottomSheet: ConstraintLayout by lazy { findViewById(R.id.main_bottom_sheet) }
val bottomSheetBehavior: BottomSheetBehavior<ConstraintLayout> by lazy { BottomSheetBehavior.from(bottomSheet) }

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    mainButton.setOnClickListener {
        when (bottomSheetBehavior.state) {
            (BottomSheetBehavior.STATE_COLLAPSED) -> {
                bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
            }
            (BottomSheetBehavior.STATE_EXPANDED) -> {
                bottomSheetBehavior.state = BottomSheetBehavior.STATE_COLLAPSED
            }
        }
    }
}
}

enter image description here

there is no bounce.

Is there any solution to apply bounce animation?

1----------------------------------1

I solved using below code

I don't think this solution good

Answer please anytime if you have better good solution

bottomSheetBehavior.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
        override fun onStateChanged(bottomSheet: View, newState: Int) {
            if (newState == BottomSheetBehavior.STATE_SETTLING) {
                bottomSheet.animate()
                    .translationY(0f)
            }
            if (newState == BottomSheetBehavior.STATE_EXPANDED) {
                bottomSheet.animate()
                    .setDuration(200)
                    .translationY(20f)
            }
            if (newState == BottomSheetBehavior.STATE_COLLAPSED) {
                bottomSheet.animate()
                    .setDuration(200)
                    .translationY(-20f)
            }
        }

        override fun onSlide(bottomSheet: View, slideOffset: Float) {
        }
    })

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素年丶 2025-01-23 22:51:39

该动画称为overshoot。这是我使用 setWindowsAnimation(res int style) 的尝试:

mainBottomSheet.getWindow().setWindowAnimations(R.style.SlideAnimation);

并在 styles.xml 中添加您最喜欢的动画,如下所示:

<style name="SlideAnimation" parent="Animation.AppCompat.Dialog">
      <item name="android:windowEnterAnimation">@anim/slide_in</item>
      <item name="android:windowExitAnimation">@anim/slide_out</item>
</style>

res/anime< 中添加以下文件/code> 目录:

<!-- slide_in.xml -->
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:duration="700"
     android:interpolator="@anim/interpolator_slight_overshoot">

    <translate
        android:fromYDelta="-100%"
        android:toYDelta="0%"/>

</set>

其重要部分是插值器,它可以根据需要制作动画:

<!-- interpolator_slight_overshoot.xml -->
<?xml version="1.0" encoding="utf-8"?>
<overshootInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
                       android:tension="1.0"/>

免责声明: 动画来源:警报库

This animation is called overshoot. This is my try using setWindowsAnimation(res int style):

mainBottomSheet.getWindow().setWindowAnimations(R.style.SlideAnimation);

and in styles.xml add your favorite animations as follow:

<style name="SlideAnimation" parent="Animation.AppCompat.Dialog">
      <item name="android:windowEnterAnimation">@anim/slide_in</item>
      <item name="android:windowExitAnimation">@anim/slide_out</item>
</style>

Add the following files in res/anime directory:

<!-- slide_in.xml -->
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:duration="700"
     android:interpolator="@anim/interpolator_slight_overshoot">

    <translate
        android:fromYDelta="-100%"
        android:toYDelta="0%"/>

</set>

and its important part that makes animation as you want, is the interpolator:

<!-- interpolator_slight_overshoot.xml -->
<?xml version="1.0" encoding="utf-8"?>
<overshootInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
                       android:tension="1.0"/>

Disclaimer: Source of animations: Alerter library

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文