0
  1. I use MotionLayout for the animate view1.
  2. I want programmatically change constraint view1 to view2.
  3. I want to use different layout's in my scene instead of using ConstrainsSet

So, I have:

res/xml/activity_scene.xml

 <Transition
    android:id="@+id/transition1"
    motion:constraintSetStart="@layout/activity_state1"
    motion:constraintSetEnd="@layout/activity_state2">

    // <..>
</Transition>

res/layout/activity_state1.xml

<androidx.constraintlayout.motion.widget.MotionLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/motionLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutDescription="@xml/activity_scene"> 

// viev1 is small and view1 at left

/>

res/layout/activity_state2.xml

<androidx.constraintlayout.motion.widget.MotionLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/motionLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutDescription="@xml/activity_scene"> 

// viev1 is big and view1 at center

/>

Now I want to change the constraint of my view1(change start position on-screen from left to right) from activity_state1.

Hence, I know how to change constraint, see to the code below:

MainActivity.kt

  fun changeConstraint() {
        motionLayout.getConstraintSet(**????**)?.let {
            it.clear(R.id.img)
            it.connect(R.id.view1, ConstraintSet.START, R.id.place2, ConstraintSet.START, 0)
            it.connect(R.id.view1, ConstraintSet.TOP, R.id.place2, ConstraintSet.TOP, 0)
        }
        motionLayout.requestLayout()
    }

I tried use another methods of change position in constraint. I tried using motionLayout.applyTo() in constraint and using, and change layoutParams of view1. Also, I tried to change the XY of view1. Unforchenetly, it does not work correctly for me. Because the MotionLayout doesn't change the trajectory of animation. Or not any changes.

Hence, I should set ID to constraintSet for finding it from MainActivity. And I want to use different layouts in my scene instead to declare ConstraintSet with id into the scene because the scene doesn't work a visual presentation of the layout.

2 question;

  1. Do u know how get constraintSet of my activity_state1.xml
  2. Do u know another methods of change start positions in MotionLayout?

1 Answers1

0

After attaching to a window MotionLayout processes the motionScene file and creates a series of ConstraintSet objects (see ConstraintSet).

You can query the ConstraintSet's contained in the MotionLayout. Get them, update the values and update MotionLayout with them.

 cs =  motionLayout.getConstraintSet(id);
 cs.change Stuf()
 motionLayout.updateState(id, cs);
 motionLayout.rebuildScene();

if that state is on the screen you can animate the change in 2.1.

 cs = motionLayout.getConstraintSet(id);
 cs.change_stuff_on_screen()
 motionLayout.updateStateAnimate(id, cs);
  
hoford
  • 4,918
  • 2
  • 19
  • 19