Alexander
Alexander

Reputation: 431

Android MotionLayout Pause

Is it possible to pause Motion layout transition and then resume this transition?(for example I show dialog fragment and I need to pause this motion scene and after dismissing dialog I want to resume transition) I launch transition by code like as MotionLayout.setTransitionToEnd()

Upvotes: 0

Views: 1091

Answers (3)

Lilia
Lilia

Reputation: 512

You can store the position of progress that you left before pausing the fragment like so:

private var outState: Bundle? = null
const val KEY_CURRENT_POSITION_LIST_CATEGORY = "KEY_CURRENT_POSITION_LIST_CATEGORY"
override fun onPause() {
        super.onPause()
        outState = Bundle()
        outState!!.putFloat(KEY_CURRENT_POSITION_LIST_CATEGORY, motion_layout.progress)
    }

And onResume, you can just set the progress of the animation in the motion_layout

 override fun onResume() {
        super.onResume()
        if (outState != null) {
        motion_layout.progress=outState!!.getFloat(KEY_CURRENT_POSITION_LIST_CATEGORY)
        }
    }

Upvotes: 0

kjanderson2
kjanderson2

Reputation: 1359

You should be able to get the current progress and set to that value... then when you are ready to resume, you can call the transitionToEnd() again and it will resume from that value... so for example

private void pauseAnimation() {
    float currentProgress = motionLayout.getProgress()
    motionLayout.setProgress(currentProgress())
}

private void resumeAnimation() {
    motionLayout.transitionToEnd()
}

That should do what you are looking to accomplish

Upvotes: 3

Alexander
Alexander

Reputation: 431

I found the public method in MotionLayout class setProgress(float) and setInterpolatedPreogress(float) https://developer.android.com/reference/android/support/constraint/motion/MotionLayout.html#setProgress(float) https://developer.android.com/reference/android/support/constraint/motion/MotionLayout.html#setInterpolatedProgress(float) which allows to resume animation

Upvotes: 0

Related Questions