如何在 Android Jetpack Compose 中绘制圆形图像? [英] How to draw a circular image in Android Jetpack Compose?

查看:227
本文介绍了如何在 Android Jetpack Compose 中绘制圆形图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

有一个

您可以使用任何其他形状来裁剪图像,例如 CircleShape 它只是

Let's say I have a rectangular avatar image like the one below, how can I force it to be drawn as a circle in Jetpack Compose?

解决方案

There's a clip modifier which can be applied to any composable as well as the Image, just pass a CircleShape into it:

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)
)

You can use any other shape to clip the image, for example CircleShape it's just RoundedCornerShape(percent = 50). Let's try RoundedCornerShape(percent = 10):

这篇关于如何在 Android Jetpack Compose 中绘制圆形图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