Zadbaj o dobrą organizację dzięki kolekcji
Zapisuj i kategoryzuj treści zgodnie ze swoimi preferencjami.
Możesz stworzyć bardziej interaktywną i zaangażować użytkowników w swojej aplikacji, wczytując plik drawable, aby wyświetlać animowane obrazy. Animowane obrazy są przydatne do tworzenia wskaźników ładowania, wskaźników sukcesu lub błędów, ułatwiania tworzenia gier oraz różnych innych funkcji interfejsu.
Zgodność wersji
Ta implementacja wymaga, aby minimalna wersja pakietu SDK projektu była ustawiona na poziom API 21 lub wyższy.
Zależności
Wyświetlanie animowanego obrazu
Poniższy kod wyświetla animowany wektor, który automatycznie przełącza się między 2 stanami:
Ładuje zasób wektorowy, animując atrybuty rysunku w czasie.
Wystąpienie Image, które używa wystąpienia Painter do wykonania animacji utworzonej przez funkcję rememberAnimatedVectorPainter() z poziomu stanu AnimatedImageVector i boolean.
Gdy atEnd ma wartość true, instancja Painter przestaje się animować.
Wyniki
Rysunek 1. Animowany obiekt rysowany wektorowo w Compose.
Kolekcje zawierające ten przewodnik
Ten przewodnik należy do tych kolekcji krótkich przewodników, które obejmują szersze zagadnienia związane z tworzeniem aplikacji na Androida:
Wyświetl obrazy
Poznaj techniki tworzenia jasnych i przyciągających uwagę elementów wizualnych, które nadadzą Twojej aplikacji na Androida atrakcyjny wygląd.
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,["# Display an animated image\n\n\u003cbr /\u003e\n\nYou can create a more interactive and engaging user experience in your app by\nloading a drawable file to display animated images. Animated images are useful\nfor creating loading indicators, success or error indicators, facilitating game\ndevelopment, and various other UI functions.\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 implementation(\"androidx.compose.animation:animation.graphics.android:1.6.3\")\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 implementation 'androidx.compose.animation:animation.graphics.android:1.6.3'\n \n```\n\n\u003cbr /\u003e\n\nDisplay an animated image\n-------------------------\n\nThe following code displays an animated vector that automatically toggles\nbetween two states:\n\n\n```kotlin\n@Composable\nfun AnimatedVectorDrawable() {\n val image = AnimatedImageVector.animatedVectorResource(R.drawable.ic_hourglass_animated)\n var atEnd by remember { mutableStateOf(false) }\n Image(\n painter = rememberAnimatedVectorPainter(image, atEnd),\n contentDescription = \"Timer\",\n modifier = Modifier.clickable {\n atEnd = !atEnd\n },\n contentScale = ContentScale.Crop\n )\n}https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/compose/snippets/src/main/java/com/example/compose/snippets/animations/AnimationSnippets.kt#L915-L927\n```\n\n\u003cbr /\u003e\n\n### Key points about the code\n\n- Loads a vector resource, animating the drawing attributes over time.\n- An `Image` instance that uses a [`Painter`](/reference/kotlin/androidx/compose/ui/graphics/painter/Painter) instance to perform the animation, created from the `AnimatedImageVector` and `boolean` state by the `rememberAnimatedVectorPainter()` function.\n- When `atEnd` is `true`, the `Painter` instance stops animating.\n\nResults\n-------\n\n**Figure 1.** Animated vector drawable in Compose.\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 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\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)"]]