Created
May 24, 2021 13:27
-
-
Save marenovakovic/abeae9629213be02824bbcdfb2bea8e4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import androidx.compose.foundation.Image | |
| 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.ImageBitmap | |
| import androidx.compose.ui.graphics.asImageBitmap | |
| 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 | |
| @Composable | |
| fun ParallaxImage( | |
| modifier: Modifier = Modifier, | |
| url: String, | |
| ) { | |
| val coroutineScope = rememberCoroutineScope() | |
| val systemUiController = rememberSystemUiController() | |
| 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() | |
| systemUiController.setStatusBarColor(color, isLight) | |
| } | |
| } | |
| val request = rememberDefaultImageRequest(url = url, target = target) | |
| LaunchedEffect(url) { | |
| loader.execute(request) | |
| } | |
| Image( | |
| modifier = modifier | |
| .height(250.dp) | |
| .fillMaxWidth(), | |
| 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