如何在 Android Jetpack Compose 中绘制圆形图像?

时间:2021-02-02 17:38:26

标签: android android-jetpack android-jetpack-compose

假设我有一个像下面这样的矩形头像,如何在 Jetpack Compose 中强制将其绘制为圆形?

enter image description here

1 个答案:

答案 0 :(得分:24)

有一个 clip 修饰符可以应用于任何可组合以及 Image,只需将 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)
)

enter image description here

您可以使用任何其他形状来裁剪图像,例如 CircleShape 它只是 RoundedCornerShape(percent = 50)。让我们试试RoundedCornerShape(percent = 10)

enter image description here