Applies an animated marquee effect to the modified content if it's too wide to fit in the available space.
BasicFocusableMarqueeSample
@Preview(showBackground = true)
@Composable
fun BasicFocusableMarqueeSample() {
val focusRequester = remember { FocusRequester() }
// Marquee only animates when the content doesn't fit in the max width.
Column(Modifier.width(30.dp)) {
Text(
"hello world",
Modifier.clickable { focusRequester.requestFocus() }
.basicMarquee(animationMode = MarqueeAnimationMode.WhileFocused)
.focusRequester(focusRequester)
.focusable(),
)
}
}
BasicMarqueeSample
@Preview(showBackground = true)
@Composable
fun BasicMarqueeSample() {
// Marquee only animates when the content doesn't fit in the max width.
Column(Modifier.width(30.dp)) { Text("hello world", Modifier.basicMarquee()) }
}
BasicMarqueeWithFadedEdgesSample
@Preview(showBackground = true)
@Composable
fun BasicMarqueeWithFadedEdgesSample() {
val edgeWidth = 32.dp
fun ContentDrawScope.drawFadedEdge(leftEdge: Boolean) {
val edgeWidthPx = edgeWidth.toPx()
drawRect(
topLeft = Offset(if (leftEdge) 0f else size.width - edgeWidthPx, 0f),
size = Size(edgeWidthPx, size.height),
brush =
Brush.horizontalGradient(
colors = listOf(Color.Transparent, Color.Black),
startX = if (leftEdge) 0f else size.width,
endX = if (leftEdge) edgeWidthPx else size.width - edgeWidthPx,
),
blendMode = BlendMode.DstIn,
)
}
Text(
"the quick brown fox jumped over the lazy dogs",
Modifier.widthIn(max = edgeWidth * 4)
// Rendering to an offscreen buffer is required to get the faded edges' alpha to be
// applied only to the text, and not whatever is drawn below this composable (e.g. the
// window).
.graphicsLayer { compositingStrategy = CompositingStrategy.Offscreen }
.drawWithContent {
drawContent()
drawFadedEdge(leftEdge = true)
drawFadedEdge(leftEdge = false)
}
.basicMarquee(
// Animate forever.
iterations = Int.MAX_VALUE,
spacing = MarqueeSpacing(0.dp),
)
.padding(start = edgeWidth),
)
}