裁剪并设置图像形状
设置圆角并制作圆形头像。
裁剪并设置图像形状 是 CoddyKit 上的免费 Jetpack Compose Academy 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Jetpack Compose Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Jetpack Compose Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Beyond the Rectangle
Images draw as plain rectangles by default. To get rounded corners or circular avatars, you clip them to a shape.
Modifier.clip Cuts the Shape
The Modifier.clip function masks a composable to a given shape. Anything outside that shape simply is not drawn.
Image(painter = p, contentDescription = null,
modifier = Modifier.clip(CircleShape))CircleShape for Avatars
For a perfect circular avatar, clip with CircleShape. Pair it with a square size and Crop for a clean, round profile picture.
Modifier.size(64.dp).clip(CircleShape)Rounded Corners
Soft, modern cards use RoundedCornerShape. Give it a corner radius in dp to round all four corners evenly.
Modifier.clip(RoundedCornerShape(16.dp))Round Specific Corners
You can round corners individually, for example only the top, by passing separate values for each corner of RoundedCornerShape.
RoundedCornerShape(topStart = 16.dp, topEnd = 16.dp)Order Matters: Clip First
Modifiers apply in order. Put clip before background or border so the shape masks them too, not just the image content.
Add a Border on Top
A clipped image looks great with a matching outline. Chain a border with the same shape right after the clip.
Modifier.clip(CircleShape).border(2.dp, Color.White, CircleShape)Clip Plus Crop Together
The avatar recipe is two modifiers plus a scale: size, then clip to CircleShape, with ContentScale.Crop so the photo fills the circle.
Image(painter = p, contentDescription = null,
contentScale = ContentScale.Crop,
modifier = Modifier.size(64.dp).clip(CircleShape))Custom Shapes
Need a hexagon or a star? You can pass any Shape, including ones you define yourself with a path, to clip into bespoke outlines.
clipToBounds for Children
To stop child content from drawing outside a container, use Modifier.clipToBounds, which clips to the element rectangle.
Shadows and Clipping
A shadow needs the shape too. Apply Modifier.shadow with the same shape before the clip so the elevation follows the rounded edge.
Modifier.shadow(4.dp, CircleShape).clip(CircleShape)Quick Check
What shape do you clip an image to in order to make a perfectly circular avatar?
Recap: Shaping Images
You now clip images to CircleShape or RoundedCornerShape, order modifiers correctly, and add borders and shadows. Course complete! 🎉
用 AI 导师学习 Kotlin — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 30
- 课程
- 120
常见问题解答
「裁剪并设置图像形状」课时是免费的吗?
是的 — 「裁剪并设置图像形状」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Jetpack Compose Academy 课程的其余内容,请升级到 CoddyKit PRO。 Jetpack Compose Academy 课程共包含 4 节课。
「裁剪并设置图像形状」这节课中我会学到什么?
设置圆角并制作圆形头像。 你通过在浏览器中直接运行的动手代码来练习 Jetpack Compose Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Jetpack Compose Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Jetpack Compose Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「裁剪并设置图像形状」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Jetpack Compose Academy 课中编写并运行代码吗?
能。每节 Jetpack Compose Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。