2

Я никак не могу разобраться в коде, что представлен на этом сайте, ибо он написан на Kotlin. Располагаю версией библиотеки com.google.android.material:material:1.2.0-alpha06. При попытке сделать анимацию по оси X между активностями, ничего не получается. Я без малейшего понятия, что я делаю не так. Ниже представлены мои попытки осуществить этот переход. Если что, главный Layout в двух активностях имеет ID root. Ни на одной из версий API 23-30 это не работает.

Можете ли Вы, пожалуйста, переписать код с сайта выше с Kotlin на Java?

Первый класс

getWindow().requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS);
MaterialSharedAxis exit = (MaterialSharedAxis) MaterialSharedAxis.create(MaterialSharedAxis.X, true).addTarget(R.id.root);
getWindow().setExitTransition(exit);

Второй класс

getWindow().requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS);
MaterialSharedAxis enter = (MaterialSharedAxis) MaterialSharedAxis.create(MaterialSharedAxis.X, true).addTarget(R.id.root);
getWindow().setEnterTransition(enter);
getWindow().setAllowEnterTransitionOverlap(true);

UPD: Теперь я попробовал сделать это на Kotlin, но всё равно ничего не получается. Что я делаю не так?

MainActivity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        window.requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS)

        val exit = MaterialSharedAxis.create(MaterialSharedAxis.X, true).apply {
            addTarget(R.id.root)
        }
        window.exitTransition = exit
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        findViewById<Button>(R.id.butt).setOnClickListener {startActivity(Intent(this, SecondActivity::class.java))}
    }
}

SecondActivity

class SecondActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        window.requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS)

        val enter = MaterialSharedAxis.create(MaterialSharedAxis.X, true).apply {
            addTarget(R.id.root)
        }
        window.enterTransition = enter
        window.allowEnterTransitionOverlap = true
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:id="@+id/root">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello First!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/butt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textView"
        android:text="GOOO" />

</androidx.constraintlayout.widget.ConstraintLayout>

activity_second.xml

<?xml version="1.0" encoding="utf-8"?>
<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=".SecondActivity"
    android:id="@+id/root">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Second!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Посмотрите другие вопросы с метками или задайте свой вопрос.