ऐनिमेशन मॉडिफ़ायर और कंपोज़ेबल

Compose में, ऐनिमेशन के सामान्य इस्तेमाल के उदाहरणों को हैंडल करने के लिए, बिल्ट-इन कंपोज़ेबल और मॉडिफ़ायर होते हैं.

पहले से मौजूद ऐनिमेटेड कंपोज़ेबल

AnimatedVisibility की मदद से, किसी ऑब्जेक्ट को दिखने और गायब होने का ऐनिमेशन जोड़ना

ग्रीन कंपोज़ेबल को दिखाया और छिपाया जा रहा है
पहली इमेज. कॉलम में किसी आइटम को दिखाने और छिपाने का ऐनिमेशन

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 ऑब्जेक्ट को जोड़ा जा सकता है. साथ ही, हर ऑब्जेक्ट में अपने हिसाब से पैरामीटर सेट किए जा सकते हैं. ज़्यादा जानकारी के लिए, रेफ़रंस देखें.

EnterTransition और ExitTransition के उदाहरण

EnterTransition ExitTransition
fadeIn
फ़ेड इन ऐनिमेशन
fadeOut
धीरे-धीरे गायब होने वाला ऐनिमेशन
slideIn
स्लाइड इन ऐनिमेशन
slideOut
स्लाइड आउट ऐनिमेशन
slideInHorizontally
हॉरिज़ॉन्टल स्लाइड-इन ऐनिमेशन
slideOutHorizontally
हॉरिज़ॉन्टल तरीके से स्लाइड आउट होने वाला ऐनिमेशन
slideInVertically
वर्टिकल स्लाइड-इन ऐनिमेशन
slideOutVertically
वर्टिकल स्लाइड आउट ऐनिमेशन
scaleIn
स्केल इन ऐनिमेशन
scaleOut
स्केल आउट ऐनिमेशन
expandIn
ऐनिमेशन में बड़ा होना
shrinkOut
छोटा होकर गायब होने वाला ऐनिमेशन
expandHorizontally
हॉरिज़ॉन्टल तौर पर बड़ा होने वाला ऐनिमेशन
shrinkHorizontally
हॉरिज़ॉन्टल तौर पर छोटा होने वाला ऐनिमेशन
expandVertically
वर्टिकल तौर पर बड़ा करने वाला ऐनिमेशन
shrinkVertically
वर्टिकल तौर पर छोटा होने वाला ऐनिमेशन

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 इंस्टेंस को ऐक्सेस करें. ट्रांज़िशन इंस्टेंस में जोड़ी गई कोई भी ऐनिमेशन स्टेट, 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 के बारे में जानकारी के लिए, updateTransition देखें.

AnimatedContent की मदद से, टारगेट की स्थिति के आधार पर ऐनिमेशन बनाना

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 बनाया जा सकता है. using इनफ़िक्स फ़ंक्शन का इस्तेमाल करके, ContentTransform में SizeTransform लागू किया जा सकता है.

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 की मदद से दो लेआउट के बीच ऐनिमेशन जोड़ना

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")
    }
}

पहले से मौजूद ऐनिमेशन मॉडिफ़ायर

animateContentSize की मदद से, कंपोज़ेबल के साइज़ में होने वाले बदलावों को ऐनिमेट करना

हरे रंग का कंपोज़ेबल, जिसके साइज़ में आसानी से बदलाव हो रहा है.
दूसरी इमेज. छोटा और बड़ा साइज़, दोनों के बीच आसानी से ऐनिमेशन करने वाला कंपोज़ेबल

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
        }

) {
}

सूची में शामिल आइटम के ऐनिमेशन

अगर आपको लेज़ी लिस्ट या ग्रिड में आइटम की क्रम बदलने की प्रोसेस को एनिमेट करना है, तो लेज़ी लेआउट आइटम के ऐनिमेशन से जुड़े दस्तावेज़ देखें.