I need to click button1 to replace view1 in the container with view2 to do the expansion animation. When button2 is clicked, replace view2 with view1 to do the shrinking animation.
I used TransitionManager.go() and ChangeBounds() to implement this animation, but only the expansion animation took effect, and the shrinking animation did not seem to take effect.
Here is the code for the Main Activity.
private val expandTransition: Transition =
TransitionSet().addTransition(ChangeBounds()).setDuration(500)
private val shrinkTransition: Transition =
TransitionSet().addTransition(ChangeBounds()).setDuration(500)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main_1)
view2Layout = LayoutInflater.from(this).inflate(R.layout.sceneB, null, false)
view2 = view2Layout.findViewById<ViewGroup>(R.id.view2)
view1 = findViewById(R.id.view1)
val sceneA = Scene(container, view2)
val sceneB = Scene(container, view1)
(view2.parent as? ViewGroup)?.removeView(view2)
button1.setOnClickListener {
TransitionManager.go(sceneA, expandTransition)
}
button2.setOnClickListener {
TransitionManager.go(sceneB, shrinkTransition)
}
}
The activity_main_1.xml
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/container"
android:layout_width="wrap_content"
android:layout_height="100dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent">
<View
android:id="@+id/view1"
android:layout_width="40dp"
android:layout_height="100dp"
android:background="#fff000" />
</FrameLayout>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="click for fold"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/button2" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="click for expand"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
The scencB.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="88dp"
android:layout_height="100dp">
<FrameLayout
android:id="@+id/view2"
android:background="@color/design_default_color_secondary_variant"
android:layout_width="88dp"
android:layout_height="match_parent"/>
</FrameLayout>
gif is below