Metaphor
💠Android Material's motion system animations.
Who's using Metaphor?
👉 Check out who's using Metaphor
Include in your project
Gradle
Add the dependency below to your module's build.gradle file:
dependencies {
implementation("io.github.androidpoet:metaphor:2.0.0")
}
Metaphor provides support for all four motion patterns
defined in the Material spec, plus elevation scale and hold.
- Container transform
- Shared axis
- Fade through
- Fade
How it works
Metaphor separates what animates from where it animates.
- A
Motion is one Material pattern as plain data: Motion.Fade(), Motion.SharedAxis.x(),
Motion.ContainerTransform() and so on. Each carries only its own parameters and defaults
to the duration the Material spec recommends.
- A
MotionSpec is the set of motions for one screen: enter, exit, popEnter, popExit
and an optional sharedElement. It holds no reference to a Fragment or Activity, so it can
be declared once and reused anywhere.
applyMotion(...) is the only thing a target needs to call. It exists for Fragment,
Activity and PopupWindow. Views use morphInto, animateVisibility and animateChanges.
object Motions {
val listOrigin = motionSpec {
exit = Motion.ElevationScale.shrink()
popEnter = Motion.ElevationScale.grow()
}
val detail = motionSpec {
sharedElement = Motion.ContainerTransform()
}
}
Container transform
How to use in Fragments
The origin screen holds a list. The destination screen grows out of the tapped item and shrinks
back into it on return.
// Origin fragment (the list)
class ArtistListFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
applyMotion {
exit = Motion.ElevationScale.shrink() // list fades back while the detail grows
popEnter = Motion.ElevationScale.grow() // list comes forward again on return
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// Wait for the list to lay out so the return transform can find the item view.
postponeEnterUntilDrawn()
}
private fun openDetail(itemView: View, item: Artist) {
val extras = FragmentNavigatorExtras(itemView to item.id)
findNavController().navigate(ArtistListFragmentDirections.toDetail(item), extras)
}
}
// Destination fragment (the detail)
class ArtistDetailFragment : Fragment() {
private val args: ArtistDetailFragmentArgs by navArgs()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
applyMotion(
spec = motionSpec {
sharedElement = Motion.ContainerTransform(
duration = 300,
scrimColor = Color.TRANSPARENT,
containerColor = Color.WHITE,
path = MotionPath.Arc,
)
},
sharedElement = SharedElement.Destination(view, args.item.id),
)
}
}
Motion.ContainerTransform takes scrimColor, containerColor, path (MotionPath.Arc or
MotionPath.Linear), fadeMode (In, Out, Cross, Through) and duration.
How to use in Views
Morph one view into a sibling. The first view is hidden and the target is shown when the
transition starts. Both views must share the same parent.
viewBinding.fabDetail.setOnClickListener {
viewBinding.fabDetail.morphInto(viewBinding.controls, Motion.ContainerTransform(duration = 300))
}
viewBinding.controls.setOnClickListener {
viewBinding.controls.morphInto(viewBinding.fabDetail)
}
How to use in Activities
// Origin activity
class ArtistListActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
applyMotion(SharedElement.Origin) {
sharedElement = Motion.ContainerTransform()
exit = Motion.Hold()
}
}
private fun openDetail(card: View) {
card.transitionName = "card"
val options = ActivityOptions.makeSceneTransitionAnimation(this, card, "card")
startActivity(Intent(this, ArtistDetailActivity::class.java), options.toBundle())
}
}
// Destination activity
class ArtistDetailActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
applyMotion(SharedElement.Destination(binding.root, "card")) {
sharedElement = Motion.ContainerTransform()
}
}
}
Shared axis
How to use in Fragments
Both screens set the same axis. forward = true plays when moving deeper, forward = false
plays when popping back.
// Origin fragment
class SettingsFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
applyMotion {
exit = Motion.SharedAxis.x(forward = true)
popEnter = Motion.SharedAxis.x(forward = false)
}
}
}
// Destination fragment
class AccountFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
applyMotion {
enter = Motion.SharedAxis.x(forward = true)
popExit = Motion.SharedAxis.x(forward = false)
}
}
}
Motion.SharedAxis.y() and Motion.SharedAxis.z() work the same way, or build one directly:
Motion.SharedAxis(axis = Motion.SharedAxis.Axis.Z, forward = true, duration = 400)
How to use in Views
viewBinding.sharedX.setOnClickListener {
viewBinding.img.toggleVisibility(Motion.SharedAxis.x(forward = true, duration = 1000))
}
viewBinding.sharedY.setOnClickListener {
viewBinding.img.toggleVisibility(Motion.SharedAxis.y(forward = true))
}
viewBinding.sharedZ.setOnClickListener {
viewBinding.img.toggleVisibility(Motion.SharedAxis.z(forward = true))
}
How to use in Activities
// Origin activity, in onCreate
applyMotion {
exit = Motion.SharedAxis.x(forward = true)
popEnter = Motion.SharedAxis.x(forward = false)
}
// Destination activity, in onCreate
applyMotion {
enter = Motion.SharedAxis.x(forward = true)
popExit = Motion.SharedAxis.x(forward = false)
}
Fade through
How to use in Fragments
Fade through suits screens that are not related, such as bottom navigation tabs.
// Origin fragment
class HomeFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
applyMotion {
enter = Motion.FadeThrough()
exit = Motion.FadeThrough()
}
}
}
// Destination fragment
class DashboardFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
applyMotion {
enter = Motion.FadeThrough(duration = 300)
exit = Motion.FadeThrough(duration = 300)
}
}
}
How to use in Views