Hello everyone I’m trying to track whether a composable is in the center of the screen.
I have an **LazyColumn **with a lot of content (elements can have different types and sizes). Recently, I was given the task to track whether a video (part of the **Post **component) is in the center of the screen and, depending on this, play it or stop it.
Since AndroidView and android.media3.ui.PlayerView are used for the video, I decided to use ViewTreeObserver.OnGlobalLayoutListener and it seems that the performance has not dropped and the scrolling is smooth enough.
But is it possible to do this in Compose so that performance does not drop?
At the moment, I have such an implementation:
AndroidView(
modifier = modifier.
onGloballyPositioned { coordinates ->
val position = coordinates.positionInWindow()
val start = position.y.roundToInt()
val end = coordinates.size.height + start
if (screenCenter in start..end) {
exoPlayer.play()
} else {
exoPlayer.pause()
}
},
factory = { viewContext ->
PlayerView(viewContext).apply {
player = exoPlayer
resizeMode = AspectRatioFrameLayout.RESIZE_MODE_FIXED_WIDTH}
}
)
And the scrolling feels less smooth than ViewTreeObserver.OnGlobalLayoutListener, is there any way to improve it? Maybe i am doing someting wrong?
Umid Olimzhanov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.