Google I/O 2024: Shared Ingredient Transitions in Jetpack Compose


Google I/O 2024 launched so many thrilling new applied sciences, particularly Gemini AI and Jetpack Compose. Although Jetpack Compose is superior and bettering quickly to meet up with the legacy XML-based layouts (which have been on the market for ages), it fell brief in some areas, equivalent to animations.

Shared Ingredient Transitions are among the many hottest APIs from the Android Animation Framework, which wasn’t out there within the Jetpack Compose till now. That’s proper – Google launched shared factor transitions for Jetpack Compose at their 2024 I/O occasion!

This long-awaited characteristic helps you create stunning, fluid animations when navigating between screens in your app. Think about a consumer tapping a picture in a listing, and it easily expands and animates into the detailed view. Shared factor transitions in Compose present a declarative technique to obtain this impact, providing you with extra management over the animation course of than the normal View system. This empowers builders to design seamless consumer experiences that improve their apps’ total feel and appear.

Listed here are a few of the key capabilities of Shared Ingredient Transitions in Jetpack Compose launched at Google I/O 2024:

Getting Began

To make the most of the newest APIs, be sure you’re utilizing the newest Android Studio Jellyfish | 2023.3.1 and API Stage 34.

Click on the Obtain Supplies button on the prime or backside of this tutorial. Unzip the ComposeTransitionAnimation.zip folder.

Now, launch Android Studio and open ComposeTransitionAnimation-Starter to import the starter mission. The ComposeTransitionAnimation-Starter mission comprises the required boilerplates and Composables to leap straight into the animation!

ComposeTransitionAnimation-Starter resembles an e-commerce app with a fundamental Listing-Element structure.

Construct and run the app – it’ll appear like this:

On this article, you’ll create a visible connection between components on Listing and Element screens utilizing Shared Ingredient Transition.

First, add the newest model of Compose dependencies. Open construct.gradle in your app module and replace:

  • Declarative Animation: Shared factor transitions are outlined declaratively utilizing modifiers like Modifier.sharedElement and Modifier.sharedBoundsMatchingContentSize. This animation course of is way less complicated in comparison with the crucial strategy required within the View system.
  • Finer Management: Compose gives extra granular management over the animation in comparison with conventional strategies. You’ll be able to outline the particular factor to animate, its transition bounds, and even the animation kind.
  • Seamless Integration With Navigation: Shared factor transitions work easily with Navigation Compose. When navigating between screens, you possibly can go the factor’s key as an argument, and Compose robotically matches components and creates the animation.
    • Getting Began

      To make the most of the newest APIs, be sure you’re utilizing the newest Android Studio Jellyfish | 2023.3.1 and API Stage 34.

      Click on the Obtain Supplies button on the prime or backside of this tutorial. Unzip the ComposeTransitionAnimation.zip folder.

      Now, launch Android Studio and open ComposeTransitionAnimation-Starter to import the starter mission. The ComposeTransitionAnimation-Starter mission comprises the required boilerplates and Composables to leap straight into the animation!

      ComposeTransitionAnimation-Starter resembles an e-commerce app with a fundamental Listing-Element structure.

      Construct and run the app – it’ll appear like this:

      On this article, you’ll create a visible connection between components on Listing and Element screens utilizing Shared Ingredient Transition.

      First, add the newest model of Compose dependencies. Open construct.gradle in your app module and replace:

    Getting Began

    To make the most of the newest APIs, be sure you’re utilizing the newest Android Studio Jellyfish | 2023.3.1 and API Stage 34.

    Click on the Obtain Supplies button on the prime or backside of this tutorial. Unzip the ComposeTransitionAnimation.zip folder.

    Now, launch Android Studio and open ComposeTransitionAnimation-Starter to import the starter mission. The ComposeTransitionAnimation-Starter mission comprises the required boilerplates and Composables to leap straight into the animation!

    ComposeTransitionAnimation-Starter resembles an e-commerce app with a fundamental Listing-Element structure.

    Construct and run the app – it’ll appear like this:

    On this article, you’ll create a visible connection between components on Listing and Element screens utilizing Shared Ingredient Transition.

    First, add the newest model of Compose dependencies. Open construct.gradle in your app module and replace:


def composeVersion = "1.7.0-beta01"

Faucet Sync Now to obtain the dependencies.

Observe: Shared factor help is experimental and is in `beta`. The APIs might change sooner or later.

Overview of Key APIs

The newest dependencies launched a couple of high-level APIs that do the heavy lifting of sharing components between Composable layouts:

  • SharedTransitionLayout: The highest-level structure required to implement shared factor transitions. It gives a SharedTransitionScope. A Composable must be in SharedTransitionScope to make use of the modifiers of shared components.
  • Modifier.sharedElement(): The modifier to flag one Composable to be matched with one other Composable inside the SharedTransitionScope.
  • Modifier.sharedBounds(): The modifier that tells the SharedTransitionScope to make use of this Composable’s bounds because the container bounds for the place the transition ought to happen.

You’ll quickly create a hero-animation utilizing these APIs.

Implementing Shared Transition Animation

