Compose มีกลไกภาพเคลื่อนไหวในตัวมากมาย และอาจทำให้คุณสับสนไม่รู้ว่าจะเลือกใช้กลไกใด ต่อไปนี้คือรายการกรณีการใช้งานภาพเคลื่อนไหวที่พบบ่อย หากต้องการดูข้อมูลโดยละเอียดเพิ่มเติมเกี่ยวกับชุดตัวเลือก API ต่างๆ ที่มีให้ โปรดอ่านเอกสารประกอบเกี่ยวกับภาพเคลื่อนไหวใน Compose ฉบับเต็ม
สร้างภาพเคลื่อนไหวให้กับพร็อพเพอร์ตี้ Composable ที่พบบ่อย
Compose มี API ที่สะดวกสบายซึ่งช่วยให้คุณแก้ปัญหาในกรณีการใช้งานภาพเคลื่อนไหวที่พบบ่อยได้มากมาย ส่วนนี้จะแสดงวิธีสร้างภาพเคลื่อนไหวให้กับพร็อพเพอร์ตี้ที่พบบ่อยของ Composable
สร้างภาพเคลื่อนไหวให้ปรากฏ / หายไป
ใช้ AnimatedVisibility เพื่อซ่อนหรือแสดง Composable ส่วนประกอบย่อยภายใน AnimatedVisibility สามารถใช้ Modifier.animateEnterExit() สำหรับการเปลี่ยนภาพเข้าหรือออกของตัวเองได้
var visible by remember { mutableStateOf(true) } // Animated visibility will eventually remove the item from the composition once the animation has finished. AnimatedVisibility(visible) { // your composable here // ... }
พารามิเตอร์เข้าและออกของ AnimatedVisibility ช่วยให้คุณกำหนดค่าลักษณะการทำงานของ Composable เมื่อปรากฏและหายไปได้ อ่านเอกสารประกอบฉบับเต็มเพื่อดูข้อมูลเพิ่มเติม
อีกตัวเลือกหนึ่งสำหรับการสร้างภาพเคลื่อนไหวให้ระดับการมองเห็นของ Composable คือการสร้างภาพเคลื่อนไหวให้ค่า
อัลฟาลดลงเมื่อเวลาผ่านไปโดยใช้ animateFloatAsState
var visible by remember { mutableStateOf(true) } val animatedAlpha by animateFloatAsState( targetValue = if (visible) 1.0f else 0f, label = "alpha" ) Box( modifier = Modifier .size(200.dp) .graphicsLayer { alpha = animatedAlpha } .clip(RoundedCornerShape(8.dp)) .background(colorGreen) .align(Alignment.TopCenter) ) { }
อย่างไรก็ตาม การเปลี่ยนค่าอัลฟามีข้อควรระวังว่า Composable จะยังคงอยู่ในองค์ประกอบ และยังคงใช้พื้นที่ที่จัดวางไว้ ซึ่งอาจทำให้โปรแกรมอ่านหน้าจอและกลไกการช่วยเหลือพิเศษอื่นๆ ยังคงพิจารณารายการบนหน้าจอ ในทางกลับกัน AnimatedVisibility จะนำรายการออกจากองค์ประกอบในที่สุด
สร้างภาพเคลื่อนไหวให้สีพื้นหลัง
val animatedColor by animateColorAsState( if (animateBackgroundColor) colorGreen else colorBlue, label = "color" ) Column( modifier = Modifier.drawBehind { drawRect(animatedColor) } ) { // your composable here }
ตัวเลือกนี้มีประสิทธิภาพมากกว่าการใช้ Modifier.background()
Modifier.background() เหมาะสำหรับการตั้งค่าสีแบบครั้งเดียว แต่เมื่อสร้างภาพเคลื่อนไหวให้สีเปลี่ยนไปตามเวลา ตัวเลือกนี้อาจทำให้เกิดการประกอบใหม่มากกว่าที่จำเป็น
หากต้องการสร้างภาพเคลื่อนไหวให้สีพื้นหลังเปลี่ยนไปเรื่อยๆ โปรดดูส่วนการทำภาพเคลื่อนไหวซ้ำ
สร้างภาพเคลื่อนไหวให้ขนาดของ Composable
Compose ช่วยให้คุณสร้างภาพเคลื่อนไหวให้ขนาดของ Composable ได้หลายวิธี ใช้
animateContentSize() สำหรับภาพเคลื่อนไหวระหว่างการเปลี่ยนแปลงขนาดของ Composable
ตัวอย่างเช่น หากคุณมีกล่องที่มีข้อความซึ่งขยายจาก 1 บรรทัดเป็นหลายบรรทัดได้ คุณสามารถใช้ Modifier.animateContentSize() เพื่อให้การเปลี่ยนภาพราบรื่นขึ้นได้ ดังนี้
var expanded by remember { mutableStateOf(false) } Box( modifier = Modifier .background(colorBlue) .animateContentSize() .height(if (expanded) 400.dp else 200.dp) .fillMaxWidth() .clickable( interactionSource = remember { MutableInteractionSource() }, indication = null ) { expanded = !expanded } ) { }
นอกจากนี้ คุณยังใช้ AnimatedContent กับ SizeTransform เพื่ออธิบาย
วิธีที่การเปลี่ยนแปลงขนาดควรเกิดขึ้นได้ด้วย
สร้างภาพเคลื่อนไหวให้ตำแหน่งของ Composable
หากต้องการสร้างภาพเคลื่อนไหวให้ตำแหน่งของ Composable ให้ใช้ Modifier.offset{ } ร่วมกับ animateIntOffsetAsState()
var moved by remember { mutableStateOf(false) } val pxToMove = with(LocalDensity.current) { 100.dp.toPx().roundToInt() } val offset by animateIntOffsetAsState( targetValue = if (moved) { IntOffset(pxToMove, pxToMove) } else { IntOffset.Zero }, label = "offset" ) Box( modifier = Modifier .offset { offset } .background(colorBlue) .size(100.dp) .clickable( interactionSource = remember { MutableInteractionSource() }, indication = null ) { moved = !moved } )
หากต้องการให้ Composable ไม่วาดทับ Composable อื่นเมื่อสร้างภาพเคลื่อนไหวให้ตำแหน่งหรือขนาด ให้ใช้ Modifier.layout{ } ตัวปรับแต่งนี้จะเผยแพร่การเปลี่ยนแปลงขนาดและตำแหน่งไปยังองค์ประกอบระดับบนสุด ซึ่งจะส่งผลต่อองค์ประกอบย่อยอื่นๆ
ตัวอย่างเช่น หากคุณกำลังย้าย Box ภายใน Column และองค์ประกอบย่อยอื่นๆ ต้องย้ายตามเมื่อ Box เคลื่อนที่ ให้ใส่ข้อมูลออฟเซ็ตด้วย Modifier.layout{ } ดังนี้
var toggled by remember { mutableStateOf(false) } val interactionSource = remember { MutableInteractionSource() } Column( modifier = Modifier .padding(16.dp) .fillMaxSize() .clickable(indication = null, interactionSource = interactionSource) { toggled = !toggled } ) { val offsetTarget = if (toggled) { IntOffset(150, 150) } else { IntOffset.Zero } val offset = animateIntOffsetAsState( targetValue = offsetTarget, label = "offset" ) Box( modifier = Modifier .size(100.dp) .background(colorBlue) ) Box( modifier = Modifier .layout { measurable, constraints -> val offsetValue = if (isLookingAhead) offsetTarget else offset.value val placeable = measurable.measure(constraints) layout(placeable.width + offsetValue.x, placeable.height + offsetValue.y) { placeable.placeRelative(offsetValue) } } .size(100.dp) .background(colorGreen) ) Box( modifier = Modifier .size(100.dp) .background(colorBlue) ) }
Modifier.layout{ }สร้างภาพเคลื่อนไหวให้ระยะห่างภายในของ Composable
หากต้องการสร้างภาพเคลื่อนไหวให้ระยะห่างภายในของ Composable ให้ใช้ animateDpAsState ร่วมกับ Modifier.padding() ดังนี้
var toggled by remember { mutableStateOf(false) } val animatedPadding by animateDpAsState( if (toggled) { 0.dp } else { 20.dp }, label = "padding" ) Box( modifier = Modifier .aspectRatio(1f) .fillMaxSize() .padding(animatedPadding) .background(Color(0xff53D9A1)) .clickable( interactionSource = remember { MutableInteractionSource() }, indication = null ) { toggled = !toggled } )
สร้างภาพเคลื่อนไหวให้ระดับความสูงของ Composable
หากต้องการสร้างภาพเคลื่อนไหวให้ระดับความสูงของ Composable ให้ใช้ animateDpAsState ร่วมกับ Modifier.graphicsLayer{ } หากต้องการเปลี่ยนระดับความสูงแบบครั้งเดียว ให้ใช้ Modifier.shadow() หากคุณกำลังสร้างภาพเคลื่อนไหวให้เงา ตัวปรับแต่ง Modifier.graphicsLayer{ } เป็นตัวเลือกที่มีประสิทธิภาพมากกว่า
val mutableInteractionSource = remember { MutableInteractionSource() } val pressed = mutableInteractionSource.collectIsPressedAsState() val elevation = animateDpAsState( targetValue = if (pressed.value) { 32.dp } else { 8.dp }, label = "elevation" ) Box( modifier = Modifier .size(100.dp) .align(Alignment.Center) .graphicsLayer { this.shadowElevation = elevation.value.toPx() } .clickable(interactionSource = mutableInteractionSource, indication = null) { } .background(colorGreen) ) { }
หรือจะใช้ Card Composable และตั้งค่าพร็อพเพอร์ตี้ระดับความสูงเป็น
ค่าต่างๆ ตามสถานะก็ได้
สร้างภาพเคลื่อนไหวให้การปรับขนาด การเปลี่ยนตำแหน่ง หรือการหมุนของข้อความ
เมื่อสร้างภาพเคลื่อนไหวให้การปรับขนาด การเปลี่ยนตำแหน่ง หรือการหมุนของข้อความ ให้ตั้งค่า textMotion
พารามิเตอร์ใน TextStyle เป็น TextMotion.Animated ซึ่งจะช่วยให้การเปลี่ยนภาพระหว่างภาพเคลื่อนไหวของข้อความราบรื่นขึ้น ใช้ Modifier.graphicsLayer{ } เพื่อ
เปลี่ยนตำแหน่ง หมุน หรือปรับขนาดข้อความ
val infiniteTransition = rememberInfiniteTransition(label = "infinite transition") val scale by infiniteTransition.animateFloat( initialValue = 1f, targetValue = 8f, animationSpec = infiniteRepeatable(tween(1000), RepeatMode.Reverse), label = "scale" ) Box(modifier = Modifier.fillMaxSize()) { Text( text = "Hello", modifier = Modifier .graphicsLayer { scaleX = scale scaleY = scale transformOrigin = TransformOrigin.Center } .align(Alignment.Center), // Text composable does not take TextMotion as a parameter. // Provide it via style argument but make sure that we are copying from current theme style = LocalTextStyle.current.copy(textMotion = TextMotion.Animated) ) }
สร้างภาพเคลื่อนไหวให้สีข้อความ
หากต้องการสร้างภาพเคลื่อนไหวให้สีข้อความ ให้ใช้แลมบ์ดา color ใน Composable BasicText ดังนี้
val infiniteTransition = rememberInfiniteTransition(label = "infinite transition") val animatedColor by infiniteTransition.animateColor( initialValue = Color(0xFF60DDAD), targetValue = Color(0xFF4285F4), animationSpec = infiniteRepeatable(tween(1000), RepeatMode.Reverse), label = "color" ) BasicText( text = "Hello Compose", color = { animatedColor }, // ... )
สลับระหว่างเนื้อหาประเภทต่างๆ
ใช้ AnimatedContent เพื่อสร้างภาพเคลื่อนไหวระหว่าง Composable ต่างๆ หากคุณ
ต้องการเพียงแค่การเฟดมาตรฐานระหว่าง Composable ให้ใช้ Crossfade
var state by remember { mutableStateOf(UiState.Loading) } AnimatedContent( state, transitionSpec = { fadeIn( animationSpec = tween(3000) ) togetherWith fadeOut(animationSpec = tween(3000)) }, modifier = Modifier.clickable( interactionSource = remember { MutableInteractionSource() }, indication = null ) { state = when (state) { UiState.Loading -> UiState.Loaded UiState.Loaded -> UiState.Error UiState.Error -> UiState.Loading } }, label = "Animated Content" ) { targetState -> when (targetState) { UiState.Loading -> { LoadingScreen() } UiState.Loaded -> { LoadedScreen() } UiState.Error -> { ErrorScreen() } } }
คุณปรับแต่ง AnimatedContent เพื่อแสดงการเปลี่ยนภาพเข้าและออกได้หลายแบบ ดูข้อมูลเพิ่มเติมได้ที่เอกสารประกอบเกี่ยวกับ
AnimatedContent หรืออ่านบล็อกโพสต์นี้เกี่ยวกับ
AnimatedContent
สร้างภาพเคลื่อนไหวขณะนำทางไปยังปลายทางต่างๆ
หากต้องการสร้างภาพเคลื่อนไหวให้การเปลี่ยนภาพระหว่าง Composable เมื่อใช้ Artifact
Navigation-Compose ให้ระบุ enterTransition และ
exitTransition ใน Composable นอกจากนี้ คุณยังตั้งค่าภาพเคลื่อนไหวเริ่มต้นที่จะใช้กับปลายทางทั้งหมดที่ระดับบนสุด NavHost ได้ด้วย ดังนี้
val navController = rememberNavController() NavHost( navController = navController, startDestination = "landing", enterTransition = { EnterTransition.None }, exitTransition = { ExitTransition.None } ) { composable("landing") { ScreenLanding( // ... ) } composable( "detail/{photoUrl}", arguments = listOf(navArgument("photoUrl") { type = NavType.StringType }), enterTransition = { fadeIn( animationSpec = tween( 300, easing = LinearEasing ) ) + slideIntoContainer( animationSpec = tween(300, easing = EaseIn), towards = AnimatedContentTransitionScope.SlideDirection.Start ) }, exitTransition = { fadeOut( animationSpec = tween( 300, easing = LinearEasing ) ) + slideOutOfContainer( animationSpec = tween(300, easing = EaseOut), towards = AnimatedContentTransitionScope.SlideDirection.End ) } ) { backStackEntry -> ScreenDetails( // ... ) } }
การเปลี่ยนภาพเข้าและออกมีหลายแบบซึ่งจะใช้ เอฟเฟกต์ต่างๆ กับเนื้อหาที่เข้ามาและออกไป โปรดดูข้อมูลเพิ่มเติมใน เอกสารประกอบ
ทำภาพเคลื่อนไหวซ้ำ
ใช้ rememberInfiniteTransition กับ infiniteRepeatable
animationSpec เพื่อทำภาพเคลื่อนไหวซ้ำไปเรื่อยๆ เปลี่ยน RepeatModes เพื่อระบุวิธีที่ภาพเคลื่อนไหวควรไปกลับ
ใช้ repeatable เพื่อทำซ้ำตามจำนวนครั้งที่กำหนด
val infiniteTransition = rememberInfiniteTransition(label = "infinite") val color by infiniteTransition.animateColor( initialValue = Color.Green, targetValue = Color.Blue, animationSpec = infiniteRepeatable( animation = tween(1000, easing = LinearEasing), repeatMode = RepeatMode.Reverse ), label = "color" ) Column( modifier = Modifier.drawBehind { drawRect(color) } ) { // your composable here }
เริ่มภาพเคลื่อนไหวเมื่อเปิด Composable
LaunchedEffect จะทำงานเมื่อ Composable เข้าสู่องค์ประกอบ โดยจะเริ่มภาพเคลื่อนไหวเมื่อเปิด Composable ซึ่งคุณสามารถใช้ตัวปรับแต่งนี้เพื่อขับเคลื่อนการเปลี่ยนแปลงสถานะภาพเคลื่อนไหวได้ การใช้ Animatable กับเมธอด animateTo เพื่อเริ่มภาพเคลื่อนไหวเมื่อเปิด ดังนี้
val alphaAnimation = remember { Animatable(0f) } LaunchedEffect(Unit) { alphaAnimation.animateTo(1f) } Box( modifier = Modifier.graphicsLayer { alpha = alphaAnimation.value } )
สร้างภาพเคลื่อนไหวแบบลำดับ
ใช้ API ของโครูทีน Animatable เพื่อสร้างภาพเคลื่อนไหวแบบลำดับหรือพร้อมกัน การเรียก animateTo ใน Animatable ทีละรายการจะทำให้ภาพเคลื่อนไหวแต่ละรายการรอให้ภาพเคลื่อนไหวก่อนหน้าเสร็จสิ้นก่อนที่จะดำเนินการต่อ
เนื่องจากเป็นฟังก์ชันระงับ
val alphaAnimation = remember { Animatable(0f) } val yAnimation = remember { Animatable(0f) } LaunchedEffect("animationKey") { alphaAnimation.animateTo(1f) yAnimation.animateTo(100f) yAnimation.animateTo(500f, animationSpec = tween(100)) }
สร้างภาพเคลื่อนไหวแบบพร้อมกัน
ใช้ API ของโครูทีน (Animatable#animateTo() หรือ animate) หรือ
API ของ Transition เพื่อสร้างภาพเคลื่อนไหวแบบพร้อมกัน หากคุณใช้ฟังก์ชันเปิดหลายรายการในบริบทของโครูทีน ฟังก์ชันนี้จะเปิดภาพเคลื่อนไหวพร้อมกัน ดังนี้
val alphaAnimation = remember { Animatable(0f) } val yAnimation = remember { Animatable(0f) } LaunchedEffect("animationKey") { launch { alphaAnimation.animateTo(1f) } launch { yAnimation.animateTo(100f) } }
คุณสามารถใช้ API ของ updateTransition เพื่อใช้สถานะเดียวกันในการขับเคลื่อน
ภาพเคลื่อนไหวของพร็อพเพอร์ตี้ต่างๆ มากมายพร้อมกันได้ ตัวอย่างต่อไปนี้จะสร้างภาพเคลื่อนไหวให้พร็อพเพอร์ตี้ 2 รายการที่ควบคุมโดยการเปลี่ยนแปลงสถานะ ได้แก่ rect และ borderWidth
var currentState by remember { mutableStateOf(BoxState.Collapsed) } val transition = updateTransition(currentState, label = "transition") val rect by transition.animateRect(label = "rect") { state -> when (state) { BoxState.Collapsed -> Rect(0f, 0f, 100f, 100f) BoxState.Expanded -> Rect(100f, 100f, 300f, 300f) } } val borderWidth by transition.animateDp(label = "borderWidth") { state -> when (state) { BoxState.Collapsed -> 1.dp BoxState.Expanded -> 0.dp } }
เพิ่มประสิทธิภาพภาพเคลื่อนไหว
ภาพเคลื่อนไหวใน Compose อาจทำให้เกิดปัญหาด้านประสิทธิภาพ เนื่องจากลักษณะของภาพเคลื่อนไหวคือการย้ายหรือเปลี่ยนพิกเซลบนหน้าจออย่างรวดเร็วทีละเฟรมเพื่อสร้างภาพลวงตาของการเคลื่อนไหว
พิจารณาขั้นตอนต่างๆ ของ Compose ได้แก่ การประกอบ การจัดวาง และการวาด หากภาพเคลื่อนไหวเปลี่ยนขั้นตอนการจัดวาง ภาพเคลื่อนไหวจะต้องให้ Composable ทั้งหมดที่ได้รับผลกระทบจัดวางใหม่และวาดใหม่ หากภาพเคลื่อนไหวเกิดขึ้นในขั้นตอนการวาด ภาพเคลื่อนไหวจะมีประสิทธิภาพมากกว่าการเรียกใช้ภาพเคลื่อนไหวในขั้นตอนการจัดวางโดยค่าเริ่มต้น เนื่องจากภาพเคลื่อนไหวจะมีงานที่ต้องทำโดยรวมน้อยกว่า
หากต้องการให้แอปทำงานน้อยที่สุดขณะสร้างภาพเคลื่อนไหว ให้เลือกใช้แลมบ์ดาเวอร์ชันของ Modifier หากเป็นไปได้ ซึ่งจะข้ามการจัดองค์ประกอบใหม่และดำเนินการ
ภาพเคลื่อนไหวนอกขั้นตอนการจัดองค์ประกอบ หรือใช้
Modifier.graphicsLayer{ } เนื่องจากตัวปรับแต่งนี้จะทำงานในขั้นตอนการวาด
เสมอ ดูข้อมูลเพิ่มเติมเกี่ยวกับเรื่องนี้ได้ที่ส่วนการเลื่อนการอ่านใน
เอกสารประกอบเกี่ยวกับประสิทธิภาพ
เปลี่ยนเวลาของภาพเคลื่อนไหว
Compose ใช้ภาพเคลื่อนไหวแบบสปริง สำหรับภาพเคลื่อนไหวส่วนใหญ่โดยค่าเริ่มต้น ภาพเคลื่อนไหวแบบสปริงหรือภาพเคลื่อนไหวตามหลักฟิสิกส์จะดูเป็นธรรมชาติมากขึ้น นอกจากนี้ยังขัดจังหวะได้ด้วย เนื่องจากภาพเคลื่อนไหวจะพิจารณาความเร็วปัจจุบันของออบเจ็กต์แทนที่จะใช้เวลาคงที่
หากต้องการลบล้างค่าเริ่มต้น API ของภาพเคลื่อนไหวทั้งหมดที่แสดงก่อนหน้านี้มีความสามารถในการตั้งค่า animationSpec เพื่อปรับแต่งวิธีที่ภาพเคลื่อนไหวทำงาน ไม่ว่าคุณจะต้องการให้ภาพเคลื่อนไหวทำงานในช่วงระยะเวลาหนึ่งหรือให้ภาพเคลื่อนไหวเด้งมากขึ้น
ต่อไปนี้คือข้อมูลสรุปของตัวเลือก animationSpec ต่างๆ
spring: ภาพเคลื่อนไหวตามหลักฟิสิกส์ ซึ่งเป็นค่าเริ่มต้นสำหรับภาพเคลื่อนไหวทั้งหมด คุณสามารถเปลี่ยนความแข็งหรือ `dampingRatio` เพื่อให้ภาพเคลื่อนไหวมีลักษณะที่แตกต่างกันได้tween(ย่อมาจาก between): ภาพเคลื่อนไหวตามระยะเวลา ซึ่งจะสร้างภาพเคลื่อนไหว ระหว่าง 2 ค่าด้วยฟังก์ชันEasingkeyframes: ข้อมูลจำเพาะสำหรับการระบุค่าที่จุดสำคัญบางจุดในภาพเคลื่อนไหวrepeatable: ข้อมูลจำเพาะตามระยะเวลาที่จะทำงานตามจำนวนครั้งที่กำหนด ซึ่งระบุโดยRepeatModeinfiniteRepeatable: ข้อมูลจำเพาะตามระยะเวลาที่จะทำงานไปเรื่อยๆsnap: สแนปไปยังค่าสิ้นสุดทันทีโดยไม่มีภาพเคลื่อนไหว
อ่านเอกสารประกอบฉบับเต็มเพื่อดูข้อมูลเพิ่มเติมเกี่ยวกับ AnimationSpec
แหล่งข้อมูลเพิ่มเติม
ดูตัวอย่างภาพเคลื่อนไหวสนุกๆ เพิ่มเติมใน Compose ได้ที่รายการต่อไปนี้
- ภาพเคลื่อนไหว 5 แบบอย่างรวดเร็วใน Compose
- การทำให้แมงกะพรุนเคลื่อนไหวใน Compose
- การปรับแต่ง
AnimatedContentใน Compose - การทำความเข้าใจฟังก์ชัน Easing ใน Compose