Translate News

Animated transitions in Android

When programming for android, you can already use a number of ready-made transition animations between screens. But what if the designer wants to use their transitions? Let's consider what are the possibilities to implement non-standard screen changes. 

Activities and fragments Activity

What tools the system offers for transitions between screens depends on whether transitions between Activity are used or everything happens within different fragments.

Application with multiple Activity

When using multiple Activities, you can use simple transition animations or create beautiful effects with a shared element.

Easy transition between Activity

You can either use the ready-made transitions that Android provides, or define your own, for example

You can either override the transitions for all Acitivty in the application theme

Or, you can override the transition within a separate Activity by calling

in the onCreate of the called Activity.

Shared Element Transition

The essence of a shared element transition is that a common element flows between two screens, creating the appearance of the integrity of two Activities between themselves.

Take View from one screen. Tell the system that the view with the same id is on the next one. Start the transition.

Android OS will do the rest for you.

Single activity application

The essence of a single activity application is in its name. Your application will consist of one activity, and the screens inside will be implemented as fragments. This approach provides flexibility and control over the transitions. Within the View, you can do absolutely anything.

You can control any View, apply any transition, etc. With Activity, this will not work, because listening or influencing the speed of working off closing and opening an activity directly will not work.

The architecture of transitions between fragments of one activity

It's easy to do the basic logic for transitions between screens. Defining the interface

It is not necessary to use Single from RxJava, any callback type can be set. The exit () method should return something that can be “listened to” for execution. The Fragment that needs to support "exit" will implement the interface above.

Next, you need to call exit () when switching between fragments. Wait for the Fragment to perform the required animation, then move on to a new Fragment, which can trigger the “start” animation.

If you are using, for example Cicerone, then you can override your AnimatedSupportAppNavigator

Everything is the same as in Cicerone, only now if the Fragment supports exit animation, it will play first.

Fragment opening and closing animation

Fragment exit animation

The Fragment itself, implementing exit (), plays the exit animation and transfers control further.

AdditiveAnimation is used for simplicity. It allows you to easily control the animation of various elements and listen to the end of the playback.

Fragment Entry Animation

If necessary, onViewCreated can trigger the "entry" animation.

This is just the base you need to understand how you can navigate between Activity and Fragment. More complex examples depend on the specific task and the whim of the designer. Having access to animation at the View level, you can make any imaginable and not very animation. For example, you can run a full-screen Lottie animation made in Adobe after effects.