Compose में, ऐनिमेशन के सामान्य इस्तेमाल के उदाहरणों को मैनेज करने के लिए, पहले से मौजूद कंपोज़ेबल और मॉडिफ़ायर होते हैं.
पहले से मौजूद ऐनिमेटेड कंपोज़ेबल
Compose में कई ऐसे कॉम्पोज़ेबल होते हैं जो कॉन्टेंट के दिखने, गायब होने, और लेआउट में बदलाव होने पर ऐनिमेशन दिखाते हैं.
दिखने और गायब होने के लिए ऐनिमेशन जोड़ना
AnimatedVisibility
कंपोज़ेबल, अपने कॉन्टेंट के दिखने और गायब होने की प्रोसेस को ऐनिमेट करता है.
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 // ... }
डिफ़ॉल्ट रूप से, कॉन्टेंट फ़ेड इन और बड़ा होकर दिखता है. साथ ही, फ़ेड आउट और छोटा होकर गायब हो जाता है. EnterTransition और ExitTransition ऑब्जेक्ट तय करके, इस ट्रांज़िशन को अपनी पसंद के मुताबिक बनाएं.
var visible by remember { mutableStateOf(true) } val density = LocalDensity.current AnimatedVisibility( visible = visible, enter = slideInVertically { // Slide in from 40 dp from the top. with(density) { -40.dp.roundToPx() } } + expandVertically( // Expand from the top. expandFrom = Alignment.Top ) + fadeIn( // Fade in with the initial alpha of 0.3f. initialAlpha = 0.3f ), exit = slideOutVertically() + shrinkVertically() + fadeOut() ) { Text( "Hello", Modifier .fillMaxWidth() .height(200.dp) ) }
पिछले उदाहरण में दिखाए गए तरीके से, एक से ज़्यादा EnterTransition या ExitTransition ऑब्जेक्ट को + ऑपरेटर के साथ जोड़ा जा सकता है. साथ ही, हर ऑब्जेक्ट के व्यवहार को पसंद के मुताबिक बनाने के लिए, वैकल्पिक पैरामीटर इस्तेमाल किए जा सकते हैं. ज़्यादा जानकारी के लिए, रेफ़रंस पेज देखें.
एंटर और एग्ज़िट ट्रांज़िशन के उदाहरण
AnimatedVisibility में एक ऐसा वैरिएंट भी है जो MutableTransitionState आर्ग्युमेंट लेता है. इससे, कंपोज़िशन ट्री में AnimatedVisibility कंपोज़ेबल को जोड़ते ही, ऐनिमेशन ट्रिगर किया जा सकता है. यह ऐनिमेशन की स्थिति को देखने के लिए भी उपयोगी है.
// Create a MutableTransitionState<Boolean> for the AnimatedVisibility. val state = remember { MutableTransitionState(false).apply { // Start the animation immediately. targetState = true } } Column { AnimatedVisibility(visibleState = state) { Text(text = "Hello, world!") } // Use the MutableTransitionState to know the current animation state // of the AnimatedVisibility. Text( text = when { state.isIdle && state.currentState -> "Visible" !state.isIdle && state.currentState -> "Disappearing" state.isIdle && !state.currentState -> "Invisible" else -> "Appearing" } ) }
चाइल्ड ऑब्जेक्ट को सीन में शामिल करने और हटाने पर दिखने वाले ऐनिमेशन
AnimatedVisibility (डायरेक्ट या इनडायरेक्ट चाइल्ड) के अंदर मौजूद कॉन्टेंट,
animateEnterExit
मॉडिफ़ायर का इस्तेमाल कर सकता है. इससे हर कॉन्टेंट के लिए, अलग-अलग ऐनिमेशन तय किया जा सकता है. इनमें से हर चाइल्ड के लिए विज़ुअल इफ़ेक्ट, AnimatedVisibility कंपोज़ेबल में बताए गए ऐनिमेशन और चाइल्ड के खुद के एंटर और एग्ज़िट ऐनिमेशन का कॉम्बिनेशन होता है.
var visible by remember { mutableStateOf(true) } AnimatedVisibility( visible = visible, enter = fadeIn(), exit = fadeOut() ) { // Fade in/out the background and the foreground. Box( Modifier .fillMaxSize() .background(Color.DarkGray) ) { Box( Modifier .align(Alignment.Center) .animateEnterExit( // Slide in/out the inner box. enter = slideInVertically(), exit = slideOutVertically() ) .sizeIn(minWidth = 256.dp, minHeight = 64.dp) .background(Color.Red) ) { // Content of the notification… } } }
कुछ मामलों में, हो सकता है कि आप AnimatedVisibility को कोई भी ऐनिमेशन लागू न करने दें, ताकि बच्चे animateEnterExit के हिसाब से अपने-अपने ऐनिमेशन चुन सकें. इसके लिए, AnimatedVisibility कंपोज़ेबल में EnterTransition.None और ExitTransition.None की वैल्यू तय करें.
कस्टम ऐनिमेशन जोड़ना
अगर आपको पहले से मौजूद एंटर और एग्ज़िट ऐनिमेशन के अलावा, कस्टम ऐनिमेशन इफ़ेक्ट जोड़ने हैं, तो AnimatedVisibility के लिए कॉन्टेंट लैम्डा में transition प्रॉपर्टी का इस्तेमाल करके, पहले से मौजूद Transition इंस्टेंस को ऐक्सेस करें. Transition इंस्टेंस में जोड़ी गई कोई भी ऐनिमेशन स्टेट, AnimatedVisibility के एंटर और एक्ज़िट ऐनिमेशन के साथ-साथ चलेगी. AnimatedVisibility, Transition में मौजूद सभी ऐनिमेशन के खत्म होने तक इंतज़ार करता है. इसके बाद, वह कॉन्टेंट को हटाता है.
Transition से अलग बनाए गए एग्ज़िट ऐनिमेशन (जैसे, animate*AsState का इस्तेमाल करके बनाए गए ऐनिमेशन) के लिए, AnimatedVisibility उनका हिसाब नहीं रख पाएगा. इसलिए, हो सकता है कि ऐनिमेशन खत्म होने से पहले ही, कंपोज़ेबल कॉन्टेंट को हटा दिया जाए.
var visible by remember { mutableStateOf(true) } AnimatedVisibility( visible = visible, enter = fadeIn(), exit = fadeOut() ) { // this: AnimatedVisibilityScope // Use AnimatedVisibilityScope#transition to add a custom animation // to the AnimatedVisibility. val background by transition.animateColor(label = "color") { state -> if (state == EnterExitState.Visible) Color.Blue else Color.Gray } Box( modifier = Modifier .size(128.dp) .background(background) ) }
ऐनिमेशन मैनेज करने के लिए Transition का इस्तेमाल करने के बारे में ज़्यादा जानने के लिए, ट्रांज़िशन की मदद से एक साथ कई प्रॉपर्टी को ऐनिमेट करना लेख पढ़ें.
टारगेट स्टेट के आधार पर ऐनिमेट करना
AnimatedContent कंपोज़ेबल, टारगेट स्टेट के आधार पर कॉन्टेंट में बदलाव होने पर, उसे ऐनिमेट करता है.
Row { var count by remember { mutableIntStateOf(0) } Button(onClick = { count++ }) { Text("Add") } AnimatedContent( targetState = count, label = "animated content" ) { targetCount -> // Make sure to use `targetCount`, not `count`. Text(text = "Count: $targetCount") } }
डिफ़ॉल्ट रूप से, शुरुआती कॉन्टेंट फ़ेड आउट हो जाता है और फिर टारगेट कॉन्टेंट फ़ेड इन हो जाता है. इस व्यवहार को फ़ेड थ्रू कहा जाता है.
transitionSpec पैरामीटर में ContentTransform ऑब्जेक्ट की जानकारी देकर, इस ऐनिमेशन के व्यवहार को अपनी पसंद के मुताबिक बनाया जा सकता है. with इनफ़िक्स फ़ंक्शन का इस्तेमाल करके, EnterTransition ऑब्जेक्ट को ExitTransition ऑब्जेक्ट के साथ जोड़कर, ContentTransform का इंस्टेंस बनाया जा सकता है. ContentTransform ऑब्जेक्ट पर SizeTransform लागू किया जा सकता है. इसके लिए, इसे using इनफ़िक्स फ़ंक्शन के साथ अटैच करें.
AnimatedContent( targetState = count, transitionSpec = { // Compare the incoming number with the previous number. if (targetState > initialState) { // If the target number is larger, it slides up and fades in // while the initial (smaller) number slides up and fades out. slideInVertically { height -> height } + fadeIn() togetherWith slideOutVertically { height -> -height } + fadeOut() } else { // If the target number is smaller, it slides down and fades in // while the initial number slides down and fades out. slideInVertically { height -> -height } + fadeIn() togetherWith slideOutVertically { height -> height } + fadeOut() }.using( // Disable clipping since the faded slide-in/out should // be displayed out of bounds. SizeTransform(clip = false) ) }, label = "animated content" ) { targetCount -> Text(text = "$targetCount") }

EnterTransition से यह तय होता है कि टारगेट कॉन्टेंट कैसे दिखना चाहिए और
ExitTransition से यह तय होता है कि शुरुआती कॉन्टेंट कैसे गायब होना चाहिए. AnimatedVisibility के लिए उपलब्ध EnterTransition और ExitTransition के सभी फ़ंक्शन के अलावा, AnimatedContent में slideIntoContainer और slideOutOfContainer भी उपलब्ध हैं.
ये slideInHorizontally/Vertically और slideOutHorizontally/Vertically के आसान विकल्प हैं. ये AnimatedContent कॉन्टेंट के शुरुआती कॉन्टेंट और टारगेट कॉन्टेंट के साइज़ के आधार पर, स्लाइड की दूरी का हिसाब लगाते हैं.
SizeTransform से यह तय होता है कि शुरुआती और टारगेट कॉन्टेंट के बीच, साइज़ को किस तरह ऐनिमेट किया जाना चाहिए. ऐनिमेशन बनाते समय, आपके पास शुरुआती साइज़ और टारगेट साइज़, दोनों का ऐक्सेस होता है. SizeTransform यह भी कंट्रोल करता है कि ऐनिमेशन के दौरान, कॉन्टेंट को कॉम्पोनेंट के साइज़ के हिसाब से क्लिप किया जाना चाहिए या नहीं.
var expanded by remember { mutableStateOf(false) } Surface( color = MaterialTheme.colorScheme.primary, onClick = { expanded = !expanded } ) { AnimatedContent( targetState = expanded, transitionSpec = { fadeIn(animationSpec = tween(150, 150)) togetherWith fadeOut(animationSpec = tween(150)) using SizeTransform { initialSize, targetSize -> if (targetState) { keyframes { // Expand horizontally first. IntSize(targetSize.width, initialSize.height) at 150 durationMillis = 300 } } else { keyframes { // Shrink vertically first. IntSize(initialSize.width, targetSize.height) at 150 durationMillis = 300 } } } }, label = "size transform" ) { targetExpanded -> if (targetExpanded) { Expanded() } else { ContentIcon() } } }

चाइल्ड एलिमेंट के एंट्री और एग्ज़िट ट्रांज़िशन को ऐनिमेट करना
AnimatedVisibility की तरह ही, animateEnterExit मॉडिफ़ायर, AnimatedContent के कॉन्टेंट लैम्डा में उपलब्ध होता है. हर डायरेक्ट या इनडायरेक्ट चाइल्ड पर, EnterAnimation और ExitAnimation को अलग-अलग लागू करने के लिए, इसका इस्तेमाल करें.
कस्टम ऐनिमेशन जोड़ना
AnimatedVisibility की तरह ही, transition फ़ील्ड भी AnimatedContent के कॉन्टेंट लैम्डा में उपलब्ध होता है. इसका इस्तेमाल करके, अपनी पसंद के मुताबिक ऐसा ऐनिमेशन इफ़ेक्ट बनाएं जो AnimatedContent ट्रांज़िशन के साथ-साथ चलता हो. ज़्यादा जानकारी के लिए,
updateTransition देखें.
दो लेआउट के बीच ऐनिमेशन बनाना
Crossfade, क्रॉसफ़ेड ऐनिमेशन की मदद से दो लेआउट के बीच ऐनिमेशन दिखाता है. current पैरामीटर को पास की गई वैल्यू को टॉगल करके, कॉन्टेंट को क्रॉसफ़ेड ऐनिमेशन के साथ स्विच किया जाता है.
var currentPage by remember { mutableStateOf("A") } Crossfade(targetState = currentPage, label = "cross fade") { screen -> when (screen) { "A" -> Text("Page A") "B" -> Text("Page B") } }
पहले से मौजूद ऐनिमेशन मॉडिफ़ायर
Compose, कंपोज़ेबल पर सीधे तौर पर खास बदलावों को ऐनिमेट करने के लिए मॉडिफ़ायर उपलब्ध कराता है.
कंपोज़ेबल के साइज़ में होने वाले बदलावों को ऐनिमेट करना
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 } ) { }
सूची के आइटम के ऐनिमेशन
अगर आपको लेज़ी लिस्ट या ग्रिड में आइटम के क्रम को फिर से तय करने के लिए ऐनिमेशन बनाना है, तो लेज़ी लेआउट आइटम ऐनिमेशन का दस्तावेज़ देखें.
आपके लिए सुझाव
- ध्यान दें: JavaScript बंद होने पर, लिंक टेक्स्ट दिखता है
- वैल्यू के आधार पर ऐनिमेशन
- Compose में ऐनिमेशन
- ऐनिमेशन टूलिंग की सुविधा {:#tooling}