gpt4 book ai didi

android - 撰写图像/图标渐变色调

转载 作者:行者123 更新时间:2023-12-04 23:54:50 26 4
gpt4 key购买 nike

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

最佳答案

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

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

ImageVector 通过 RenderVectorGroup 渲染, 可以通过 VectorConfig对于每个 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)
)
}

关于android - 撰写图像/图标渐变色调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69873776/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com