我试图使用BlendMode.SRC_IN在PNG图片(透明背景)上方添加覆盖颜色,但背景变成了黑色,好像也遮挡了背景像素一样。
@Composable
fun Icon(
fraction: Float,
image: ImageAsset,
defaultColor: Color = Color(0xFFEEEEEE),
progressColor: Color = Color(0xFF888888),
size: Dp = image.width.dp
) {
Box(modifier = Modifier.size(size = size)) {
Canvas(modifier = Modifier.fillMaxSize()) {
drawImage(
image = image,
dstSize = IntSize(
width = size.toIntPx(),
height = size.toIntPx()
),
colorFilter = ColorFilter.tint(
color = defaultColor
),
blendMode = BlendMode.Src
)
drawIntoCanvas {
val paint = Paint().apply {
color = progressColor
blendMode = BlendMode.SrcIn
}
it.restore()
it.drawRect(
rect = Rect(
offset = Offset.Zero,
size = Size(
width = size.toPx() * fraction,
height = size.toPx()
)
),
paint = paint
)
it.save()
}
}
}
}
以下是我在一个Screen(color = Color.WHITE)
上排列多个Icon()
的样子,
Surface(color = Color.White) {
Row {
listOf(
R.drawable.anemo,
R.drawable.cryo,
R.drawable.dendro,
R.drawable.electro,
R.drawable.geo,
R.drawable.hydro,
R.drawable.pyro
).forEachIndexed { index, imageRes ->
val from = 100f/7 * index
val to = 100f/7 * (index + 1)
val fraction = when {
progress > to -> 1f
progress > from -> (progress - from)/(to - from)
else -> 0f
}
Icon(
fraction = fraction,
image = imageResource(id = imageRes),
size = 50.dp
)
}
}
}
我希望得到的结果是,
![result](https://istack.dev59.com/NtzFn.webp)
这里是我尝试的Github代码库。