所以我正在try 做一个简单的应用程序,将 colored颜色 更改为红色或绿色,并在价格波动时恢复为黑色,我目前的实现是这样的

@Composable
fun LaunchingComposable() {
 var coinPrice by remember {
        mutableStateOf(2000.30)
    }  

CoinHeader(modifier = Modifier.padding(horizontal = 8.dp),
                        "http://myicon.com/image.png",
                        coinPrice
                    )
                    LaunchedEffect(Unit) {
                        delay(3000)
                        coinPrice = 2000.40
                        delay(3000)
                        coinPrice = 2000.20
                        delay(3000)
                        coinPrice = 2000.10
                        delay(3000)
                        coinPrice = 2000.20
                    }
}

...

@Composable
fun CoinHeader(modifier: Modifier, coinImageUrl: String, currentPrice: Double) {
    val baseColor = remember { Animatable(Black) }
    val previousPrice = remember {
        currentPrice
    }

        Row(
        modifier = modifier,
        horizontalArrangement = Arrangement.Start,
        verticalAlignment = Alignment.CenterVertically
    ) {
        AsyncImage(
            modifier = Modifier
                .width(30.dp)
                .height(30.dp)
                .padding(end = 4.dp)
                .data(coinImageUrl)
                .build()
          
        )

        LaunchedEffect(currentPrice) {
            if (previousPrice < currentPrice) {
                baseColor.animateTo(Red, animationSpec = tween(1000))
            } else {
                baseColor.animateTo(Green, animationSpec = tween(1000))
            }
            baseColor.animateTo(Black, animationSpec = tween(1000))
        }
        Text(text = currentPrice.toPrice(), color = baseColor.targetValue)
    }
}

在我的上一个作曲中,我预计值将更改为Green - Red - Red - Green

我需要总是存储我的硬币价格的前值,以便比较它,做一个褪色动画与 colored颜色 ,然后返回到黑色.

目前,这是我的输出

enter image description here

问题有2个

  • 不会发生从红色到黑色或从绿色到黑色的淡入淡出
  • 似乎总是与第一个值进行比较

有没有人能解释一下,如果我在更改了CoinPrice之后重新组合,为什么之前的值设置不正确?

推荐答案

你们目前实施的previousPrice实际上只是原始价格,因为它永远不会改变.您从未将其设置为新值,因此它将永远保存收到的第一个currentPrice.您的remember调用甚至没有键,因此它永远不会重新计算它,但即使您重新计算了它,也无法将其计算为先前的值而不是当前的值.

我认为您必须在记住的前一个价格周围使用一个可变的包装类,这样您才能实际更改它.一个数组可能就足够了,或者您可以编写一个特定的数据类来包装一个var.

大概是这样的:

val rememberedPreviousPrice = remember { arrayOf(currentPrice) }
val previousPrice = rememberedPreviousPrice[0]
rememberedPreviousPrice[0] = currentPrice

其次,您使用的是baseColor.targetValue而不是baseColor.value,因此它不使用动画值,而是直接跳到最终("目标") colored颜色 .

您的代码中可能存在其他问题.我不确定,因为我自己还没有在构图中做太多的LaunchedEffect或动画.


顺便说一下,货币不能用双精度.请改用BigDecimal.阅读herehere.

Android相关问答推荐

Android:点击操作栏返回按钮后应用程序无react

无法列出目录中的文件'

如何完全隐藏的元素堆叠在CardView?

关于BLE扫描工作原理的说明

如何从Android 12的来电中获取电话号码?

原始mp3文件不显示与proguard

如何将两个变量传递给Nav主机,然后将其传递给另一个屏幕?

使用Android Jetpack Compose,为子Composable定义ViewModel是不是一种糟糕的做法?

为什么我在 android 中使用 TabLayout 时无法启动我的 Activity?

activity在 Runnable 中如何工作?我的 Android 表格未显示

Jetpack Compose 使用 SavedStateHandle 发送返回结果不适用于 ViewModel 中注入的 SavedStateHandle

如何在jetpack compose中使可组合的屏幕zoom 到不同的手机(屏幕)尺寸?

从 HiltViewModel @Injection 访问 Application()

系统导航栏在某些场景下应用了深色效果

如何以编程方式通过 whatsapp android 共享图像和文本

Jetpack Compose Canvas drawText colored颜色 混合?

是否可以在 Kotlin 中为 mutableStateOf() 设置自定义设置器

Kotlin Compose forEach 中的负间距

Jetpack Compose Material3 - switch 标签

如何使用 Kotlin 在 Android Wear(Galaxy watch 4)中继续在后台运行应用程序