Orbital

🪐 Jetpack Compose animation library that allows you to implement animations such as shared element transition. This library support Kotlin Multiplatform (Android, iOS, Desktop, macOS, and js)
Download

Gradle
Add the dependency below to your module's build.gradle file:
dependencies {
implementation("com.github.skydoves:orbital:0.4.0")
}
Note: This is an experimental library that demonstrates various animations with Jetpack Compose. Please make sure that your project uses Jetpack Compose 1.5.4, Compose Compiler 1.5.4, and Kotlin 1.9.20.
For Kotlin Multiplatform, add the dependency below to your module's build.gradle.kts file:
sourceSets {
val commonMain by getting {
dependencies {
implementation("com.github.skydoves:orbital:$version")
}
}
}
Usage
You can implement three kinds of animations with Orbital: Movement, Transformation, and Shared Element Transition.
Basically, you can run animation with Orbital Composable function, which provides OrbitalScope that allows you to create animations.
Transformation
The example below shows how to implement resizing animation with the animateTransformation extension of the OrbitalScope.
The rememberContentWithOrbitalScope allows you to create custom animations such as animateTransformation on the OrbitalScope.
You can apply the animateTransformation animation to specific Composables and customize its AnimationSpec as seen the below:
val transformationSpec = SpringSpec<IntSize>(
dampingRatio = Spring.DampingRatioMediumBouncy,
stiffness = 200f
)
var isTransformed by rememberSaveable { mutableStateOf(false) }
val poster = rememberContentWithOrbitalScope {
GlideImage(
modifier = if (isTransformed) {
Modifier.size(300.dp, dp)
} {
Modifier.size(dp, dp)
}.animateTransformation(, transformationSpec),
imageModel = ItemUtils.urls[],
contentScale = ContentScale.Fit
)
}
Orbital(
modifier = Modifier
.clickable { isTransformed = !isTransformed }
) {
Column(
Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
poster()
}
}
Movement
The example below shows how to implement movement animation with the animateMovement extension of the OrbitalScope.
The rememberContentWithOrbitalScope allows you to create custom animations such as animateMovement on the OrbitalScope.
You can apply the animateMovement animation to specific Composables and customize its AnimationSpec as seen the below:
val movementSpec = SpringSpec<IntOffset>(
dampingRatio = Spring.DampingRatioMediumBouncy,
stiffness =
)
isTransformed rememberSaveable { mutableStateOf() }
poster = rememberContentWithOrbitalScope {
GlideImage(
modifier = (isTransformed) {
Modifier.size(dp, dp)
} {
Modifier.size(dp, dp)
}.animateMovement(, movementSpec),
imageModel = ItemUtils.urls[],
contentScale = ContentScale.Fit
)
}
Orbital(
modifier = Modifier
.clickable { isTransformed = !isTransformed }
) {
(isTransformed) {
Column(
Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
poster()
}
} {
Column(
Modifier
.fillMaxSize()
.padding(dp),
horizontalAlignment = Alignment.End,
verticalArrangement = Arrangement.Bottom
) {
poster()
}
}
}
Shared Element Transition
The example below shows how to implement shared element transition with the animateSharedElementTransition extension of the OrbitalScope.
The rememberContentWithOrbitalScope allows you to create custom animations such as animateSharedElementTransition on the OrbitalScope.
You can apply the animation to specific Composables and customize its .
Also, you can set the different s for the movement and transformation as seen the below:
Note: LookaheadLayout is a very experimental API, so measuring complex Composables might throw exceptions.
Shared Element Transition with Multiple Items
The example below shows how to implement shared element transition with multipe items. The basic concept of the usage is the same as the Shared Element Transition example.
var isTransformed by rememberSaveable { mutableStateOf(false) }
val items = rememberContentWithOrbitalScope {
ItemUtils.urls.forEach { url ->
GlideImage(
modifier = if (isTransformed) {
Modifier.size(140.dp, 180.dp)
} else {
Modifier.size(100.dp, 220.dp)
}
.animateSharedElementTransition(movementSpec, transformationSpec)
.padding(dp),
imageModel = url,
contentScale = ContentScale.Fit
)
}
}
Orbital(
modifier = Modifier
.fillMaxSize()
.clickable { isTransformed = !isTransformed },
isTransformed = isTransformed,
onStartContent = {
Column(
Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
items()
}
},
onTransformedContent = {
Row(
verticalAlignment = Alignment.CenterVertically
) { items() }
}
)
Shared Element Transition With LazyList
The provided code example illustrates the implementation of shared element transformation (container transform) with a lazy list, such as LazyColumn and LazyRow. The OrbitalScope function initiates a scope in which all layout scopes will be measured and pre-calculated for size and position across all child layouts.
You should bear in mind these three aspects:
Transition Between Composables
You can implement transitions between composable functions, as you've learned in the previous functions. The sample code below demonstrates how you can implement a shared element transition between screens A and B by defining shared content:
Unfortunately, you can't achieve this transition with Jetpack Compose Navigation yet.
Find this repository useful? :heart:
Support it by joining stargazers for this repository. :star:
Also, follow me on GitHub for my next creations! 🤩
License
Designed and developed by 2022 skydoves (Jaewoong Eum)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.