Compose Shared Element

395 stars
by Maciej Ciemiega

Experiment with SharedElement transition in Jetpack Compose, inspired by Flutter Hero widget.

View on GitHub

Documentation

compose-shared-element

Note: This is not meant to be a maintained library.

Proof of concept exploration of how to implement SharedElement transition in Jetpack Compose.
Inspired by Flutter Hero widget.

Transition consists of following animations:

  • Position
  • Scale
  • Crossfade (which makes it work on any kind of element, e.g. Text)

UsersListScreen:
UsersListScreen


UserDetailScreen:
UserDetailScreen
Slowed down transition animation:
TransitionAnimation

Usage

  1. Define FROM and TO elements with shared tag
@Composable
fun ScreenA() {
  // ...
  
    SharedElement(tag = "tag", type = SharedElementType.FROM) {
        Image(image, Modifier.preferredSize(48.dp))
    }
  
  // ...
}

@Composable
fun ScreenB() {
  // ...
  
    SharedElement(tag = "tag", type = SharedElementType.TO) {
        Image(image, Modifier.preferredSize(200.dp))
    }
    
  // ...
}
  1. Make SharedElementsRoot a common parent of ScreenA and ScreenB. It doesn't have to be their direct parent.
SharedElementsRoot {
    // change between ScreenA and ScreenB
}
  1. Transition starts automatically when second SharedElement is detected

Sample

See sample usage with UsersListScreen and UserDetailsScreen here.

Issues

Considering the experimental state of this library and state of Jetpack Compose itself there are some issues described here.

Dispatch Newsletter
Be the first to discover new Compose libraries
Curated Insights
Digest in 5 minutes or less
Android Analysis
Entertaining takes on happenings
Insider Tips
From top Android developers
Hidden Gems
You won't find elsewhere
"
"Dispatch is a must read for Android devs today and my go-to for keeping up with all things Jetpack Compose. I eagerly await each issue, not just for the Compose gems honestly but for Vinay's unique insights, stories, and finds. The Dev Delight and Featured Dev sections are particular favorites of mine! He really should do a podcast."
Kaushik Gopal
Principal Engineer @ Instacart | Host of the Fragmented Podcast | GDE for Android
Join thousands of Android devs who look forward to Dispatch every week