You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Compose中renderInSharedTransitionScopeOverlay后animateEnterExit未解析问题

Compose BOM 2025.01.01中animateEnterExit无法识别问题

我在Android开发者文档及官方示例中了解到,可通过renderInSharedTransitionScopeOverlay().animateEnterExit()为非共享Composable实现Shared Element Transition动画,但使用Compose BOM 2025.01.01版本时,编写以下代码出现Unresolved reference: animateEnterExit错误:

with(sharedTransitionScope) {
    BottomCard(
        modifier = Modifier
            .renderInSharedTransitionScopeOverlay(
                zIndexInOverlay = Float.MAX_VALUE,
            )
            .animateEnterExit(
                enter = fadeIn() + slideInVertically {
                    it
                },
                exit = fadeOut() + slideOutVertically {
                    it
                }
            ),
    )
}

使用的Compose版本:composeBom = "2025.01.01"


问题原因

Compose BOM 2025.01.01对应Compose 1.7.x版本,此版本中animateEnterExit作为Modifier的扩展已被移除,该API属于旧版共享过渡实现,现已被新的API体系替代。

替代解决方案

方案1:为Composable直接传入过渡参数

如果BottomCard支持接收enterTransition和exitTransition参数,可直接在SharedTransitionScope上下文中配置:

with(sharedTransitionScope) {
    BottomCard(
        modifier = Modifier
            .renderInSharedTransitionScopeOverlay(zIndexInOverlay = Float.MAX_VALUE),
        enterTransition = fadeIn() + slideInVertically { it },
        exitTransition = fadeOut() + slideOutVertically { it }
    )
}

方案2:使用AnimatedVisibility包裹

若BottomCard不支持直接传入过渡参数,可在外层用AnimatedVisibility实现动画:

with(sharedTransitionScope) {
    AnimatedVisibility(
        visible = true, // 根据业务逻辑控制可见性状态
        enter = fadeIn() + slideInVertically { it },
        exit = fadeOut() + slideOutVertically { it }
    ) {
        BottomCard(
            modifier = Modifier
                .renderInSharedTransitionScopeOverlay(zIndexInOverlay = Float.MAX_VALUE)
        )
    }
}

方案3:确认依赖配置

确保项目已通过Compose BOM正确引入动画相关依赖(BOM会自动管理版本,无需单独指定):

implementation platform("androidx.compose:compose-bom:2025.01.01")
implementation "androidx.compose.material3:material3"
implementation "androidx.compose.animation:animation"

内容的提问来源于stack exchange,提问作者user924

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 12:47:13