- I use MotionLayout for the animate view1.
- I want programmatically change constraint view1 to view2.
- 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;
- Do u know how get constraintSet of my activity_state1.xml
- Do u know another methods of change start positions in MotionLayout?