A simple library for automatically animating between Compose states.

Overview

compose-autotransition

Status: Experimental

A simple library for automatically animating between Compose states.

var scale by remember { mutableStateOf(1f) }
var elevation by remember { mutableStateOf(0.dp) }

with(rememberAutoTransition()) {
    Button(onClick = {
        withAnimation {
            scale = 0.5f
            elevation = 8.dp
        }
    }) { /**/ }
}

The main component of this library is the AutoTransition.withAnimation function, which takes a block and animates changes to any snapshot state objects (such as those created by mutableStateOf) written to inside the block. Use the rememberAutoTransition() Composable function to get an instance of AutoTransition.

withAnimation uses Compose's snapshot system to determine what state you changed in the block and then launches a coroutine to animate it. It does this by running the block in a snapshot, then disposing the snapshot without applying it. Since the snapshot is never applied, it's important to not perform side effects in the withAnimation block, or to write to any states not backed by snapshot state.

Demo

withAnimation.demo.mp4

Customization

By default, withTransition can animate changes to any MutableState objects holding values of types that Compose ships with TwoWayConverter vector converters for. This set is defined in DefaultAdapters.kt. To specify how to handle additional types (either additional state object types or value types inside MutableStates), provide a custom AutoTransitionFactory using AutoTransitionFactory.Builder and LocalAutoTransitionFactory.

Here's an example of providing an adapter for a custom value type:

class Temp(val degrees: Float)

@Composable
fun App() {
    val factory = remember {
        AutoTransitionFactory.Builder()
            .addAdapterRegistry(MutableStateAdapter(
                Temp::class,
                TwoWayConverter(
                    convertToVector = { /**/ },
                    convertFromVector = { /**/ }
                )
            ))
            .build()
    }

    CompositionLocalProvider(LocalAutoTransitionFactory provides factory) {
        // Rest of app.
        HomeScreen()
    }
}

@Composable
fun HomeScreen() {
    with(rememberAutoTransition()) {
        //
    }
}

The AutoTransitionFactory.Builder can also override the default animation spec.

TODO

  • Write docs.
  • Release on Maven.
  • Support multiplatform.
You might also like...
A Kotlin library to use Jetpack Compose in Android and iOS. Allow to write UI for both in Kotin. Still experimental as many compose features are not yet available.
A Kotlin library to use Jetpack Compose in Android and iOS. Allow to write UI for both in Kotin. Still experimental as many compose features are not yet available.

Multiplatform Compose A Kotlin library to use Jetpack Compose in Android and iOS. Allow to write UI for both in Kotin. Still experimental as many comp

Compose Curved-Scroll is an Android Jetpack Compose library made with ❤️
Compose Curved-Scroll is an Android Jetpack Compose library made with ❤️

Compose-Curved-Scroll-library Compose Curved-Scroll is an Android Jetpack Compos

Android.compose.squircle - Android LightWeight Squircle Library for JetPack Compose

Android LightWeight Squircle Library for JetPack Compose Usage Based on Compose

Compose-html - An Android library which provides HTML support for Jetpack Compose texts
Compose-html - An Android library which provides HTML support for Jetpack Compose texts

HtmlCompose An Android library which provides HTML support for Jetpack Compose t

Lightweight library to tweak the fling behaviour in Android. This library is only compatible with Jetpack-Compose.
Lightweight library to tweak the fling behaviour in Android. This library is only compatible with Jetpack-Compose.

Flinger (Only compatible with compose) What is Flinger? Flinger is a plugin that is made on top of jetpack compose that will help the developer to twe

A simple implementation of collapsing toolbar for Jetpack Compose
A simple implementation of collapsing toolbar for Jetpack Compose

compose-collapsing-toolbar A simple implementation of CollapsingToolbarLayout for Jetpack Compose Installation You should add mavenCentral() repositor

A simple Snake application to demonstrate the use of Compose for Desktop platform with Kotlin
A simple Snake application to demonstrate the use of Compose for Desktop platform with Kotlin

Snake App using Compose for Desktop A simple Snake desktop application to demonstrate the use of Compose UI toolkit for Desktop platform with Kotlin.

A simple 'Slide to Unlock' Material widget for Android, written in Jetpack Compose
A simple 'Slide to Unlock' Material widget for Android, written in Jetpack Compose

SlideTodo A simple 'Slide to Unlock' Material widget for Android, written in Jetpack Compose you can find source code here Getting Started allprojects

Simple space game, built with Compose for Desktop!
Simple space game, built with Compose for Desktop!

Asteroids, built with Compose for Desktop Want to learn how I built it? Read the two-part article in which I describe the most interesting parts of th

Owner
Zach Klippenstein
Engineer at Google, previously Square, Amazon. Opinions my own.
Zach Klippenstein
Example Jetpack Compose Android App, that uses the newest mechanisms, like StateFlow, SharedFlow, etc. to manage states and handle events. ViewModel, UI and Screenshot tests included :)

AndroidMVIExample Example Jetpack Compose Android App, that uses the newest mechanisms, like StateFlow, SharedFlow, etc. to manage states and handle e

Patryk Kosieradzki 55 Nov 18, 2022
This library will make it easier to pass arguments between screens in Jetpack Compose.

Compose Navigation This library will make it easier to pass arguments between screens in Jetpack Compose Setup allprojects { repositories { ...

Nguyen Van Tan 1 Oct 30, 2021
Simple composable for rendering transitions between backstacks.

compose-backstack Simple library for Jetpack Compose for rendering backstacks of screens and animated transitions when the stack changes. It is not a

Zach Klippenstein 408 Jan 3, 2023
Sample project to demonstrate how to have clear and better interactions between composables and viewmodels.

Form Validation Sample project to demonstrate how to have clear and better interactions between composables and viewmodels. Concepts used uiState conc

Saurabh Pant 20 Dec 22, 2022
Compose-Ratingbar-library - A simple implementation for rating bar in Jetpack Compose

Compose-Ratingbar-library - A simple implementation for rating bar in Jetpack Compose

Mahmoud Hussein 14 Dec 21, 2022
It's a simple app written in Kotlin that shows a simple solution for how to save an image into Firebase Storage, save the URL in Firestore, and read it back using Jetpack Compose.

It's a simple app written in Kotlin that shows a simple solution for how to save an image into Firebase Storage, save the URL in Firestore, and read it back using Jetpack Compose.

Alex 10 Dec 29, 2022
Luis David Orellana 3 Jun 20, 2022
Svg-to-compose-intellij - A simple Android Studio plugin to generate Jetpack Compose ImageVector icons

svg-to-compose-intellij A simple Android Studio plugin to generate Jetpack Compo

Pavel Shurmilov 21 Dec 15, 2022
A Simple Blog App using Jetpack Compose, Flow, Navigation Compose, Room and Firebase

BlogCompose A Simple Blog App using Jetpack Compose, Flow, Navigation Compose, Room and Firebase Instructions Download your Firebase configuration fil

null 4 Oct 10, 2022
A simple authentication application using Jetpack compose to illustrate signin and sign up using Mvvm, Kotlin and jetpack compose

Authentication A simple authentication application using Jetpack compose to illustrate signin and sign up using Mvvm, Kotlin and jetpack compose Scree

Felix Kariuki 5 Dec 29, 2022