当前代码

@Composable
fun TextDemo() {
    var selectedIndex by remember {
        mutableIntStateOf(0)
    }
    Row {
        TabText(
            text = "First",
            isSelected = selectedIndex == 0,
            onClick = {
                selectedIndex = 0
            },
        )
        TabText(
            text = "Second",
            isSelected = selectedIndex == 1,
            onClick = {
                selectedIndex = 1
            },
        )
    }
}

@Composable
fun TabText(
    text: String,
    isSelected: Boolean,
    onClick: () -> Unit,
) {
    val tabTextColor by animateColorAsState(
        targetValue = if (isSelected) {
            Color.Red
        } else {
            Color.Black
        },
        animationSpec = tween(
            easing = LinearEasing,
        ),
        label = "tab_text_color",
    )

    Text(
        modifier = Modifier
            .padding(8.dp)
            .clickable {
                onClick()
            },
        text = text,
        color = tabTextColor,
    )
}

用户界面可供参考 Row中的两个Text

UI Screenshot

版面判断器重组

Screenshot of Layout inspector

Question

How to reduce the recompositions when text color changes?

对于alphatransition等属性,可以在使用Modifier.graphicsLayer {}设置动画时避免重新组合

具有alpha个动画而不是 colored颜色 的相同代码在每次 Select 更改时仅重组一次.

Layout inspector screenshot

代码

@Composable
fun TabText(
    text: String,
    isSelected: Boolean,
    onClick: () -> Unit,
) {
    val alphaValue by animateFloatAsState(
        targetValue = if (isSelected) {
            1F
        } else {
            0.5F
        },
        animationSpec = tween(
            easing = LinearEasing,
        ),
        label = "tab_text_color",
    )

    Text(
        modifier = Modifier
            .graphicsLayer {
                alpha = alphaValue
            }
            .padding(8.dp)
            .clickable {
                onClick()
            },
        text = text,
    )
}

推荐答案

首先,当您记录读取State的重组时,最好在SideEffect以内完成,否则有可能得到误报,因为记录本身也被算作State read.

要对文本 colored颜色 更改进行一次重组,您可以在Tab中使用Canvas或任何绘制修饰符,并且只调用绘制阶段,而使用DrawScopeTextMeasurerdrawText函数进行Color更改.

第二种 Select 是将BlendModes与Modifier.draContent{}一起使用,通过一次重新合成来更改 colored颜色

@Preview
@Composable
private fun TextAnimationTest() {

    var isSelected by remember {
        mutableStateOf(false)
    }

    SideEffect {
        println("Recomposing...")
    }

    val tabTextColor by animateColorAsState(
        targetValue = if (isSelected) {
            Color.Red
        } else {
            Color.Black
        },
        animationSpec = tween(
            easing = LinearEasing,
        ),
        label = "tab_text_color",
    )


  Column {
      Button(
          onClick = {
              isSelected = isSelected.not()
          }
      ) {
          Text("Selected: $isSelected")
      }

      Text("Some Text", modifier = Modifier
          .graphicsLayer {
              compositingStrategy = CompositingStrategy.Offscreen
          }
          .drawWithContent {
              drawContent()

              drawRect(
                  color = tabTextColor,
                  blendMode = BlendMode.SrcIn
              )
          }
      )
  }
}

Android相关问答推荐

垂直居中图标

错误:无法解析Symbol@style/Theme. Androidstudio in AndroidManifest.html for Kotlin Android Development''

CameraX与jetpack组成屏幕逻辑

将Any强制转换为Integer将从API返回NullPointerException

如何让用户与我的应用生成的多个音频文件交互

list 合并失败,AGP 8.3.0

Android应用程序中的背景问题

使用Kotlin/Compose与Java/XML指南的比较

保护所有程序包文件和类

为什么柱子的高度不都一样?

如何判断堆肥是否为空?

(已解决)从最近的应用程序打开应用程序时出错

如何使用 Jetpack Compose 在图像上叠加文本

在 Jetpack Compose 中清除列表时可组合不重组

如何在jetpack compose中通过lamda返回columnScope/RowScope

viewModel 的可变值状态不起作用

为什么我不能直接记住 mutableStateOf 可组合函数?

使用 capacitor cordova 插件的 Android Studio 错误

基线配置文件 x R8/Proguard

ObjectBox,如何在冲突中放弃一切迁移?