Zadbaj o dobrą organizację dzięki kolekcji
Zapisuj i kategoryzuj treści zgodnie ze swoimi preferencjami.
Przewijanie paralaksy to technika, w której treści tła i pierwszego planu przesuwają się z różną prędkością. Możesz zastosować tę technikę, aby ulepszyć interfejs aplikacji i utworzyć bardziej dynamiczne wrażenia podczas przewijania przez użytkowników.
Zgodność wersji
Ta implementacja wymaga, aby minimalna wersja pakietu SDK projektu była ustawiona na poziom API 21 lub wyższy.
Zależności
Tworzenie efektu paralaksy
Aby uzyskać efekt paralaksy, zastosuj ułamek wartości przewijania z komponentu przewijania do komponentu, który wymaga efektu paralaksy. Poniższy fragment kodu wybiera 2 zagnieżdżone elementy wizualne – obraz i blok tekstu – i przewija je w tym samym kierunku z różną szybkością:
@ComposablefunParallaxEffect(){funModifier.parallaxLayoutModifier(scrollState:ScrollState,rate:Int)=layout{measurable,constraints->
valplaceable=measurable.measure(constraints)valheight=if(rate > 0)scrollState.value/rateelsescrollState.valuelayout(placeable.width,placeable.height){placeable.place(0,height)}}valscrollState=rememberScrollState()Column(modifier=Modifier.fillMaxWidth().verticalScroll(scrollState),){Image(painterResource(id=R.drawable.cupcake),contentDescription="Android logo",contentScale=ContentScale.Fit,// Reduce scrolling rate by half.modifier=Modifier.parallaxLayoutModifier(scrollState,2))Text(text=stringResource(R.string.detail_placeholder),modifier=Modifier.background(Color.White).padding(horizontal=8.dp),)}}
Tekst jest centralnym elementem każdego interfejsu użytkownika. Dowiedz się, jak możesz wyświetlać tekst w aplikacji, aby zapewnić użytkownikom przyjemne wrażenia.
Treść strony i umieszczone na niej fragmenty kodu podlegają licencjom opisanym w Licencji na treści. Java i OpenJDK są znakami towarowymi lub zastrzeżonymi znakami towarowymi należącymi do firmy Oracle lub jej podmiotów stowarzyszonych.
Ostatnia aktualizacja: 2025-02-06 UTC.
[[["Łatwo zrozumieć","easyToUnderstand","thumb-up"],["Rozwiązało to mój problem","solvedMyProblem","thumb-up"],["Inne","otherUp","thumb-up"]],[["Brak potrzebnych mi informacji","missingTheInformationINeed","thumb-down"],["Zbyt skomplikowane / zbyt wiele czynności do wykonania","tooComplicatedTooManySteps","thumb-down"],["Nieaktualne treści","outOfDate","thumb-down"],["Problem z tłumaczeniem","translationIssue","thumb-down"],["Problem z przykładami/kodem","samplesCodeIssue","thumb-down"],["Inne","otherDown","thumb-down"]],["Ostatnia aktualizacja: 2025-02-06 UTC."],[],[],null,["# Create a parallax scrolling effect\n\n\u003cbr /\u003e\n\nParallax scrolling is a technique in which the background content and foreground\ncontent scroll at different speeds. You can implement this technique to enhance\nyour app's UI, creating a more dynamic experience as your users scroll.\n\nVersion compatibility\n---------------------\n\nThis implementation requires that your project minSDK be set to API level 21 or\nhigher.\n\n### Dependencies\n\n### Kotlin\n\n\u003cbr /\u003e\n\n```kotlin\n implementation(platform(\"androidx.compose:compose-bom:2025.08.00\"))\n \n```\n\n\u003cbr /\u003e\n\n### Groovy\n\n\u003cbr /\u003e\n\n```groovy\n implementation platform('androidx.compose:compose-bom:2025.08.00')\n \n```\n\n\u003cbr /\u003e\n\nCreate a parallax effect\n------------------------\n\nTo achieve the parallax effect, you apply a fraction of the scrolling value from\nthe scrolling composable to the composable that needs the parallax effect. The\nfollowing snippet takes two nested visual elements---an image and a block of\ntext---and scrolls them in the same direction at different speeds:\n\n\n```kotlin\n@Composable\nfun ParallaxEffect() {\n fun Modifier.parallaxLayoutModifier(scrollState: ScrollState, rate: Int) =\n layout { measurable, constraints -\u003e\n val placeable = measurable.measure(constraints)\n val height = if (rate \u003e 0) scrollState.value / rate else scrollState.value\n layout(placeable.width, placeable.height) {\n placeable.place(0, height)\n }\n }\n\n val scrollState = rememberScrollState()\n Column(\n modifier = Modifier\n .fillMaxWidth()\n .verticalScroll(scrollState),\n ) {\n\n Image(\n painterResource(id = R.drawable.cupcake),\n contentDescription = \"Android logo\",\n contentScale = ContentScale.Fit,\n // Reduce scrolling rate by half.\n modifier = Modifier.parallaxLayoutModifier(scrollState, 2)\n )\n\n Text(\n text = stringResource(R.string.detail_placeholder),\n modifier = Modifier\n .background(Color.White)\n .padding(horizontal = 8.dp),\n\n )\n }\n}https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/compose/snippets/src/main/java/com/example/compose/snippets/images/ParallaxEffect.kt#L39-L73\n```\n\n\u003cbr /\u003e\n\n### Key points about the code\n\n- Creates a custom `layout` modifier to adjust the rate by which the composable scrolls.\n- The `Image` scrolls at a slower rate than the `Text`, producing a parallax effect as the two composables translate vertically at different rates.\n\nResults\n-------\n\n\u003cbr /\u003e\n\n**Figure 1.** A scrolling list with a parallax effect.\n\n\u003cbr /\u003e\n\nCollections that contain this guide\n-----------------------------------\n\nThis guide is part of these curated Quick Guide collections that cover\nbroader Android development goals: \n\n### Display a list or grid\n\nLists and grids allow your app to display collections in a visually pleasing form that's easy for users to consume. \n[Quick guide collection](/develop/ui/compose/quick-guides/collections/display-a-list-or-grid) \n\n### Display images\n\nDiscover techniques for using bright, engaging visuals to give your Android app a beautiful look and feel. \n[Quick guide collection](/develop/ui/compose/quick-guides/collections/display-images) \n\n### Display text\n\nText is a central piece of any UI. Find out different ways you can present text in your app to provide a delightful user experience. \n[Quick guide collection](/develop/ui/compose/quick-guides/collections/display-text) \n\nHave questions or feedback\n--------------------------\n\nGo to our frequently asked questions page and learn about quick guides or reach out and let us know your thoughts. \n[Go to FAQ](/quick-guides/faq) [Leave feedback](https://issuetracker.google.com/issues/new?component=1573691&template=1993320)"]]