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



需要帮助创建一个布局,该布局看起来像谷歌地图中的explore按钮

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

相关内容

最新更新