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?
1 Answer
Reset to default 0Yeah 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
版权声明:本文标题:android - Jetpack Compose shared bounds background transparency - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736598528a1945177.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论