How to perform a fade animation on Activity transition?

android, android-intent, animation, pending-transition

Solution

You could create your own .xml animation files to fade in a new `Activity` and fade out the current `Activity`:

fade_in.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
           android:interpolator="@android:anim/accelerate_interpolator"
           android:fromAlpha="0.0" android:toAlpha="1.0"
           android:duration="500" />

fade_out.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
           android:interpolator="@android:anim/accelerate_interpolator"
           android:fromAlpha="1.0" android:toAlpha="0.0"
           android:fillAfter="true"
           android:duration="500" />

Use it in code like that: (Inside your `Activity`)

Intent i = new Intent(this, NewlyStartedActivity.class);
startActivity(i);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);

The above code will fade out the currently active `Activity` and fade in the newly started `Activity` resulting in a smooth transition.

UPDATE: @Dan J pointed out that using the built in Android animations improves performance, which I indeed found to be the case after doing some testing. If you prefer working with the built in animations, use:

overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);

Notice me referencing `android.R` instead of `R` to access the resource id.

UPDATE: It is now common practice to perform transitions using the Transition class introduced in API level 19.

Problem

I am codifiying a transition effect between my logo activity and my Main activity, but I have the problem that before vanish the activity move to top: ``` <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false" > <alpha android:duration="2000" android:fromAlpha="0.0" android:toAlpha="1.0" > </alpha> </set> ``` How could I improve this code to get only a vanish effect?

Original source

Related problems