How to Perform a Fade Animation on Activity Transition

How to perform a fade animation on Activity transition?

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.

Reopen Activity with Fade In and Fade Out Animation in Android

Try this After startActivity(); call overridePendingTransition(R.anim.fade_in,R.anim.fade_out);

How to fade out an Activity's UI, and fade in another Activity's UI?

To fade out current activity:

Create a style in res/values:

<style name="PageTransition.Activity" parent="@android:style/Animation.Activity">
<item name="android:activityOpenEnterAnimation">@android:anim/fade_in</item>
<item name="android:activityOpenExitAnimation">@android:anim/fade_out</item>
<item name="android:activityCloseEnterAnimation">@android:anim/fade_out</item>
<item name="android:activityCloseExitAnimation">@android:anim/fade_in</item>
</style>

Then in your activiy's style which is usually Apptheme for MainActivity:
add this line :

 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/one</item>
<item name="colorPrimaryDark">@color/one</item>
<item name="colorAccent">@color/colorAccent</item>
        name="android:windowAnimationStyle">@style/PageTransition.Activity</item>
    </style>

To start next activity's UI delayed:
For your case, you'll need to use handler to delay transitions:

 private final int interval = 1000; // 1 Second
private Handler handler = new Handler();
private Runnable runnable = new Runnable() {
public void run() {

ValueAnimator animator1 = ValueAnimator.ofInt(10, 0);

animator1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {

float x = (Integer) valueAnimator.getAnimatedValue() / (float) 10;
yourview.setAlpha(x);
}
});

animator1.setDuration(500);
animator1.start();
}

Call this oncreate:

handler.postAtTime(runnable, System.currentTimeMillis() + interval);
handler.postDelayed(runnable, 2000); //two seconds

Obviously, you wanna set your initial alphas to 0.
Hope this helped!

EDIT:

Define your own fadein-fadeout anims if your want to customize duration...

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:interpolator/decelerate_quad"
android:fromAlpha="startalpha" android:toAlpha="endalpha"
android:duration="{YourDuration in ms}"/>

If the previous way fails for some APIs, use this before starting your activity:

...
overridePendingTransition(int enterAnim, int exitAnim);
startActivity(intent);
....

Fade in Activity from previous Activity in Android

It is about the order of things. Here is an example which fades into next activity after 3 seconds:

new Handler().postDelayed(new Runnable() {
@Override
public void run() {

//Create an intent that will start the main activity.
Intent mainIntent = new Intent(SplashActivity.this, MainMenuActivity.class);
SplashActivity.this.startActivity(mainIntent);

//Finish splash activity so user cant go back to it.
SplashActivity.this.finish();

//Apply splash exit (fade out) and main entry (fade in) animation transitions.
overridePendingTransition(R.anim.mainfadein, R.anim.splashfadeout);
}
}, 3000);

Note that here there a two animations fade in and fade out.

mainfadein.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="700" />

splashfadeout.xml

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

How to animate fade out only on activity

Yes, it's simple, the "id" for no animation is 0. So you can do the following:

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

Activity transition in Android

You can do this with Activity.overridePendingTransition(). You can define simple transition animations in an XML resource file.



Related Topics



Leave a reply



Submit