0Pricing
Jetpack Compose Academy · บทเรียน

ตัดและกำหนดรูปทรงให้ภาพ

ทำมุมมนและสร้างรูปประจำตัวแบบวงกลม

ตัดและกำหนดรูปทรงให้ภาพ เป็นบทเรียน Jetpack Compose Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Jetpack Compose Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Jetpack Compose Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ตัดและกำหนดรูปทรงให้ภาพ”

ทำมุมมนและสร้างรูปประจำตัวแบบวงกลม คุณปฏิบัติ Jetpack Compose Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Jetpack Compose Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Jetpack Compose Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “ตัดและกำหนดรูปทรงให้ภาพ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Jetpack Compose Academy นี้ได้ไหม

ได้ บทเรียน Jetpack Compose Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. Composable Image
  2. ไอคอน Material ด้วย Icon
  3. ContentScale และการครอบตัด
  4. ตัดและกำหนดรูปทรงให้ภาพ
← กลับไปที่ Jetpack Compose Academy