शेयर किए गए एलिमेंट के इस्तेमाल के सामान्य उदाहरण

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

एसिंक्रोनस इमेज

आम तौर पर, इमेज को एसिंक्रोनस तरीके से लोड करने के लिए किसी लाइब्रेरी का इस्तेमाल किया जाता है. जैसे, Coil के AsyncImage कंपोज़ेबल का इस्तेमाल करते समय. दो कंपोज़ेबल के बीच इसे आसानी से काम करने के लिए, हमारा सुझाव है कि placeholderMemoryCacheKey() और memoryCacheKey() को एक ही कुंजी पर सेट करें. यह कुंजी, शेयर किए गए एलिमेंट की कुंजी से ली गई स्ट्रिंग के तौर पर होनी चाहिए, ताकि मैच किए गए शेयर किए गए एलिमेंट के लिए कैश मेमोरी की कुंजी एक जैसी हो. शेयर किया गया नया एलिमेंट, नई इमेज लोड होने तक प्लेसहोल्डर के तौर पर, मैच की गई इमेज की कैश मेमोरी का इस्तेमाल करेगा.

AsyncImage का इस्तेमाल आम तौर पर इस तरह किया जाता है:

AsyncImage(
    model = ImageRequest.Builder(LocalContext.current)
        .data("your-image-url")
        .crossfade(true)
        .placeholderMemoryCacheKey("image-key") //  same key as shared element key
        .memoryCacheKey("image-key") // same key as shared element key
        .build(),
    placeholder = null,
    contentDescription = null,
    modifier = Modifier
        .size(120.dp)
        .sharedBounds(
            rememberSharedContentState(
                key = "image-key"
            ),
            animatedVisibilityScope = this
        )
)

टेक्स्ट

fontSize में हुए बदलावों को ऐनिमेट करने के लिए, Modifier.sharedBounds(), resizeMode = ScaleToBounds() का इस्तेमाल करें. इस ट्रांज़िशन से, साइज़ में बदलाव आसानी से होता है. contentScale पैरामीटर में बदलाव करके, किसी खास फ़ॉन्ट के वेट या स्टाइल को ऐनिमेट किया जा सकता है.

Text(
    text = "This is an example of how to share text",
    modifier = Modifier
        .wrapContentWidth()
        .sharedBounds(
            rememberSharedContentState(
                key = "shared Text"
            ),
            animatedVisibilityScope = this,
            enter = fadeIn(),
            exit = fadeOut(),
            resizeMode = SharedTransitionScope.ResizeMode.ScaleToBounds()
        )
)

TextAlign बदलावों में डिफ़ॉल्ट रूप से ऐनिमेशन नहीं होता है. इसके बजाय, शेयर किए गए ट्रांज़िशन के लिए अलग-अलग TextAlign इस्तेमाल करने के बजाय, Modifier.wrapContentSize() या Modifier.wrapContentWidth() का इस्तेमाल करें.