با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
NavDisplay قابلیتهای پویانمایی داخلی را برای ایجاد انتقالهای بصری صاف در حین حرکت کاربران در برنامه شما فراهم میکند. شما می توانید این انیمیشن ها را به صورت جهانی برای NavDisplay یا بر اساس هر NavEntry با استفاده از ابرداده سفارشی کنید.
نادیده گرفتن انتقال های پیش فرض
NavDisplay از ContentTransform s برای تعریف نحوه متحرک شدن محتوا در حین پیمایش استفاده می کند. میتوانید با ارائه پارامترهای انتقال به NavDisplay ، رفتارهای پیشفرض انیمیشن را لغو کنید.
transitionSpec : این پارامتر ContentTransform را تعریف می کند تا وقتی محتوا به پشته اضافه می شود (یعنی هنگام حرکت به جلو) اعمال شود.
popTransitionSpec : این پارامتر ContentTransform را تعریف می کند تا زمانی که محتوا از پشته حذف می شود (یعنی هنگام پیمایش به عقب) اعمال شود.
predictivePopTransitionSpec : این پارامتر ContentTransform را تعریف می کند تا در هنگام نمایش محتوا با استفاده از یک حرکت پیشگویانه به عقب اعمال شود.
تغییر در سطح NavEntry فردی را لغو کنید
شما همچنین می توانید انیمیشن های سفارشی را برای NavEntry های خاص با استفاده از ابرداده آنها تعریف کنید. NavDisplay کلیدهای فراداده ویژه را برای اعمال انتقال در هر ورودی تشخیص می دهد:
NavDisplay.transitionSpec : از این تابع کمکی برای تعریف انیمیشن پیمایش رو به جلو استفاده کنید.
NavDisplay.popTransitionSpec : از این تابع کمکی برای تعریف انیمیشن پیمایش به عقب برای یک NavEntry خاص استفاده کنید.
NavDisplay.predictivePopTransitionSpec : از این تابع کمکی برای تعریف انیمیشن برای حرکات پیشگویانه برگشت برای NavEntry خاص استفاده کنید.
این انتقالهای فراداده در هر ورودی، انتقالهای جهانی NavDisplay با همین نام را لغو میکنند.
قطعه زیر هم انتقال جهانی NavDisplay و هم یک لغو در سطح NavEntry فردی را نشان می دهد:
@SerializabledataobjectScreenA:NavKey@SerializabledataobjectScreenB:NavKey@SerializabledataobjectScreenC:NavKeyclassAnimatedNavDisplayActivity:ComponentActivity(){overridefunonCreate(savedInstanceState:Bundle?){super.onCreate(savedInstanceState)setContent{Scaffold{paddingValues->
valbackStack=rememberNavBackStack(ScreenA)NavDisplay(backStack=backStack,onBack={backStack.removeLastOrNull()},entryProvider=entryProvider{entry<ScreenA>{ContentOrange("This is Screen A"){Button(onClick={backStack.add(ScreenB)}){Text("Go to Screen B")}}}entry<ScreenB>{ContentMauve("This is Screen B"){Button(onClick={backStack.add(ScreenC)}){Text("Go to Screen C")}}}entry<ScreenC>(metadata=NavDisplay.transitionSpec{// Slide new content up, keeping the old content in place underneathslideInVertically(initialOffsetY={it},animationSpec=tween(1000))togetherWithExitTransition.KeepUntilTransitionsFinished}+NavDisplay.popTransitionSpec{// Slide old content down, revealing the new content in place underneathEnterTransition.NonetogetherWithslideOutVertically(targetOffsetY={it},animationSpec=tween(1000))}+NavDisplay.predictivePopTransitionSpec{// Slide old content down, revealing the new content in place underneathEnterTransition.NonetogetherWithslideOutVertically(targetOffsetY={it},animationSpec=tween(1000))}){ContentGreen("This is Screen C")}},transitionSpec={// Slide in from right when navigating forwardslideInHorizontally(initialOffsetX={it})togetherWithslideOutHorizontally(targetOffsetX={-it})},popTransitionSpec={// Slide in from left when navigating backslideInHorizontally(initialOffsetX={-it})togetherWithslideOutHorizontally(targetOffsetX={it})},predictivePopTransitionSpec={// Slide in from left when navigating backslideInHorizontally(initialOffsetX={-it})togetherWithslideOutHorizontally(targetOffsetX={it})},modifier=Modifier.padding(paddingValues))}}}}
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی."],[],[],null,["# Animate between destinations\n\n[`NavDisplay`](/reference/kotlin/androidx/navigation3/ui/package-summary#NavDisplay(kotlin.collections.List,androidx.compose.ui.Modifier,androidx.compose.ui.Alignment,kotlin.Function1,kotlin.collections.List,androidx.navigation3.ui.SceneStrategy,androidx.compose.animation.SizeTransform,kotlin.Function1,kotlin.Function1,kotlin.Function1,kotlin.Function1)) provides built-in animation capabilities to create smooth\nvisual transitions as users navigate through your app. You can customize these\nanimations globally for the `NavDisplay` or on a per-`NavEntry` basis using\nmetadata.\n\nOverride default transitions\n----------------------------\n\n`NavDisplay` uses `ContentTransform`s to define how content animates during\nnavigation. You can override the default animation behaviors by providing\ntransition parameters to `NavDisplay`.\n\n- `transitionSpec`: This parameter defines the `ContentTransform` to apply when content is added to the back stack (i.e., when navigating forward).\n- `popTransitionSpec`: This parameter defines the `ContentTransform` to apply when content is removed from the back stack (i.e., when navigating back).\n- `predictivePopTransitionSpec`: This parameter defines the `ContentTransform` to apply when content is popped using a predictive back gesture.\n\nOverride transitions at the individual `NavEntry` level\n-------------------------------------------------------\n\nYou can also define custom animations for specific [`NavEntry`s](/reference/kotlin/androidx/navigation3/runtime/NavEntry) using their\nmetadata. `NavDisplay` recognizes special metadata keys to apply per-entry\ntransitions:\n\n- `NavDisplay.transitionSpec`: Use this helper function to define the forward navigation animation.\n- `NavDisplay.popTransitionSpec`: Use this helper function to define the backward navigation animation for a specific `NavEntry`.\n- `NavDisplay.predictivePopTransitionSpec`: Use this helper function to define the animation for predictive back gestures for a specific `NavEntry`.\n\nThese per-entry metadata transitions override the `NavDisplay`'s global\ntransitions of the same name.\n\nThe following snippet demonstrates both global `NavDisplay` transitions and an\noverride at the individual `NavEntry` level:\n\n\n```kotlin\n@Serializable\ndata object ScreenA : NavKey\n\n@Serializable\ndata object ScreenB : NavKey\n\n@Serializable\ndata object ScreenC : NavKey\n\nclass AnimatedNavDisplayActivity : ComponentActivity() {\n\n override fun onCreate(savedInstanceState: Bundle?) {\n super.onCreate(savedInstanceState)\n setContent {\n\n Scaffold { paddingValues -\u003e\n\n val backStack = rememberNavBackStack(ScreenA)\n\n NavDisplay(\n backStack = backStack,\n onBack = { backStack.removeLastOrNull() },\n entryProvider = entryProvider {\n entry\u003cScreenA\u003e {\n ContentOrange(\"This is Screen A\") {\n Button(onClick = { backStack.add(ScreenB) }) {\n Text(\"Go to Screen B\")\n }\n }\n }\n entry\u003cScreenB\u003e {\n ContentMauve(\"This is Screen B\") {\n Button(onClick = { backStack.add(ScreenC) }) {\n Text(\"Go to Screen C\")\n }\n }\n }\n entry\u003cScreenC\u003e(\n metadata = NavDisplay.transitionSpec {\n // Slide new content up, keeping the old content in place underneath\n slideInVertically(\n initialOffsetY = { it },\n animationSpec = tween(1000)\n ) togetherWith ExitTransition.KeepUntilTransitionsFinished\n } + NavDisplay.popTransitionSpec {\n // Slide old content down, revealing the new content in place underneath\n EnterTransition.None togetherWith\n slideOutVertically(\n targetOffsetY = { it },\n animationSpec = tween(1000)\n )\n } + NavDisplay.predictivePopTransitionSpec {\n // Slide old content down, revealing the new content in place underneath\n EnterTransition.None togetherWith\n slideOutVertically(\n targetOffsetY = { it },\n animationSpec = tween(1000)\n )\n }\n ) {\n ContentGreen(\"This is Screen C\")\n }\n },\n transitionSpec = {\n // Slide in from right when navigating forward\n slideInHorizontally(initialOffsetX = { it }) togetherWith\n slideOutHorizontally(targetOffsetX = { -it })\n },\n popTransitionSpec = {\n // Slide in from left when navigating back\n slideInHorizontally(initialOffsetX = { -it }) togetherWith\n slideOutHorizontally(targetOffsetX = { it })\n },\n predictivePopTransitionSpec = {\n // Slide in from left when navigating back\n slideInHorizontally(initialOffsetX = { -it }) togetherWith\n slideOutHorizontally(targetOffsetX = { it })\n },\n modifier = Modifier.padding(paddingValues)\n )\n }\n }\n }\n}https://github.com/android/snippets/blob/26d364466ee1c03d658ba2f0905f7cc1a97afefa/compose/snippets/src/main/java/com/example/compose/snippets/navigation3/animations/AnimationSnippets.kt#L46-L129\n```\n\n\u003cbr /\u003e\n\n**Figure 1**. App with custom animations."]]