Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save marenovakovic/e2a6ae15cc40d11233106c504f780be3 to your computer and use it in GitHub Desktop.
Save marenovakovic/e2a6ae15cc40d11233106c504f780be3 to your computer and use it in GitHub Desktop.
import androidx.compose.foundation.Image
import androidx.compose.foundation.ScrollState
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import coil.ImageLoader
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import yourApp.rememberCoilTarget
import yourApp.rememberDefaultImageRequest
import yourApp.computeDominantTopSectionColor
import kotlinx.coroutines.launch
import kotlin.math.min
@Composable
fun ParallaxImage(
modifier: Modifier = Modifier,
url: String,
scrollState: ScrollState,
) {
val coroutineScope = rememberCoroutineScope()
val systemUiController = rememberSystemUiController()
var parallaxColor by remember { mutableStateOf(Color.Transparent) }
var image by remember { mutableStateOf(ImageBitmap(1, 1)) }
val loader = ImageLoader(LocalContext.current)
val target = rememberCoilTarget { bitmap ->
image = bitmap.asImageBitmap()
coroutineScope.launch {
val (color, isLight) = bitmap
.computeDominantTopSectionColor()
parallaxColor = color
systemUiController.setStatusBarColor(color, isLight)
}
}
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,
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment