You need to enable JavaScript to run this app. Jetpack Compose 浅色模式下AnimatedVisibility淡入动画异常问题咨询
优惠活动
大模型
产品
解决方案
定价
更多

Jetpack Compose 浅色模式下AnimatedVisibility淡入动画异常问题咨询

问题根因

该异常和深浅色模式本身无直接关联,属于动画时序和布局变化的叠加效应,触发原因有三个:

  1. 容器高度动画和文本淡入动画的时序重叠:容器高度动画从第800ms启动、持续到1600ms,文本淡入从第1000ms启动,淡入过程中容器高度仍在动态增长,父容器设置了居中对齐,文本的位置会随容器高度增加不断上移,和淡入效果叠加后呈现从底部滑入的表现。
  2. 文本添加了animateContentSize()修饰符:容器宽度增长过程中,文本换行次数减少、自身高度降低,animateContentSize会对这个高度变化施加动画,进一步加剧了位移效果。
  3. 深色模式下未复现属于偶然情况:你通过隐藏Text测量宽高的逻辑受主题渲染规则影响,深色模式下的测量值刚好匹配了动画时序,未触发明显位移。

修复方案

方案1:替换为透明度动画(改动最小,推荐)

仅需要淡入效果的场景下,无需使用AnimatedVisibility,直接通过透明度动画实现,完全规避布局可见性切换带来的额外布局干扰:

@ExperimentalAnimationApi
@Composable
fun TestExpandHorizontally() {
    var text =
        "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book."

    var widthState by remember { mutableStateOf(10.dp) }
    val width by animateDpAsState(
        targetValue = widthState,
        tween(
            durationMillis = 800,
            easing = LinearOutSlowInEasing
        )
    )

    var heightState by remember { mutableStateOf(10.dp) }
    val height by animateDpAsState(
        targetValue = heightState,
        tween(
            delayMillis = 800,
            durationMillis = 800,
            easing = LinearOutSlowInEasing
        )
    )

    // 替换MutableTransitionState为透明度动画状态
    var showText by remember { mutableStateOf(false) }
    val textAlpha by animateFloatAsState(
        targetValue = if (showText) 1f else 0f,
        animationSpec = tween(delayMillis = 1000, durationMillis = 1000)
    )

    var textWidth by remember { mutableStateOf(0) }
    var textHeight by remember { mutableStateOf(0) }

    // 原有隐藏测量文本区域保留即可
    Column(modifier = Modifier
        .fillMaxSize()
        .background(MaterialTheme.colors.background)) {
        Column(
            horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center, modifier = Modifier
                .wrapContentWidth()
                .wrapContentHeight()
                .background(MaterialTheme.colors.background)
                .padding(20.dp)
        ) {
            Text(
                text = text,
                modifier = Modifier
                    .padding(20.dp)
                    .background(MaterialTheme.colors.background)
                    .onSizeChanged {
                        textWidth = (it.width.toFloat() / 2.2.toFloat()).toInt()
                        textHeight = (it.height.toFloat() / 2.2.toFloat()).toInt()
                    }
                    ,
                color = MaterialTheme.colors.background,
                fontFamily = FontFamily.SansSerif
            )
        }
    }

    Column(horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center, modifier = Modifier.padding(20.dp)) {
        Column(
            Modifier
                .width(width)
                .height(height)
                .background(Color.Gray),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            // 替换AnimatedVisibility为普通Text,加alpha修饰符,删掉animateContentSize
            Text(
                text = text,
                modifier = Modifier
                    .padding(20.dp)
                    .alpha(textAlpha),
                color = MaterialTheme.colors.onBackground,
                fontFamily = FontFamily.SansSerif
            )
        }

        Button(
            onClick = {
                widthState += textWidth.dp 
                heightState += textHeight.dp
                showText = true // 触发透明度动画
            }, colors = ButtonDefaults.buttonColors(
                backgroundColor = Color.Blue
            )
        ) {
            Text(text = "Press here", color = Color.White)
        }
    }
}

方案2:固定文本尺寸(保留AnimatedVisibility的场景)

如果需要保留AnimatedVisibility的其他能力,可以固定文本的宽高为提前测量的值,避免文本布局随容器变化产生位移,同时可以替换隐藏测量逻辑为官方TextMeasurer获得更准确的尺寸:

// 给展示的Text设置固定宽高,删除animateContentSize
AnimatedVisibility(
    visibleState = textVisibility,
    enter = fadeIn(animationSpec = tween(delayMillis = 1000, durationMillis = 1000))
) {
    Text(
        text = text,
        modifier = Modifier
            .size(textWidth.dp, textHeight.dp) // 固定为提前测量的尺寸
            .padding(20.dp),
        color = MaterialTheme.colors.onBackground,
        fontFamily = FontFamily.SansSerif
    )
}

两种方案均可实现文本在容器内直接淡入的预期效果,无额外滑入位移。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 21:57:04