Home > Back-end >  Why are two Motionlayout started simultaneously but executed serially
Why are two Motionlayout started simultaneously but executed serially

Time:11-28

I created two basically consistent animations based on MotionLayout, one sliding up and the other sliding down. I started the two animations at the same time, but occasionally they are out of sync, it is more like the animation is executed serially. Is there any API to synchronize them?

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>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

       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()
        }
    }
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

MotionLayout instances are independent. If you want strict synchronization of two drive the setProgress methods yourself. You can do that in several ways:

  • ObjectAnimator
  • Create your own timer
  • Listen to the progress of on MotionLayout and set the progress of the other.
  • Related