如何在AndroidJetpackCompose中绘制圆形图像?
假设我有一个像下面这样的矩形头像图像,如何在 Jetpack Compose 中强制将其绘制为圆形?
回答
有一个剪辑修饰符可以应用于任何可组合以及Image,只需将 a 传递给CircleShape它:
Image(
painter = painterResource(R.drawable.sample_avatar),
contentDescription = "avatar",
contentScale = ContentScale.Crop, // crop the image if it's not a square
modifier = Modifier
.size(64.dp)
.clip(CircleShape) // clip to the circle shape
.border(2.dp, Color.Gray, CircleShape) // add a border (optional)
)
您可以使用任何其他形状来剪辑图像,例如CircleShape它只是RoundedCornerShape(percent = 50). 让我们试试RoundedCornerShape(percent = 10):