admin管理员组

文章数量:1122852

I want to use animation zoom ,that is built for the Flutter language in Kotlin Jetpack Compose. How can I implement this animation by transition between activities using navigation?

Detail

I want to use animation zoom ,that is built for the Flutter language in Kotlin Jetpack Compose. How can I implement this animation by transition between activities using navigation?

Detail

Share Improve this question edited 2 days ago aliking1400 asked 2 days ago aliking1400aliking1400 13 bronze badges 3
  • I'm voting to close this because you told us what you want to do but did not ask a question that can be answered. – dominicoder Commented 2 days ago
  • @dominicoder edited – aliking1400 Commented 2 days ago
  • What have you tried? What are you specifically stuck on? There is ample documentation on doing screen transition animations on Android. So the answer to "how can I implement this" is "do some research, read some docs, watch some tutorials, and write the code". – dominicoder Commented yesterday
Add a comment  | 

1 Answer 1

Reset to default 0

I am not very clear what do you mean

How can I implement this animation by transition between activities using navigation?

If you mean using the Android navigation component, this is not a common usage. Because the Android navigation component usually uses a single activity with multiple fragments. If you mean normal start activity by intent, that is more reasonable. Whatever, I can give you the solution for these two situations.

Start activity by intent:

First activity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val button = findViewById<Button>(R.id.btn_to_second)
        button.setOnClickListener {
            val intent = Intent(this, SecondActivity::class.java)
            startActivity(intent)
            if (Build.VERSION.SDK_INT >= 34) {
                overrideActivityTransition(
                    OVERRIDE_TRANSITION_OPEN,
                    R.anim.zoom_in,
                    R.anim.zoom_out
                )
            } else {
                overridePendingTransition(R.anim.zoom_in, R.anim.zoom_out)
            }
        }
    }
}

Second activity

class SecondActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)
        onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                finish()
                if (Build.VERSION.SDK_INT >= 34) {
                    overrideActivityTransition(
                        OVERRIDE_TRANSITION_OPEN,
                        R.anim.zoom_in,
                        R.anim.zoom_out
                    )
                } else {
                    overridePendingTransition(R.anim.zoom_in, R.anim.zoom_out)
                }
            }
        })
    }
}

zoom_in.xml file

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <scale
        android:duration="300"
        android:fromXScale="0.3"
        android:fromYScale="0.3"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1"
        android:toYScale="1" />

</set>

zoom_out.xml file

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <scale
        android:duration="300"
        android:fromXScale="1"
        android:fromYScale="1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="0.0"
        android:toYScale="0.0" />

</set>

Put these animation XML files under your res/anim folder.

Then it's done, you can navigate between these two activities with zoom animation.

Navigation component:

Define navigation graph

<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/nav_main"
    app:startDestination="@id/mainFragment">

    <activity
        android:id="@+id/secondActivity"
        android:name="cn.denghanxi.gradletest.SecondActivity"
        android:label="activity_second"
        tools:layout="@layout/activity_second" />

    <action
        android:id="@+id/actionToSecondActivity"
        app:destination="@id/secondActivity"
        app:enterAnim="@anim/zoom_in"
        app:exitAnim="@anim/zoom_out"
        app:popEnterAnim="@anim/zoom_in"
        app:popExitAnim="@anim/zoom_out" />

</navigation>

Then use NavController to navigate by the action.

(Note: Pop animation does not seem to work properly for activities. We still need to configure the "onBackPressedDispatcher" of the "second activity" as in the example above.)

本文标签: androidActivity transaction in kotlinStack Overflow