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

时间:2018-10-31 15:42:17

标签: android android-animation android-constraintlayout android-motionlayout

需要帮助来创建类似于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
}

谢谢

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

使用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()
    }
}