A Shared Transition Animation, or hero-animation, consists of three main steps:

  1. Wrapping collaborating views with SharedTransitionLayout.
  2. Defining SharedTransitionScope to the supply and vacation spot views.
  3. Transition with Shared Ingredient.

Including SharedTransitionLayout

Open the MainActivity class. It comprises ListScreen and DetailScreen, which is able to share components throughout a transition animation. As talked about earlier, you will need to wrap them with SharedTransitionLayout to make them eligible for a Shared Transition Animation.

Replace the AnimatedContent block as follows:


SharedTransitionLayout {
  AnimatedContent(
    targetState = showDetails, 
    label = "shared_transition"
  ) { shouldShowDetails ->
    if (!shouldShowDetails) {
      ListScreen(
        // Present code
        ... ... ...
      )
    } else {
      DetailScreen(
        // Present code
        ... ... ...
       )
     }
  }
}

At this level, you may even see this warning from Android Studio for utilizing an experimental api:

To resolve this, add these imports on prime of the MainActivity:


import androidx.compose.animation.ExperimentalSharedTransitionApi
import androidx.compose.animation.SharedTransitionLayout

Then add this annotation over the onCreate(savedInstanceState: Bundle?) methodology:


@OptIn(ExperimentalSharedTransitionApi::class)

Construct and run.

Defining SharedTransitionScope

Up subsequent, it is advisable to outline SharedTransitionScope to the views collaborating within the transition animation. The Composable must be inside SharedTransitionScope to make use of Modifier.sharedElement() for the animation. Therefore, you’ll must go down SharedTransitionScope from SharedTransitionLayout in MainActivity to the supply and vacation spot Composable executing the animation.

On this case, you’ll transition from the smaller Picture Composable within the ListScreen (supply) to the bigger Composable in DetailScreen (vacation spot).

Begin with ListScreen.kt inside ui package deal. Replace the ListScreen operate with these parameters:


@Composable
enjoyable ListScreen(
  paddingValues: PaddingValues,
  gadgets: Listing<Merchandise>,
  onItemClicked: (Merchandise) -> Unit = {},
  sharedTransitionScope: SharedTransitionScope,
  animatedVisibilityScope: AnimatedVisibilityScope,
)

Then go the sharedTransitionScope and animatedVisibilityScope references for every ListItem:


gadgets.forEach { merchandise ->
  ListItem(
    merchandise = merchandise,
    onItemClicked = onItemClicked,
    sharedTransitionScope = sharedTransitionScope,
    animatedVisibilityScope = animatedVisibilityScope,
  )
}

Additionally, replace th eListItem Composable methodology signature accordingly:


@Composable
enjoyable ListItem(
  merchandise: Merchandise,
  onItemClicked: (Merchandise) -> Unit = {},
  sharedTransitionScope: SharedTransitionScope,
  animatedVisibilityScope: AnimatedVisibilityScope,
)

You’ll see the warning for utilizing an experimental api once more from the compiler, together with the errors for the lacking imports.

Fret not! Add these imports on prime:


import androidx.compose.animation.AnimatedVisibilityScope
import androidx.compose.animation.ExperimentalSharedTransitionApi
import androidx.compose.animation.SharedTransitionScope

And the annotation for the ListScreen.kt file, above of the package deal title like this:


@file:OptIn(ExperimentalSharedTransitionApi::class)

package deal com.kodeco.android.composetransition.ui

That ensures you will have all the required imports and can mute warnings for utilizing experimental APIs for the scope of the ListScreen.kt file.

Observe: Add the imports and annotation on DetailScreen.kt, too. You’ll want them shortly!

Your vacation spot Composable is the DetailScreen methodology. Now add animation scopes as methodology parameters as follows:


@Composable
enjoyable DetailScreen(
  merchandise: Merchandise, onBack: () -> Unit,
  sharedTransitionScope: SharedTransitionScope,
  animatedVisibilityScope: AnimatedVisibilityScope,
)

You’re able to wire up ListScreen and DetailScreen to carry out the transition animation.

Open MainActivity and replace SharedTransitionLayout block to go animatedVisibilityScope and sharedTransitionScope to its descendants:


SharedTransitionLayout {
  AnimatedContent(
    targetState = showDetails, 
    label = "shared_transition"
  ) { shouldShowDetails ->
    if (!shouldShowDetails) {
      ListScreen(
        paddingValues = paddingValues,
        gadgets = gadgets.worth,
        onItemClicked = { merchandise ->
          detailItem = merchandise
          showDetails = !showDetails
        },
        animatedVisibilityScope = this@AnimatedContent,
        sharedTransitionScope = this@SharedTransitionLayout,
      )
    } else {
      DetailScreen(
        merchandise = detailItem,
        onBack = { showDetails = !showDetails },
        animatedVisibilityScope = this@AnimatedContent,
        sharedTransitionScope = this@SharedTransitionLayout,
      )
    }
  }
}

Construct and run once more to make sure you resolved all compilation errors, however don’t count on the animation to occur but!

Recent Articles

Related Stories

Leave A Reply

Please enter your comment!
Please enter your name here

Stay on op - Ge the daily news in your inbox