Marko Novakovic
05/25/2021, 8:15 PMMarko Novakovic
05/25/2021, 8:15 PM@Composable
fun ParallaxImage(
modifier: Modifier = Modifier,
url: String,
scrollState: ScrollState,
) {
SideEffect {
println("recomposing ParallaxImage")
}
val coroutineScope = rememberCoroutineScope()
var parallaxColor by remember { mutableStateOf(Color.Transparent) }
var image by remember { mutableStateOf(ImageBitmap(1, 1)) }
val loader = ImageLoader(LocalContext.current)
val target = rememberCoilTarget { bitmap ->
println("setting image")
image = bitmap.asImageBitmap()
coroutineScope.launch {
val color = computeColor(bitmap)
println("setting parallax color")
parallaxColor = color
}
}
val request = rememberDefaultImageRequest(url = url, target = target)
LaunchedEffect(url) {
loader.execute(request)
}
Image(
modifier = modifier
.height(250.dp)
.fillMaxWidth()
.background(parallaxColor)
.graphicsLayer { alpha = min(1f, 1 - (scrollState.value / 400f)) },
bitmap = image,
contentScale = ContentScale.FillWidth,
contentDescription = null,
)
}
Marko Novakovic
05/25/2021, 8:16 PMrecomposing ParallaxImage
setting image
recomposing ParallaxImage
setting parallax color
recomposing ParallaxImage
Marko Novakovic
05/25/2021, 8:20 PMParallaxImage
is recomposed for every state variable changed inside of it. Is it because those `state`s are used through body of ParallaxImage
? but again, shouldn’t just composables inside of it be recomposed?Zach Klippenstein (he/him) [MOD]
05/25/2021, 9:00 PMMarko Novakovic
05/26/2021, 4:58 AMZach Klippenstein (he/him) [MOD]
05/26/2021, 5:01 PMparallaxColor
and image
are read in the ParallaxImage
function, which is why the entire ParallaxImage
function gets recomposed when they changeZach Klippenstein (he/him) [MOD]
05/26/2021, 5:02 PMMarko Novakovic
05/26/2021, 6:16 PMZach Klippenstein (he/him) [MOD]
05/26/2021, 8:28 PMMarko Novakovic
05/26/2021, 9:24 PMMarko Novakovic
05/28/2021, 7:29 PMZach Klippenstein (he/him) [MOD]
05/28/2021, 7:34 PMMarko Novakovic
05/28/2021, 7:49 PM