admin管理员组

文章数量:1123788

I am trying out the relatively new Modifier.sharedBounds() in Jetpack Compose to animate between a Card and the next screen, which is a Scaffold.

The problem is, that the background of the Card fades out at the same time as the content of it fades out (it is using the same enter and exit transition). This leads to ugly in-between frames where the whole card is half transparent and the content beneath it can be seen.

I would like for the card to remain opaque with only the content fading between the initial and the target view.
Similar to how it used to be done with container transform.

Is that possible?

Here is some code of what I already have for better understanding.

The Card:

ElevatedCard(
    modifier = Modifier
        ...
        .sharedBounds(
            sharedContentState = sharedTransitionScope.rememberSharedContentState(key = "bounds_$id"),
            animatedVisibilityScope = animatedVisibilityScope,
            resizeMode = SharedTransitionScope.ResizeMode.RemeasureToBounds,
            placeHolderSize = SharedTransitionScope.PlaceHolderSize.animatedSize,
        )
) {
    ... // content of the Card
}

The Scaffold:

Scaffold(
    modifier = Modifier
        ...
        .sharedBounds(
            sharedContentState = sharedTransitionScope.rememberSharedContentState(key = "bounds_$id"),
            animatedVisibilityScope = animatedVisibilityScope,
            resizeMode = SharedTransitionScope.ResizeMode.RemeasureToBounds,
        )
) { 
    ... // content of the target screen
}

If I understand correctly, the developer guide mentions the problem briefly in their YouTube video at timestamp 14:32, but I was unable to figure out the solution.

I have tried messing with the Modifier.renderInSharedTransitionOverlay() and I tried changing the zIndexOverlay in the Modifier.sharedBounds(), but to no avail.

My questions:

  • Is it possible to have the container (in this case the Card) remain opaque while the content is fading to the target state?
  • If not, are there workarounds?

I am trying out the relatively new Modifier.sharedBounds() in Jetpack Compose to animate between a Card and the next screen, which is a Scaffold.

The problem is, that the background of the Card fades out at the same time as the content of it fades out (it is using the same enter and exit transition). This leads to ugly in-between frames where the whole card is half transparent and the content beneath it can be seen.

I would like for the card to remain opaque with only the content fading between the initial and the target view.
Similar to how it used to be done with container transform.

Is that possible?

Here is some code of what I already have for better understanding.

The Card:

ElevatedCard(
    modifier = Modifier
        ...
        .sharedBounds(
            sharedContentState = sharedTransitionScope.rememberSharedContentState(key = "bounds_$id"),
            animatedVisibilityScope = animatedVisibilityScope,
            resizeMode = SharedTransitionScope.ResizeMode.RemeasureToBounds,
            placeHolderSize = SharedTransitionScope.PlaceHolderSize.animatedSize,
        )
) {
    ... // content of the Card
}

The Scaffold:

Scaffold(
    modifier = Modifier
        ...
        .sharedBounds(
            sharedContentState = sharedTransitionScope.rememberSharedContentState(key = "bounds_$id"),
            animatedVisibilityScope = animatedVisibilityScope,
            resizeMode = SharedTransitionScope.ResizeMode.RemeasureToBounds,
        )
) { 
    ... // content of the target screen
}

If I understand correctly, the developer guide mentions the problem briefly in their YouTube video at timestamp 14:32, but I was unable to figure out the solution.

I have tried messing with the Modifier.renderInSharedTransitionOverlay() and I tried changing the zIndexOverlay in the Modifier.sharedBounds(), but to no avail.

My questions:

  • Is it possible to have the container (in this case the Card) remain opaque while the content is fading to the target state?
  • If not, are there workarounds?
Share Improve this question asked yesterday EmilEmil 4405 silver badges13 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Yeah man its possible. you can have the container(card in your case) and remain opaque while the content is fading to the targeted state. You can follow my approach.

ElevatedCard(
    modifier = Modifier
        .sharedBounds(
            sharedContentState = sharedTransitionScope.rememberSharedContentState(key = "bounds_$id"),
            animatedVisibilityScope = animatedVisibilityScope,
            resizeMode = SharedTransitionScope.ResizeMode.RemeasureToBounds,
            placeHolderSize = SharedTransitionScope.PlaceHolderSize.animatedSize,
        )
) {
    Box(
        modifier = Modifier.fillMaxSize()
    ) {
        // here the card backgnd remains opaque
        Box(modifier = Modifier.fillMaxSize().background(Color.Gray))

        // so the content inside here fades out separately
        AnimatedVisibility(
            visible = /*provide the condition state/,
            enter = fadeIn(),
            exit = fadeOut()
        ) {
            // the actual content of the card
            Column {
               //here just used a text composable to show you
                Text("Card Content")
               
            }
        }
    }
}

And the other thing is if you want the transition with a Scaffold then you dont need to do anything extra as long as it properly syncs with the sharedBounds() mod you already have there.

here is a sample spinnet

Scaffold(
    modifier = Modifier
        .sharedBounds(
            sharedContentState = sharedTransitionScope.rememberSharedContentState(key = "bounds_$id"),
            animatedVisibilityScope = animatedVisibilityScope,
            resizeMode = SharedTransitionScope.ResizeMode.RemeasureToBounds,
        )
) { 
    // Content of the target screen
}

本文标签: androidJetpack Compose shared bounds background transparencyStack Overflow