You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importandroidx.compose.foundation.Imageimportandroidx.compose.foundation.layout.*importandroidx.compose.foundation.rememberScrollStateimportandroidx.compose.foundation.verticalScrollimportandroidx.compose.runtime.*importandroidx.compose.ui.Modifierimportandroidx.compose.ui.graphics.graphicsLayerimportandroidx.compose.ui.res.painterResourceimportandroidx.compose.ui.unit.dp
@Composable
funParallaxExample() {
// Create a ScrollState object to track the scroll positionval scrollState = rememberScrollState()
// Calculate the parallax offset based on the current scroll positionval parallaxOffset =with(LocalDensity.current) { scrollState.value.dp /3 }
Column(
modifier =Modifier
.fillMaxSize()
.verticalScroll(scrollState)
) {
// Add a background image that will move with the scrollImage(
painter = painterResource(id =R.drawable.background_image),
contentDescription ="Background image",
modifier =Modifier
.graphicsLayer(
translationY =-parallaxOffset
)
.height(400.dp)
.fillMaxWidth()
)
// Add some content that will scroll normallyColumn(
modifier =Modifier.padding(16.dp)
) {
// Add some text content// ...// Add some more image content// ...
}
}
}
Canvas(modifier =Modifier.fillMaxSize()) {
// Define the source rectangle, which specifies the portion of the image to displayval sourceRect =Rect(left =50f, top =50f, right =150f, bottom =150f)
// Define the destination rectangle, which specifies where to draw the portion of the imageval destinationRect =Rect(left =0f, top =0f, right =100f, bottom =100f)
// Draw the portion of the image using the DrawImage API
drawImage(
image = imageResource(R.drawable.my_image),
srcOffset =Offset(x = sourceRect.left, y = sourceRect.top),
srcSize =Size(width = sourceRect.width(), height = sourceRect.height()),
dstOffset =Offset(x = destinationRect.left, y = destinationRect.top),
dstSize =Size(width = destinationRect.width(), height = destinationRect.height())
)
}
The text was updated successfully, but these errors were encountered:
The text was updated successfully, but these errors were encountered: