将布局滑动到屏幕中间(向上滑动),然后向上或向下滑动

Swipe layout to the middle of the screen(slide up), then swipe up or down

需要帮助创建类似于 google 地图

中的 探索 按钮的布局
  1. 通过点击屏幕底部的按钮,面板从底部出现到屏幕的~中间(包括动画)

  2. 然后需要提供向上或向下滑动面板的能力

我差不多通过MotionLayout(alpha-2)搞定了,换个方案也不错

<MotionScene
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <Transition
        app:constraintSetStart="@+id/start"
        app:constraintSetEnd="@+id/end"
        app:duration="1000"
        app:interpolator="linear" />

    <OnSwipe
        app:touchAnchorId="@+id/container"
        app:touchAnchorSide="top"
        app:dragDirection="dragUp" />

    <ConstraintSet android:id="@+id/start">
        <Constraint
            android:id="@+id/container"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent">
            <CustomAttribute
                app:attributeName="BackgroundColor"
                app:customColorValue="#FF00FF" />
        </Constraint>
    </ConstraintSet>

    <ConstraintSet android:id="@+id/end">
        <Constraint
            android:id="@id/container"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent">
            <CustomAttribute
                app:attributeName="BackgroundColor"
                app:customColorValue="#00FFFF" />
        </Constraint>
    </ConstraintSet>

    <ConstraintSet android:id="@+id/half">
        <Constraint
            android:id="@id/container"
            android:layout_width="0dp"
            android:layout_height="300dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent">
            <CustomAttribute
                app:attributeName="BackgroundColor"
                app:customColorValue="#00FFFF" />
        </Constraint>
    </ConstraintSet>
</MotionScene>

然后我设置进度(但没有动画)

private fun onBottomButtonClick() {
    //motionLayout.transitionToState(R.id.half) //:(
    //motionLayout.setTransition(R.id.start, R.id.end) //locks layout

    motionLayout.progress = 0.5f
}

谢谢

解决方案是BottomSheetDialogFragment,几乎可以满足要求

使用MotionLayout和ValueAnimator做动画的解决方案如下

private fun slideToHalf() {
    if (motionLayout.progress == 0.5F) {
        return
    }
    ValueAnimator.ofFloat(motionLayout.progress, 0.5F).also {
        it.addUpdateListener { valueAnimator ->
            val progress = valueAnimator.animatedValue as Float
            motionLayout.progress = progress
        }
        it.duration = 200L
        it.interpolator = AccelerateInterpolator()
        it.start()
    }
}