为什么两个Motionlayout同时启动却串行执行

Why are two Motionlayout started simultaneously but executed serially

我基于MotionLayout创建了两个基本一致的动画,一个向上滑动,一个向下滑动。我同时启动了两个动画,但偶尔会不同步,更像是动画是串行执行的。有没有API同步他们?

like this occasionally abnormal

Execution log

<?xml version="1.0" encoding="utf-8"?>
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <ConstraintSet android:id="@+id/start">
        <Constraint
            android:id="@+id/guide_line"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_end="0dp" />
    </ConstraintSet>

    <ConstraintSet android:id="@+id/end">
        <Constraint
            android:id="@id/guide_line"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_begin="0dp" />
    </ConstraintSet>

    <Transition
        app:constraintSetEnd="@id/end"
        app:constraintSetStart="@+id/start"
        app:duration="300"
        app:motionInterpolator="linear" />
</MotionScene>

       viewBinding.playerBottomLl.setTransitionListener(object : MotionLayout.TransitionListener{
            override fun onTransitionStarted(p0: MotionLayout?, p1: Int, p2: Int) {
                LogUtils.e("TestMotion", "bottom: start: ${System.currentTimeMillis()}")
            }

            override fun onTransitionChange(p0: MotionLayout?, p1: Int, p2: Int, p3: Float) {

            }

            override fun onTransitionCompleted(p0: MotionLayout?, p1: Int) {
                LogUtils.e("TestMotion", "bottom: complete: ${System.currentTimeMillis()}")
            }

            override fun onTransitionTrigger(p0: MotionLayout?, p1: Int, p2: Boolean, p3: Float) {

            }

        })
    }

   ***

    override fun onVisibilityChanged(isVisible: Boolean) {
        LogUtils.e("TestMotion", "bottom: onVisibilityChanged: ${System.currentTimeMillis()}")
        if (isVisible) {
            viewBinding.playerBottomLl.transitionToEnd()
        } else {
            viewBinding.playerBottomLl.transitionToStart()
        }
    }

MotionLayout 实例是独立的。如果你想严格同步两个 自己驱动 setProgress 方法。 您可以通过多种方式做到这一点:

  • ObjectAnimator
  • 创建你自己的计时器
  • 监听一个MotionLayout的进度,设置另一个的进度。