如何将android.graphics.Color转换为androidx.compose.ui.graphics.Color

时间:2020-10-11 11:18:56

标签: android android-jetpack-compose

我正在绘制一个JetpackCompose Canvas

    Canvas(modifier = modifier.clipToBounds()) {
        val colorEvaluator = ArgbEvaluator()
        colorEvaluator.evaluate(t, Color.Red, Color.Blue)
        drawCircle(
            style = Stroke(8.dp.toPx()),
            color = AndroidColor.valueOf(colorEvaluator.evaluate(t, Color.Red, Color.Blue) as Int),
            center = Offset(size.minDimension/2 , size.minDimension/2),
            radius = size.minDimension * t)
        drawRect(
            color = darkColor,
            style = Stroke(16.dp.toPx()),
        )
    }

哪里

import androidx.compose.ui.graphics.Color
import android.graphics.Color as AndroidColor

我在错误消息所在的color = AndroidColor.valueOf(colorEvaluator.evaluate(t, Color.Red, Color.Blue) as Int),上收到了此错误

Type mismatch.
Required:
androidx.compose.ui.graphics.Color
Found:
android.graphics.Color

1 个答案:

答案 0 :(得分:1)

好像我应该向ArgbEvaluator提供AndroidColor.REDAndroidColor.BLUE,而不是不小心使用Color的{​​{1}}

ComposeAndroidColor的地方

import android.graphics.Color as AndroidColor

已更新

找到了一种更好的方法,即按照以下代码在 Canvas(modifier = modifier.clipToBounds()) { drawCircle( style = Stroke(8.dp.toPx()), color = Color(ArgbEvaluator().evaluate(t, AndroidColor.RED, AndroidColor.BLUE) as Int), center = Offset(size.minDimension / 2, size.minDimension / 2), radius = size.minDimension * t ) drawRect( color = darkColor, style = Stroke(16.dp.toPx()), ) } 中使用lerp。使用这种方法,我们不再需要使用旧的compose

ArgbEvaluator