Created
February 26, 2022 13:49
-
-
Save K1rakishou/ebe20e9aa14400b493a20dc69e69ab37 to your computer and use it in GitHub Desktop.
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
class MainActivity : ComponentActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
ComposePlaygroundTheme { | |
val zoomContentState = remember { ZoomContentState() } | |
ZoomableContent(zoomContentState) { | |
Text( | |
modifier = Modifier.clickable { zoomContentState.zoomIn() }, | |
text = "Test text" | |
) | |
} | |
} | |
} | |
} | |
} | |
@Composable | |
fun ZoomableContent( | |
zoomContentState: ZoomContentState, | |
duration: Int = 2250, | |
content: @Composable () -> Unit | |
) { | |
var scale by remember { mutableStateOf(1f) } | |
LaunchedEffect(zoomContentState) { | |
zoomContentState.zoomEventsFlow.collectLatest { zoomData -> | |
animate( | |
initialValue = 0f, | |
targetValue = 1f, | |
initialVelocity = 0f, | |
animationSpec = tween(durationMillis = duration, easing = LinearEasing), | |
block = { progress, _ -> | |
if (progress < 0.5f) { | |
scale = lerpF(1f, zoomData.value, progress * 2) | |
} else { | |
scale = lerpF(zoomData.value, 1f, (progress * 2) - 1f) | |
} | |
} | |
) | |
} | |
} | |
Box(modifier = Modifier.scale(scale)) { | |
content() | |
} | |
} | |
private fun lerpF(from: Float, to: Float, progress: Float): Float { | |
return from + progress * (to - from) | |
} | |
class ZoomContentState { | |
val zoomEventsFlow = MutableSharedFlow<ZoomData>(extraBufferCapacity = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST) | |
fun zoomIn() { | |
zoomEventsFlow.tryEmit(ZoomData(1.5f)) | |
} | |
} | |
data class ZoomData(val value: Float) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment