TransitionManager.beginDelayedTransition не анимируется при первой загрузке активности - PullRequest
0 голосов
/ 16 апреля 2019

Я использую constraintLayout с анимацией с использованием TransitionManager.

У меня есть следующие 2 макета

Это мой main_activity.xml

<android.support.constraint.ConstraintLayout
        android:id="@+id/constraintLayout"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

       <ImageView
            android:id="@+id/image"
            android:layout_width="0dp"
            android:layout_height="160dp"
            android:background="@color/colorPrimary"
            app:layout_constraintTop_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"/>

    <Button
            android:id="@+id/btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            android:text="Let's start animating"/>
</android.support.constraint.ConstraintLayout>

И это мойmain_activity_alt.xml

<android.support.constraint.ConstraintLayout
        android:id="@+id/constraintLayout"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto">

      <ImageView
            android:id="@+id/image"
            android:layout_width="0dp"
            android:layout_height="160dp"
            android:background="@color/colorPrimary"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"/>

    <Button
            android:id="@+id/btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            android:text="Let's start animating"/>
</android.support.constraint.ConstraintLayout>

В моей MainActivity я хочу анимировать изображение, чтобы скользить вверх, что было бы main_activity_alt.xml

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val constraintSet1 = ConstraintSet()
        constraintSet1.clone(constraintLayout)

        val constraintSet2 = ConstraintSet()
        constraintSet2.clone(this, R.layout.activity_main_alt)

        val transition = ChangeBounds()
        transition.duration = 5000

   val handler = Handler()
        handler.postDelayed({
            TransitionManager.beginDelayedTransition(constraintLayout, transition)
            constraintSet2.applyTo(constraintLayout)
        }, 10)
    }

Когда запускается выше, изображение уже отображается.main_activity.xml должен спрятаться, а main_activity_alt.xml будет его последним местом отдыха.

Однако, когда экраны загружаются, он сразу же отображает imageview без анимации

enter image description here

Что-то не так с вышеизложенным?

Ответы [ 2 ]

2 голосов
/ 16 апреля 2019

Попробуйте сделать задержку для вашего перехода, чтобы первый макет был раздут, когда переход начнется

transition.delay = 300

если это не сработает, попробуйте

Handler().postDelayed({do transition here}, 300)

1 голос
/ 16 апреля 2019

Из документации для beginDelayedTransition :

При вызове этого метода TransitionManager захватывает текущие значения в корне сцены и затем отправляет запрос на выполнениепереход на следующий кадр.В это время новые значения в корне сцены будут записаны, а изменения будут анимированы.

Вам придется подождать, пока макет не будет размечен, прежде чем пытаться выполнить переход.Есть несколько способов сделать это, но самым простым будет опубликовать код перехода следующим образом:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val constraintLayout = findViewById<ConstraintLayout>(R.id.constraintLayout)
    constraintLayout.post {
        val constraintSet1 = ConstraintSet()
        constraintSet1.clone(constraintLayout)
        val constraintSet2 = ConstraintSet()
        constraintSet2.clone(this, R.layout.activity_main2_alt)
        val transition = ChangeBounds()
        transition.duration = 5000
        TransitionManager.beginDelayedTransition(constraintLayout, transition)
        constraintSet2.applyTo(constraintLayout)
    }
}
...