Skip to content

Instantly share code, notes, and snippets.

@Calvin-LL
Last active April 9, 2024 05:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Calvin-LL/2b40e7348d0505d1e069609c6a7c615e to your computer and use it in GitHub Desktop.
Save Calvin-LL/2b40e7348d0505d1e069609c6a7c615e to your computer and use it in GitHub Desktop.
Jetpack Compose NavHost transitions based on the default built-in Android native activity transitions
NavHost(
// ...
enterTransition = { ActivityOpenEnter },
exitTransition = { ActivityOpenExit },
popEnterTransition = { ActivityCloseEnter },
popExitTransition = { ActivityCloseExit },
) {
// ...
}
import android.graphics.Path
import android.view.animation.PathInterpolator
import androidx.compose.animation.core.Easing
import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.slideInHorizontally
import androidx.compose.animation.slideOutHorizontally
// from https://cs.android.com/android/platform/superproject/main/+/main:frameworks/base/core/res/res/interpolator/fast_out_extra_slow_in.xml;drc=606c6aba7560c9bf873f27589afef4f81ff22fd5
val FastOutExtraSlowIn: Easing = Easing { fraction ->
val path = Path().apply {
moveTo(0f, 0f)
cubicTo(0.05f, 0f, 0.133333f, 0.06f, 0.166666f, 0.4f)
cubicTo(0.208333f, 0.82f, 0.25f, 1f, 1f, 1f)
}
val pathInterpolator = PathInterpolator(path)
pathInterpolator.getInterpolation(fraction)
}
// from https://cs.android.com/android/platform/superproject/main/+/main:frameworks/base/core/res/res/anim/activity_open_enter.xml;drc=9ebb08d22e8598eb6dc676a24bd34068cdc7a745
val ActivityOpenEnter = fadeIn(tween(83, 50, LinearEasing)) + slideInHorizontally(tween(
450, easing = FastOutExtraSlowIn
), { it / 10 })
// from https://cs.android.com/android/platform/superproject/main/+/main:frameworks/base/core/res/res/anim/activity_open_exit.xml;drc=9ebb08d22e8598eb6dc676a24bd34068cdc7a745
val ActivityOpenExit = slideOutHorizontally(tween(450, easing = FastOutExtraSlowIn), { -it / 10 })
// from https://cs.android.com/android/platform/superproject/main/+/main:frameworks/base/core/res/res/anim/activity_close_enter.xml;drc=9ebb08d22e8598eb6dc676a24bd34068cdc7a745
val ActivityCloseEnter = slideInHorizontally(tween(450, easing = FastOutExtraSlowIn), { -it / 10 })
// from https://cs.android.com/android/platform/superproject/main/+/main:frameworks/base/core/res/res/anim/activity_close_exit.xml;drc=9ebb08d22e8598eb6dc676a24bd34068cdc7a745
val ActivityCloseExit = fadeOut(tween(83, 35, LinearEasing)) + slideOutHorizontally(tween(
450, easing = FastOutExtraSlowIn
), { it / 10 })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment