Created
March 24, 2021 11:25
-
-
Save warting/9873b2dd92ae87d053b72b9899e9542b to your computer and use it in GitHub Desktop.
Jetpack compose zoomable image view example
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.background | |
import androidx.compose.foundation.gestures.rememberTransformableState | |
import androidx.compose.foundation.gestures.transformable | |
import androidx.compose.foundation.layout.Arrangement | |
import androidx.compose.foundation.layout.Column | |
import androidx.compose.foundation.layout.fillMaxSize | |
import androidx.compose.runtime.Composable | |
import androidx.compose.runtime.getValue | |
import androidx.compose.runtime.mutableStateOf | |
import androidx.compose.runtime.remember | |
import androidx.compose.runtime.setValue | |
import androidx.compose.ui.Alignment | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.geometry.Offset | |
import androidx.compose.ui.graphics.Color | |
import androidx.compose.ui.graphics.graphicsLayer | |
import androidx.compose.ui.layout.ContentScale | |
import dev.chrisbanes.accompanist.glide.GlideImage | |
@Composable | |
fun ZoomableImage(image: Any, maxScale: Float = 4f, minScale: Float = 0.7f) { | |
// set up all transformation states | |
var scale by remember { mutableStateOf(1f) } | |
var rotation by remember { mutableStateOf(0f) } | |
var offset by remember { mutableStateOf(Offset.Zero) } | |
val state = rememberTransformableState { zoomChange, offsetChange, rotationChange -> | |
scale *= zoomChange | |
rotation += rotationChange | |
offset += offsetChange | |
} | |
Column( | |
verticalArrangement = Arrangement.Center, | |
horizontalAlignment = Alignment.CenterHorizontally, | |
modifier = Modifier | |
.background(Color.Black) | |
) { | |
GlideImage( | |
data = image, | |
modifier = Modifier | |
.fillMaxSize() | |
.transformable(state = state) | |
.graphicsLayer( | |
scaleX = scale, | |
scaleY = scale, | |
rotationZ = rotation, | |
translationX = offset.x, | |
translationY = offset.y | |
), | |
contentScale = ContentScale.Crop, | |
contentDescription = null | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment