Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 = 50,
- 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) {
- // zoom in
- scale = lerpF(1f, zoomData.value, progress * 2)
- } else {
- // zoom back out
- 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)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement