我有一个绑定到ViewPager的MotionLayout过渡。
private val pageChangedListener = object : SimpleOnPageChangeListener() {
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
super.onPageScrolled(position, positionOffset, positionOffsetPixels)
motionLayout.progress = (position + positionOffset) / (pager.adapter!!.count - 1)
}
}
除fitsSystemWindows之外,其他所有功能都可以正常运行,而fitsSystemWindows可以在添加到MotionLayout时中断动画。整个布局在动画的开始和结尾处跳转。看起来当motionLayout不完全为0或1时,两次添加了填充。我已经准备了两个视频,并将fitsSystemWindows设置为true和false,以更好地可视化问题。
请注意,视频的布局得到了简化,过渡显然要复杂得多,但这足以看出区别。另外,我正在使用constraintlayout:2.0.0-alpha3
。
fitsSystemWindows="true" ->
https://imgur.com/a/SlHH1NJ-动画从头到尾跳动。
fitsSystemWindows="false" ->
https://imgur.com/a/WwkfyoM-没有跳跃,但是底部按钮显然不适合系统窗口。
有什么想法可以解决过渡问题吗?
我的布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:layout_width="match_parent"
android:layout_height="match_parent">
...
<androidx.constraintlayout.motion.widget.MotionLayout
android:id="@+id/motionLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:layoutDescription="@xml/motion_scene">
<TextView
android:id="@+id/titleTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-thin"
android:gravity="center"
android:text="..."
android:textSize="52sp" />
<androidx.viewpager.widget.ViewPager
android:id="@+id/pager"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:visibility="gone" />
<LinearLayout
android:id="@+id/timerLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical">
...
</LinearLayout>
<Button
android:id="@+id/stopBtn"
style="?attr/borderlessButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:layout_margin="28dp"
android:drawableTop="@drawable/ic_close"
android:drawablePadding="12dp"
android:letterSpacing=".1"
android:text="..."
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.motion.widget.MotionLayout>
</FrameLayout>
MotionScene xml:
<?xml version="1.0" encoding="utf-8"?>
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:motion="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<Transition
motion:constraintSetEnd="@+id/end"
motion:constraintSetStart="@+id/start"
motion:duration="250"
motion:interpolator="linear">
<ConstraintSet android:id="@+id/start">
<Constraint
android:id="@+id/titleTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp"
android:alpha="1"
android:scaleX="1"
android:scaleY="1"
motion:layout_constraintBottom_toTopOf="@+id/timerLayout"
motion:layout_constraintEnd_toEndOf="parent"
motion:layout_constraintStart_toStartOf="parent" />
<Constraint
android:id="@+id/timerLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp"
android:scaleX="1"
android:scaleY="1"
motion:layout_constraintBottom_toBottomOf="parent"
motion:layout_constraintEnd_toEndOf="parent"
motion:layout_constraintStart_toStartOf="parent"
motion:layout_constraintTop_toTopOf="parent" />
</ConstraintSet>
<ConstraintSet android:id="@+id/end">
<Constraint
android:id="@+id/titleTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:alpha="0"
android:scaleX="0.6"
android:scaleY="0.6"
motion:layout_constraintBottom_toTopOf="@+id/timerLayout"
motion:layout_constraintEnd_toEndOf="parent"
motion:layout_constraintStart_toStartOf="parent"
tools:alpha="0.5" />
<Constraint
android:id="@+id/timerLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="430dp"
android:scaleX="0.7"
android:scaleY="0.7"
motion:layout_constraintBottom_toBottomOf="parent"
motion:layout_constraintEnd_toEndOf="parent"
motion:layout_constraintStart_toStartOf="parent"
motion:layout_constraintTop_toTopOf="parent" />
</ConstraintSet>
</Transition>
</MotionScene>
答案 0 :(得分:0)
我遇到了类似的问题,我通过向运动布局添加 MotionLayout.TransitionListener
来解决它。你可以试试你的情况。
motion_layout.setTransitionListener(object : MotionLayout.TransitionListener {
/** Called when a drawer is about to start a transition*/
override fun onTransitionStarted(motionLayout: MotionLayout?, startId: Int, endId: Int) { }
/** Called when a drawer's position changes*/
override fun onTransitionChange(motionLayout: MotionLayout?, startId: Int, endId: Int, progress: Float) { }
/** Called when a drawer has settled completely a state*/
override fun onTransitionCompleted(motionLayout: MotionLayout?, currentId: Int) {
motionLayout?.let {
ViewCompat.setOnApplyWindowInsetsListener(it) { _, insets ->
image.setMarginTop(insets.systemWindowInsetTop)
insets
}
}
}
/** Call when a trigger is fired*/
override fun onTransitionTrigger(motionLayout: MotionLayout?, triggerId: Int, positive: Boolean, progress: Float) { }
})