Basics
There is very little setup needed to start using Litho Transition.
To show that, let's imagine that we have a simple Component that renders a yellow square, and aligns it to either right or left edge of screen based on value of the @State boolean toRight.
However, when the value of the state changes, we re-render the ComponentTree which makes the square simply “jump” from its previous position to the new one. If such transition between states isn't what you want and you wish it would rather “flow” from one place to the other, we have good news: there are just a couple of things you need to add to your code to make it happen.
Here are the key elements you'll need to work with:
@OnCreateTransitionmethod. You need to add a method annotated with@OnCreateTransitionto your Spec, which is what we use to define the transition animations. It should return aTransition, and its first argument should always be ofComponentContexttype. As other lifecycle methods in a Spec, it could also have@Proparguments, as well as arguments ofStateValuetype, although this comes at a cost - more on this later.Transitionis a description of which Component/Property (mandatory) and how (optional) you want to animate. You will not use a constructor to createTransitioninstances, instead you will use one of the providedBuilders.transitionKeyis an identifier that you normally assign to aComponentthat you want to animate, and then use it when definingTransition.AnimatedPropertiesare used to target the property of aComponentthat should be animated when its value changes.
To put it all together, here is what it would look like in our case:
Notice that we:
- On line 12 we assign a
transitionKeyto theRectcomponent usingComponent.Builder#transitionKey()method. - On lines 19-20 we create a
TransitionusingTransition.create()that takes atransitionKeyand then specify the property of the component using.animate()method that takes anAnimatedProperty.
Both of these methods take variable number of arguments, so the description of the multiple Transitions may nicely be collapsed and it may look like this:
Key Features
- Interruptible: Animations can be interrupted and driven to a new ending value automatically.
- Declarative: The framework handles and drives the animations for you, meaning you get 60fps, interruptible animations without extra work.
- Animated Properties: Supports animating
X,Y,WIDTH,HEIGHT,SCALE,ALPHA, andROTATION.