撰写图像/图标渐变色调

Ale*_*lex 8 android android-jetpack-compose

是否可以在撰写布局中使用渐变对矢量图像进行着色?似乎 Modifier.background 为其创建了背景,因此它不是解决方案。ColorFilter之类的还不支持。

jns*_*jns 16

更新

有一个更简单的解决方案,取自 @nebulasmoothie 对Android Jetpack Compose 中的 Text Gradient的回答

Icon(
     modifier = Modifier
        .graphicsLayer(alpha = 0.99f)
        .drawWithCache {
           onDrawWithContent {
             drawContent()
             drawRect(brushGradient, blendMode = BlendMode.SrcAtop)
           }
        },
     imageVector =  Icons.Default.Favorite,
     contentDescription = null,
    )
Run Code Online (Sandbox Code Playgroud)

ImageVector 通过 渲染RenderVectorGroup,可以VectorConfig为每个传递 a VectorPath。文档声明它应该用于动画,所以我不确定它是否是使用它进行渐变的有效方法,但这是我发现的唯一可以传递Brush.

实现 VectorConfig 并使其返回 VectorProperty.Fill 的渐变:

class GradientConfig(private val brush: Brush) : VectorConfig {

    override fun <T> getOrDefault(property: VectorProperty<T>, defaultValue: T): T {
        return when (property) {
            is VectorProperty.Fill -> brush as T
            else -> super.getOrDefault(property, defaultValue)
        }
    }
}

@Composable
fun GradientIcon(image: ImageVector, gradientConfig: GradientConfig) {

    val configs = hashMapOf<String, VectorConfig>(image.root.name to gradientConfig)

    Icon(
        painter = rememberVectorPainter(image = image, configs = configs),
        contentDescription = null,
    )
}

@Composable
fun rememberVectorPainter(image: ImageVector, configs: Map<String, VectorConfig>): VectorPainter {

    return androidx.compose.ui.graphics.vector.rememberVectorPainter(
        defaultWidth = image.defaultWidth,
        defaultHeight = image.defaultHeight,
        viewportWidth = image.viewportWidth,
        viewportHeight = image.viewportHeight,
        name = image.name,
        tintColor = image.tintColor,
        tintBlendMode = image.tintBlendMode,
        content = { _, _ -> RenderVectorGroup(group = image.root, configs = configs) }
    )
}

@Preview(name = "GradientIcon")
@Composable
fun PreviewGradientIcon() {

    val gradient = Brush.linearGradient(
        colors = listOf(
            Color(0xff9F5CFF),
            Color(0xffF0A966)
        ),
        start = Offset(12f, 0f),
        end = Offset(12f, 24f),
    )

    GradientIcon(
        icon = Icons.Filled.Palette,
        gradientConfig = GradientConfig(gradient)
    )
}  
Run Code Online (Sandbox Code Playgroud)

  • 现在我们可以添加 .graphicsLayer {compositingStrategy = CompositingStrategy.Offscreen },以避免使用 0.99 Alpha 通道 (5认同)