简体   繁体   English

如何在 Jetpack Compose 中为 TextStyle 设置动画?

[英]How to animate TextStyle in Jetpack Compose?

The text in one of my Composables is struck-through when a certain boolean variable is true.当某个布尔变量为真时,我的一个可组合项中的文本会被删除。 How can I animate this change in TextStyle on recomposition so that the line fades in rather than appearing and disappearing abruptly?如何在重新组合时为TextStyle这种更改设置动画,以便线条淡入而不是突然出现和消失?

@Composable
fun MyComposable(
    completed: Boolean
) {
    val textStyle = TextStyle(textDecoration = if (completed) TextDecoration.LineThrough else null)

    Text(
        text = title,
        color = textColor,
        style = textStyle,
        modifier = Modifier.align(Alignment.CenterVertically)
    )

Not sure if it exists a way to animate a TextStyle .不确定它是否存在动画TextStyle
Not a great solution, but just a workaround:不是一个很好的解决方案,而只是一种解决方法:

Box() {
    AnimatedVisibility(
        visible = !completed,
        enter = fadeIn(
            animationSpec = tween(durationMillis = duration)
        ),
        exit = fadeOut(
            animationSpec = tween(durationMillis = duration)
        )) {
        Text(
            text = title,
            style = TextStyle(textDecoration=null)
        )
    }
    AnimatedVisibility(
        visible = completed,
        enter = fadeIn(
            animationSpec = tween(durationMillis = duration)
        ),
        exit = fadeOut(
            animationSpec = tween(durationMillis = duration)
        )) {
        Text(
            text = title,
            style = TextStyle(textDecoration = TextDecoration.LineThrough),
        )
    }
}

在此处输入图片说明

Gabriele's answer is also a decent workaround, but perhaps a simpler one would be to put the Text and the " Stroke ", in a box, overlapping. Gabriele 的答案也是一个不错的解决方法,但也许更简单的方法是将Text和“ Stroke ”放在一个框中,重叠。 Say,说,

@Composable
fun MyComposable(
    completed: Boolean
) {

Box{
    Text(
        text = title,
        color = textColor,
        style = textStyle,
        modifier = Modifier.align(Alignment.CenterVertically)
    )
  AnimatedVisibility (completed) {
   Box(Modifier.width(1.dp)){
     //Empty Box of unit width to serve as the stroke
    // Add background modifiers to match the font color
   }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 TextStyle 从 Jetpack Compose 转换为 android.graphics.Typeface? - How to convert TextStyle from Jetpack Compose to android.graphics.Typeface? 如何在 Jetpack Compose 中为图像设置动画? - How to Animate An Image in Jetpack Compose? 如何在 Jetpack Compose 中为 LinearProgressIndicator 的进度设置动画? - How to animate the progress of a LinearProgressIndicator in Jetpack Compose? 如何在 Jetpack Compose 中为按钮的宽度设置动画 - How to animate width of a button in Jetpack Compose 如何在 Jetpack Compose 中为动态列表的元素设置动画? - How to animate elements of a dynamic list in Jetpack Compose? 使用 Jetpack Compose 时,如何创建基于 TextStyle 的 MaterialTheme.typography.body2 和 Color.Red 的实例? - How can I create a instance of TextStyle based MaterialTheme.typography.body2 and Color.Red when I use Jetpack Compose? 如何在 Jetpack Compose 中为矩形制作进度条动画 - How to animate a rectangle like a Progress Bar in Jetpack Compose 如何在 Jetpack Compose Android 的 LazyColumn 中显示项目视图的动画 - How to show animate an item view in LazyColumn on Jetpack Compose Android 如何在 Android Jetpack compose 中为初始人口设置动画 - How to animate list initial population in Android Jetpack compose 如何在 Jetpack Compose 中将文本从一种排版动画化到另一种排版 - How to animate text from a typography to another in jetpack compose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM