Created
January 5, 2024 18:11
-
-
Save ardakazanci/196a187ce8fb8f329f4a5e6b5e6126fc to your computer and use it in GitHub Desktop.
FlipCard for Jetpack Compose
This file contains 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
@Composable | |
fun FlipCard() { | |
var rotationXDegrees by remember { mutableStateOf(0f) } | |
var rotationYDegrees by remember { mutableStateOf(0f) } | |
val rotationX by animateFloatAsState( | |
targetValue = rotationXDegrees, | |
animationSpec = tween(durationMillis = 1000) | |
) | |
val rotationY by animateFloatAsState( | |
targetValue = rotationYDegrees, | |
animationSpec = tween(durationMillis = 1000) | |
) | |
val dragGestureModifier = Modifier.pointerInput(Unit) { | |
detectDragGestures { _, dragAmount -> | |
rotationXDegrees += dragAmount.y * 0.1f | |
rotationYDegrees -= dragAmount.x * 0.1f | |
} | |
} | |
Card( | |
modifier = Modifier | |
.padding(top = 200.dp) | |
.fillMaxWidth() | |
.graphicsLayer { | |
this.rotationX = rotationX | |
this.rotationY = rotationY | |
cameraDistance = 12 * density | |
} | |
.then(dragGestureModifier), | |
elevation = CardDefaults.cardElevation(5.dp) | |
) { | |
Box(contentAlignment = Alignment.Center) { | |
Image( | |
painter = painterResource(id = R.drawable.front), | |
contentDescription = "Front Image" | |
) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment