Material 3 Expressive เป็นวิวัฒนาการขั้นถัดไปของ Material Design ซึ่งรวมถึงการอัปเดตธีม คอมโพเนนต์ และฟีเจอร์การปรับเปลี่ยนในแบบของคุณ เช่น สีแบบไดนามิก
คู่มือนี้จะเน้นที่การย้ายข้อมูลจากไลบรารี Jetpack Wear Compose Material 2.5 (androidx.wear.compose) ไปยังไลบรารี Jetpack Wear Compose Material 3 (androidx.wear.compose.material3) สำหรับแอป
แนวทาง
หากต้องการย้ายข้อมูลโค้ดแอปจาก M2.5 ไปยัง M3 ให้ทำตามแนวทางเดียวกับที่อธิบายไว้ในคำแนะนำในการย้ายข้อมูล Compose Material สำหรับโทรศัพท์ โดยเฉพาะอย่างยิ่งในส่วนต่อไปนี้
- คุณไม่ควรใช้ทั้ง M2.5 และ M3 ในแอปเดียวในระยะยาว
- คุณไม่ควรใช้ไลบรารี Horologist Composables, Compose Layout หรือ Compose Material อีกต่อไป แต่ให้ใช้คอมโพเนนต์ใน M3 แทน
- ใช้แนวทางแบบแบ่งเป็นระยะ
ความสัมพันธ์
M3 มีแพ็กเกจและเวอร์ชันแยกต่างหากจาก M2.5 ดังนี้
M2.5
implementation("androidx.wear.compose:compose-material:1.4.0")
M3
implementation("androidx.wear.compose:compose-material3:1.7.1")
ดูเวอร์ชัน M3 ล่าสุดได้ในหน้ารุ่นของ Wear Compose
ไลบรารี Wear Compose Foundation เวอร์ชัน 1.7.1 ได้เปิดตัวคอมโพเนนต์ใหม่บางรายการที่ออกแบบมาเพื่อทำงานร่วมกับคอมโพเนนต์ Material 3
ในทำนองเดียวกัน SwipeDismissableNavHost จากไลบรารี Wear Compose Navigation จะมีภาพเคลื่อนไหวที่อัปเดตเมื่อทำงานบน Wear OS 6 (ระดับ API 36) ขึ้นไป เมื่ออัปเดตเป็น Wear Compose Material 3 เวอร์ชัน เราขอแนะนำให้อัปเดตไลบรารี Wear Compose Foundation และ Navigation ด้วย
implementation("androidx.wear.compose:compose-foundation:1.7.1")
implementation("androidx.wear.compose:compose-navigation:1.7.1")
ธีม
ทั้งใน M2.5 และ M3 ธีม Composable จะมีชื่อว่า MaterialTheme แต่แพ็กเกจและพารามิเตอร์การนำเข้าจะแตกต่างกัน ใน M3 พารามิเตอร์ Colors ได้เปลี่ยนชื่อเป็น ColorScheme และมีการเปิดตัว MotionScheme สำหรับการใช้การเปลี่ยนผ่าน
M2.5
import androidx.wear.compose.material.MaterialTheme
MaterialTheme(
colors = AppColors,
typography = AppTypography,
shapes = AppShapes,
content = content
)
M3
import androidx.wear.compose.material3.MaterialTheme // ... MaterialTheme( colorScheme = ColorScheme(), typography = Typography(), shapes = Shapes(), motionScheme = MotionScheme.standard(), content = { /*content here*/ } )
สี
ระบบสีใน M3 แตกต่างจาก M2.5 อย่างมาก จำนวนพารามิเตอร์สีเพิ่มขึ้น มีชื่อแตกต่างกัน และแมปกับคอมโพเนนต์ M3 แตกต่างกัน ใน Compose การดำเนินการนี้จะมีผลกับคลาส M2.5 Colors, คลาส M3 ColorScheme และฟังก์ชันที่เกี่ยวข้อง ดังนี้
M2.5
import androidx.wear.compose.material.Colors
val appColorScheme: Colors = Colors(
// M2.5 Color parameters
)
M3
import androidx.wear.compose.material3.ColorScheme // ... val appColorScheme: ColorScheme = ColorScheme( // M3 ColorScheme parameters )
ตารางต่อไปนี้อธิบายความแตกต่างที่สำคัญระหว่าง M2.5 และ M3
| M2.5 | M3 |
|---|---|
Colors |
เปลี่ยนชื่อเป็น ColorScheme แล้ว |
| 13 สี | 29 สี |
| ไม่มี | ธีมสีแบบไดนามิกใหม่ |
| ไม่มี | สีใหม่ในลำดับที่ 3 เพื่อการแสดงออกที่มากขึ้น |
ธีมสีแบบไดนามิก
ฟีเจอร์ใหม่ใน M3 คือธีมสีแบบไดนามิก หากผู้ใช้เปลี่ยนสีหน้าปัด สีใน UI จะเปลี่ยนให้เข้ากัน
ใช้ฟังก์ชัน dynamicColorScheme เพื่อใช้รูปแบบสีแบบไดนามิกและระบุ defaultColorScheme เป็นตัวเลือกสำรองในกรณีที่รูปแบบสีแบบไดนามิกไม่พร้อมใช้งาน
@Composable fun myApp() { val dynamicColorScheme = dynamicColorScheme(LocalContext.current) MaterialTheme(colorScheme = dynamicColorScheme ?: myBrandColors) {} } internal val myBrandColors: ColorScheme = ColorScheme( /* Specify colors here */)
การพิมพ์
ระบบการพิมพ์ใน M3 แตกต่างจาก M2.5 และมีฟีเจอร์ต่อไปนี้
- รูปแบบข้อความใหม่ 9 แบบ
- แบบอักษร Flex ซึ่งช่วยให้ปรับแต่งสเกลประเภทสำหรับน้ำหนัก ความกว้าง และความกลมที่แตกต่างกันได้
AnimatedTextซึ่งใช้แบบอักษรที่ยืดหยุ่น
M2.5
import androidx.wear.compose.material.Typography
val Typography = Typography(
// M2.5 TextStyle parameters
)
M3
import androidx.wear.compose.material3.Typography val Typography = Typography( // M3 TextStyle parameters )
Flex Fonts
Flex Fonts ช่วยให้นักออกแบบระบุความกว้างและน้ำหนักของแบบอักษรสำหรับขนาดที่เฉพาะเจาะจงได้
รูปแบบข้อความ
TextStyles ต่อไปนี้พร้อมใช้งานใน M3 โดยคอมโพเนนต์ M3 ต่างๆ จะใช้สีเหล่านี้โดยค่าเริ่มต้น
| การพิมพ์ | TextStyle |
|---|---|
| Display | displayLarge, displayMedium, displaySmall |
| ชื่อ | titleLarge, titleMedium, titleSmall |
| ป้ายกำกับ | labelLarge, labelMedium, labelSmall |
| ลำตัว | bodyLarge, bodyMedium, bodySmall, bodyExtraSmall |
| ตัวเลข | numeralExtraLarge, numeralLarge, numeralMedium, numeralSmall, numeralExtraSmall |
| Arc | arcLarge, arcMedium, arcSmall |
รูปร่าง
ระบบรูปร่างใน M3 แตกต่างจาก M2.5 จำนวนพารามิเตอร์รูปร่างเพิ่มขึ้น มีชื่อแตกต่างกัน และแมปกับคอมโพเนนต์ M3 แตกต่างกัน รูปร่างมีขนาดดังต่อไปนี้
- เล็กพิเศษ
- เล็ก
- ปานกลาง
- ใหญ่
- ใหญ่พิเศษ
ใน Compose การดำเนินการนี้จะมีผลกับคลาส M2.5 Shapes และคลาส M3 Shapes ดังนี้
M2.5
import androidx.wear.compose.material.Shapes
val Shapes = Shapes(
// M2.5 Shapes parameters
)
M3
import androidx.wear.compose.material3.Shapes val Shapes = Shapes( // M3 Shapes parameters )
ใช้การแมปพารามิเตอร์รูปร่างจากย้ายข้อมูลจาก Material 2 ไปยัง Material 3 ใน Compose เป็นจุดเริ่มต้น
การเปลี่ยนรูปร่าง
M3 เปิดตัวการเปลี่ยนรูปร่าง โดยตอนนี้รูปร่างจะเปลี่ยนไปตามการโต้ตอบ
ลักษณะการทำงานของการเปลี่ยนรูปร่างพร้อมใช้งานเป็นรูปแบบต่างๆ ในปุ่มกลมหลายปุ่ม โปรดดูรายการปุ่มที่รองรับการเปลี่ยนรูปร่างต่อไปนี้
| ปุ่ม | ฟังก์ชันการเปลี่ยนรูปร่าง |
|---|---|
IconButton |
IconButtonDefaults.animatedShapes จะทำให้ปุ่มไอคอนเคลื่อนไหวเมื่อกด |
IconToggleButton |
IconToggleButtonDefaults.animatedShapes จะแสดงภาพเคลื่อนไหวของปุ่มเปิด/ปิดไอคอนเมื่อกด และ IconToggleButtonDefaults.variantAnimatedShapes จะแสดงภาพเคลื่อนไหวของปุ่มเปิด/ปิดไอคอนเมื่อกดและเลือก/ยกเลิกการเลือก |
TextButton |
TextButtonDefaults.animatedShapes จะทำให้ปุ่มข้อความเคลื่อนไหวเมื่อกด |
TextToggleButton |
TextToggleButtonDefaults.animatedShapes จะทำให้ปุ่มเปิด/ปิดข้อความเคลื่อนไหวเมื่อกด และ TextToggleButtonDefaults.variantAnimatedShapes จะทำให้ปุ่มเปิด/ปิดข้อความเคลื่อนไหวเมื่อกดและเลือก/ยกเลิกการเลือก |
คอมโพเนนต์และเลย์เอาต์
คอมโพเนนต์และเลย์เอาต์ส่วนใหญ่จาก M2.5 มีให้บริการใน M3 อย่างไรก็ตาม คอมโพเนนต์และเลย์เอาต์ M3 บางรายการไม่มีใน M2.5 นอกจากนี้ คอมโพเนนต์ M3 บางรายการยังมีรูปแบบมากกว่าคอมโพเนนต์ที่เทียบเท่าใน M2.5
แม้ว่าคอมโพเนนต์บางอย่างจะต้องพิจารณาเป็นพิเศษ แต่เราขอแนะนำให้ใช้การแมปฟังก์ชันต่อไปนี้เป็นจุดเริ่มต้น
รายการคอมโพเนนต์ Material 3 ที่พบบ่อยและคอมโพเนนต์ Material 2.5 ที่เทียบเท่ามีดังนี้
และสุดท้ายคือรายการคอมโพเนนต์ที่เกี่ยวข้องบางส่วนจากไลบรารี Wear Compose Foundation
| Wear Compose Foundation 1.7.1 | |
|---|---|
| androidx.wear.compose.foundation.hierarchicalFocusGroup | ใช้เพื่อใส่คำอธิบายประกอบ Composable ในแอปพลิเคชัน เพื่อติดตามส่วนที่ใช้งานอยู่ของ Composition และประสานงานการโฟกัส |
| androidx.wear.compose.foundation.pager.HorizontalPager | เพจเจอร์ที่เลื่อนในแนวนอน ซึ่งสร้างขึ้นจากคอมโพเนนต์ Compose Foundation พร้อมการปรับปรุงเฉพาะสำหรับ Wear เพื่อเพิ่มประสิทธิภาพและสอดคล้องกับหลักเกณฑ์ของ Wear OS |
| androidx.wear.compose.foundation.pager.VerticalPager | เพจเจอร์ที่เลื่อนในแนวตั้ง ซึ่งสร้างขึ้นจากคอมโพเนนต์ Compose Foundation พร้อมการปรับปรุงเฉพาะสำหรับ Wear เพื่อเพิ่มประสิทธิภาพและเป็นไปตามหลักเกณฑ์ของ Wear OS |
| androidx.wear.compose.foundation.lazy.TransformingLazyColumn | ใช้แทน ScalingLazyColumn เพื่อเพิ่มเอฟเฟกต์การเปลี่ยนรูปแบบการเลื่อนให้กับแต่ละรายการได้ |
ปุ่ม
ปุ่มใน M3 แตกต่างจาก M2.5 ชิป M2.5 ถูกแทนที่ด้วยปุ่ม การติดตั้งใช้งาน Button จะให้ค่าเริ่มต้นสำหรับ Text
maxLines และ textAlign คุณลบล้างค่าเริ่มต้นเหล่านั้นได้ในองค์ประกอบ Text
M2.5
import androidx.wear.compose.material.Chip
//M2.5 Buttons
Chip(...)
CompactChip(...)
Button(...)
M3
//M3 Buttons Button(onClick = { }){} CompactButton(onClick = { }){} IconButton(onClick = { }){} TextButton(onClick = { }){}
นอกจากนี้ M3 ยังมีรูปแบบปุ่มใหม่ๆ ด้วย ดูข้อมูลเพิ่มเติมได้ที่ภาพรวมเอกสารอ้างอิง API ของ Compose Material 3
M3 มีปุ่มใหม่คือ EdgeButton EdgeButton มีให้เลือก 4 ขนาด ได้แก่ เล็กพิเศษ เล็ก กลาง และใหญ่ การติดตั้งใช้งาน EdgeButton จะให้ค่าเริ่มต้นสำหรับ maxLines โดยขึ้นอยู่กับขนาดที่ปรับแต่งได้
หากคุณใช้ TransformingLazyColumn หรือ ScalingLazyColumn ให้ส่ง EdgeButton ไปยัง ScreenScaffold เพื่อให้เปลี่ยนรูปแบบ โดยเปลี่ยนรูปร่างด้วยการเลื่อน แทนที่จะเพิ่ม EdgeButton เป็นรายการสุดท้ายในลิสต์ ดูโค้ดต่อไปนี้เพื่อดูวิธีใช้ EdgeButton กับ ScreenScaffold และ TransformingLazyColumn
val state = rememberTransformingLazyColumnState() ScreenScaffold( scrollState = state, edgeButton = { EdgeButton( onClick = { }, modifier = Modifier.scrollable( state, orientation = Orientation.Vertical, reverseDirection = true, // Apply overscroll to the EdgeButton for proper scrolling behavior. overscrollEffect = rememberOverscrollEffect(), ), ) { Text(stringResource(R.string.show)) } }, ) { contentPadding -> TransformingLazyColumn(state = state, contentPadding = contentPadding) { // additional code here // ... } }
Scaffold
Scaffold ใน M3 แตกต่างจาก M2.5 ใน M3 AppScaffold และคอมโพสิเบิล ScreenScaffold ใหม่ได้เข้ามาแทนที่ Scaffold AppScaffold และ ScreenScaffold จะจัดวางโครงสร้างของหน้าจอและประสานงานการเปลี่ยนผ่านของคอมโพเนนต์ ScrollIndicator และ TimeText
AppScaffold ช่วยให้องค์ประกอบหน้าจอแบบคงที่ เช่น TimeText ยังคงมองเห็นได้ในระหว่างการเปลี่ยนหน้าจอในแอป เช่น การปัดเพื่อปิด โดยจะมีช่องสำหรับเนื้อหาหลักของแอปพลิเคชัน ซึ่งมักจะมาจากคอมโพเนนต์การนำทาง เช่น SwipeDismissableNavHost
คุณประกาศ AppScaffold 1 รายการสำหรับกิจกรรม และใช้ ScreenScaffold สำหรับแต่ละหน้าจอ
AppScaffold เพิ่มคอมโพเนนต์ TimeText เริ่มต้นลงในหน้าจอ คุณสามารถลบล้างได้หากต้องการปรับแต่งโดยใช้พารามิเตอร์ timeText
M2.5
import androidx.wear.compose.material.Scaffold
Scaffold {...}
M3
AppScaffold { val navController = rememberSwipeDismissableNavController() SwipeDismissableNavHost( navController = navController, startDestination = "message_list" ) { composable("message_list") { MessageList(onMessageClick = { id -> navController.navigate("message_detail/$id") }) } composable("message_detail/{id}") { MessageDetail(id = it.arguments?.getString("id")!!) } } } } // Implementation of one of the screens in the navigation @Composable fun MessageDetail(id: String) { // .. Screen level content goes here val scrollState = rememberTransformingLazyColumnState() val transformationSpec = rememberTransformationSpec() ScreenScaffold( scrollState = scrollState, ) { contentPadding -> // Screen content goes here // ...
หากคุณใช้ HorizontalPager กับ HorizontalPageIndicator คุณจะย้ายข้อมูลไปยัง HorizontalPagerScaffold ได้ HorizontalPagerScaffold จะอยู่ภายใน AppScaffold AppScaffold และ HorizontalPagerScaffold จะจัดวางโครงสร้างของ Pager และประสานงานการเปลี่ยนผ่านของคอมโพเนนต์ HorizontalPageIndicator และ TimeText
HorizontalPagerScaffold จะแสดง HorizontalPageIndicator ที่กึ่งกลางด้านท้ายของหน้าจอโดยค่าเริ่มต้น และประสานงานการแสดงและซ่อน TimeText และ HorizontalPageIndicator ตามว่า Pager มีการแบ่งหน้าหรือไม่ ซึ่งกำหนดโดย PagerState
นอกจากนี้ยังมีคอมโพเนนต์ AnimatedPage ใหม่ ซึ่งจะทำให้หน้าภายใน Pager เคลื่อนไหวด้วยเอฟเฟกต์การปรับขนาดและสคริมตามตำแหน่ง
AppScaffold { val pagerState = rememberPagerState(pageCount = { 10 }) HorizontalPagerScaffold(pagerState = pagerState) { HorizontalPager( state = pagerState, flingBehavior = PagerScaffoldDefaults.snapWithSpringFlingBehavior( state = pagerState ), ) { page -> AnimatedPage(pageIndex = page, pagerState = pagerState) { val columnState = rememberTransformingLazyColumnState() val transformationSpec = rememberTransformationSpec() ScreenScaffold( scrollState = columnState, ) { contentPadding -> TransformingLazyColumn( state = columnState, contentPadding = contentPadding, ) { item { ListHeader( modifier = Modifier .fillMaxWidth() .transformedHeight(this, transformationSpec) .minimumVerticalContentPadding( ListHeaderDefaults.minimumTopListContentPadding ), transformation = SurfaceTransformation(transformationSpec), ) { Text(text = "Pager sample") } } item { Card( modifier = Modifier .fillMaxWidth() .transformedHeight(this, transformationSpec) .minimumVerticalContentPadding( CardDefaults.minimumVerticalListContentPadding ), transformation = SurfaceTransformation(transformationSpec), ) { if (page == 0) { Text(text = "Page #$page. Swipe right") } else { Text(text = "Page #$page. Swipe left and right") } } } } } } } } }
สุดท้าย M3 จะเปิดตัว VerticalPagerScaffold ซึ่งใช้รูปแบบเดียวกับ HorizontalPagerScaffold ดังนี้
@Composable fun VerticalPagerScaffoldSample() { AppScaffold { val pagerState = rememberPagerState(pageCount = { 10 }) VerticalPagerScaffold(pagerState = pagerState) { VerticalPager( state = pagerState, flingBehavior = PagerScaffoldDefaults.snapWithSpringFlingBehavior( state = pagerState ), ) { page -> AnimatedPage(pageIndex = page, pagerState = pagerState) { ScreenScaffold { Column( modifier = Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center, ) { Text(text = "Page #$page") Spacer(modifier = Modifier.height(8.dp)) Text(text = "Swipe up and down") } } } } } } }
ตัวยึดตำแหน่ง
มีการเปลี่ยนแปลง API บางอย่างระหว่าง M2.5 และ M3 M3 มีตัวแก้ไขตัวยึดตำแหน่ง 2 รายการ ได้แก่
Modifier.placeholderซึ่งจะแสดงแทนเนื้อหาที่ยังไม่ได้โหลด- เอฟเฟกต์ระยิบระยับของตัวยึดตำแหน่ง
Modifier.placeholderShimmerซึ่งจะแสดงเอฟเฟกต์ระยิบระยับของตัวยึดตำแหน่งที่ทำงานในลูปภาพเคลื่อนไหวขณะรอโหลดข้อมูล
ดูการเปลี่ยนแปลงเพิ่มเติมในคอมโพเนนต์ Placeholder ได้ในตารางต่อไปนี้
| M2.5 | M3 |
|---|---|
PlaceholderState.startPlaceholderAnimation |
นำออกแล้ว |
PlaceholderState.placeholderProgression |
นำออกแล้ว |
PlaceholderState.isShowContent |
แทนที่ด้วย PlaceholderState.isVisible (บูลีนแบบกลับค่า) |
PlaceholderState.isWipeOff |
นำออกแล้ว |
PlaceholderDefaults.painterWithPlaceholderOverlayBackgroundBrush |
นำออกแล้ว |
PlaceholderDefaults.placeholderBackgroundBrush |
นำออกแล้ว |
PlaceholderDefaults.placeholderChipColors |
นำออกแล้ว |
SwipeDismissableNavHost
SwipeDismissableNavHost เป็นส่วนหนึ่งของ wear.compose.navigation เมื่อใช้คอมโพเนนต์นี้กับ M3 แล้ว MaterialTheme ของ M3 จะอัปเดต LocalSwipeToDismissBackgroundScrimColor และ LocalSwipeToDismissContentScrimColor
TransformingLazyColumn
TransformingLazyColumn เป็นส่วนหนึ่งของ androidx.wear.compose.foundation.lazy และเพิ่มการรองรับภาพเคลื่อนไหวแบบปรับขนาดและเปลี่ยนรูปร่างในรายการต่างๆ ระหว่างการเลื่อน ซึ่งจะช่วยปรับปรุงประสบการณ์ของผู้ใช้ เราขอแนะนำเป็นอย่างยิ่งให้แอปย้ายข้อมูลจาก ScalingLazyColumn ไปยัง TransformingLazyColumn
เช่นเดียวกับ ScalingLazyColumn ฟังก์ชันนี้จะให้ rememberTransformingLazyColumnState() เพื่อสร้าง TransformingLazyColumnState ที่ระบบจะจดจำไว้ใน Composition ต่างๆ
หากต้องการเพิ่มภาพเคลื่อนไหวแบบปรับขนาดและเปลี่ยนรูปร่าง ให้เพิ่มสิ่งต่อไปนี้ลงในรายการแต่ละรายการ
Modifier.transformedHeightซึ่งช่วยให้คุณคำนวณความสูงที่เปลี่ยนไปของรายการโดยใช้TransformationSpecคุณสามารถใช้rememberTransformationSpec()ได้ เว้นแต่คุณต้องการปรับแต่งเพิ่มเติมSurfaceTransformation
หากต้องการยืนยันว่าระยะห่างจากขอบที่ด้านบนและด้านล่างของรายการถูกต้อง ให้ใช้ตัวแก้ไข minimumVerticalContentPadding
val columnState = rememberTransformingLazyColumnState() val transformationSpec = rememberTransformationSpec() ScreenScaffold( scrollState = columnState ) { contentPadding -> TransformingLazyColumn( state = columnState, contentPadding = contentPadding ) { item { ListHeader( modifier = Modifier .fillMaxWidth() .transformedHeight(this, transformationSpec) .minimumVerticalContentPadding(ListHeaderDefaults.minimumTopListContentPadding), transformation = SurfaceTransformation(transformationSpec) ) { Text(text = "Header") } } // ... other items item { Button( modifier = Modifier .fillMaxWidth() .transformedHeight(this, transformationSpec) .minimumVerticalContentPadding(ButtonDefaults.minimumVerticalListContentPadding), transformation = SurfaceTransformation(transformationSpec), onClick = { /* ... */ }, icon = { Icon( imageVector = Icons.Default.Build, contentDescription = "build", ) }, ) { Text( text = "Build", maxLines = 1, overflow = TextOverflow.Ellipsis, ) } } } }
ลิงก์ที่มีประโยชน์
ดูข้อมูลเพิ่มเติมเกี่ยวกับการย้ายข้อมูลจาก M2.5 ไปยัง M3 ใน Compose ได้ที่แหล่งข้อมูลเพิ่มเติมต่อไปนี้