MultiAspectCarouselItemDrawInfo


Interface to hold information about a multi-aspect carousel item and its draw info that change as the item scrolls.

Example of MultiAspectCarouselItemDrawInfo usage:

import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.icons.filled.Image
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.carousel.MultiAspectCarouselItemDrawInfo
import androidx.compose.material3.carousel.MultiAspectCarouselScope
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp

data class CarouselItem(
    val id: Int,
    @DrawableRes val imageResId: Int,
    @StringRes val contentDescriptionResId: Int,
    val mainAxisSize: Dp,
)

val items =
    listOf(
        CarouselItem(
            0,
            R.drawable.carousel_image_1,
            R.string.carousel_image_1_description,
            305.dp,
        ),
        CarouselItem(
            1,
            R.drawable.carousel_image_2,
            R.string.carousel_image_2_description,
            205.dp,
        ),
        CarouselItem(
            2,
            R.drawable.carousel_image_3,
            R.string.carousel_image_3_description,
            275.dp,
        ),
        CarouselItem(
            3,
            R.drawable.carousel_image_4,
            R.string.carousel_image_4_description,
            350.dp,
        ),
        CarouselItem(
            4,
            R.drawable.carousel_image_5,
            R.string.carousel_image_5_description,
            100.dp,
        ),
    )

MultiAspectCarouselScope {
    val state = rememberLazyListState()
    LazyRow(
        state = state,
        contentPadding = PaddingValues(16.dp),
        horizontalArrangement = Arrangement.spacedBy(8.dp),
        modifier = Modifier.fillMaxWidth().height(221.dp),
    ) {
        itemsIndexed(items) { i, item ->
            val drawInfo = remember { MultiAspectCarouselItemDrawInfo(i, state) }
            Image(
                painter = painterResource(id = item.imageResId),
                contentDescription = stringResource(item.contentDescriptionResId),
                modifier =
                    Modifier.width(item.mainAxisSize)
                        .height(205.dp)
                        .maskClip(MaterialTheme.shapes.extraLarge, drawInfo),
                contentScale = ContentScale.Crop,
            )
        }
    }
}

Summary

Public properties

Int

The index of this item in the list.

Cmn
Boolean

True if the main scrolling axis is horizontal.

Cmn
Float

The offset in pixels from the end of this item's bounds by which the item should be masked.

Cmn
Float

The offset in pixels from the start of this item's bounds by which the item should be masked.

Cmn
Float

The maximum size this item will be in the main scrolling axis.

Cmn
Float

The smallest size this item will ever be masked to in the main scrolling axis.

Cmn
Float

The distance in pixels to translate this item's content in the main scrolling axis.

Cmn
Float

The current size of this item in the main scrolling axis taking into account any masking from maskStart and maskEnd.

Cmn

Public properties

index

val indexInt

The index of this item in the list.

isHorizontal

val isHorizontalBoolean

True if the main scrolling axis is horizontal.

maskEnd

val maskEndFloat

The offset in pixels from the end of this item's bounds by which the item should be masked.

When this item exists the end or bottom of the viewport, maskEnd will increase to make it look like this item is being squeezed against the edge of the viewport.

maskStart

val maskStartFloat

The offset in pixels from the start of this item's bounds by which the item should be masked.

When this item exists the start or top of the viewport, maskStart will increase to make it look like this item is being squeezed against the edge of the viewport.

maxSize

val maxSizeFloat

The maximum size this item will be in the main scrolling axis. This is the fully unmasked size of the item with no mask applied. size will never be greater than maxSize.

minSize

val minSizeFloat

The smallest size this item will ever be masked to in the main scrolling axis. size will never be less than minSize.

parallax

val parallaxFloat

The distance in pixels to translate this item's content in the main scrolling axis.

When an item is exiting the viewport, parallax will translate the content in the opposite direction of scroll, making it look like the item is moving more slowly and being compressed against the edge of the viewport.

size

val sizeFloat

The current size of this item in the main scrolling axis taking into account any masking from maskStart and maskEnd.