19

I am currently working on an Android app and I am having some issues with my splash activity. I want my main activity to fade in from my splash activity, not from a black screen. Both my splash and main activities use the same background image so if I do a fade_in/fade_out combo, it looks weird as the background kind of fades a bit during the transition.

The idea here is I have to versions of my logo: normal and glowing. I want my main activity to fade in so my glowing logo over takes my normal logo causing a neat little "turn on" effect. The following code fades in from a black screen, which is not ideal.

/**
 * Pause Activity
 */
@Override
protected void onPause()
{
    super.onPause();
    finish();
    overridePendingTransition(android.R.anim.fade_in, 0);
}

EDIT:

Creating my own fade_out animation to retain the 1.0 alpha level (from 1.0 to 1.0) and adding android:fillAfter="true" to my splash animation set fixed the issue.

Taig
  • 6,718
  • 4
  • 44
  • 65
Karai17
  • 923
  • 2
  • 9
  • 26

2 Answers2

39

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" />
Lumis
  • 21,517
  • 8
  • 63
  • 67
  • Hrm... This has the same problem as my current code in which it goes to a black screen before fading in if I omit the fade_out or looks a bit wonky if I leave it in. I tried implementing my own fade_out which transitions from 1.0 alpha to 1.0 alpha but the original logo was still displaced before the new one faded in. The goal here is to "over write" the splash activity with the main activity so it looks like the logo starts to glow as the other views appear. – Karai17 Feb 05 '12 at 15:54
  • 2
    Glad to hear, I also added the xml for future visitors. So if in spalsh fade out we set android:fillAfter="true" and alpha from 1.0.to 1.0 we get the so called cross-fade effect. Good to know! – Lumis Feb 05 '12 at 18:00
  • 1
    The fade in/out animations are already defined in Android SDK. There are couple more animations which you can use as well. http://developer.android.com/reference/android/R.anim.html – Sufian Nov 14 '13 at 13:28
  • 1
    @Karai17 How did you fix the black screen before the fade-in starts? – android developer Aug 17 '15 at 08:16
34

I'd recommend against a classic crossfade, but rather show the new Activity without an animation and just fade out the current Activity. This looks & feels much cleaner and resolves some minor issues where you can see the launcher/underlying app when you open the app from the background while the animation is starting.

my_splash_fade_out.xml

<?xml version="1.0" encoding="utf-8"?>

<alpha
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="750"
    android:fromAlpha="1.0"
    android:interpolator="@android:interpolator/accelerate_cubic"
    android:startOffset="250"
    android:toAlpha="0.0"
    android:zAdjustment="top" />

I'm adding a startOffset here to give the newly created Activity a bit off a head start, as it is rather heavy.

MySplashActivity.java

...
startActivity( ... );
finish();
overridePendingTransition( 0, R.anim.screen_splash_fade_out );

Preview

Preview animation

Taig
  • 6,718
  • 4
  • 44
  • 65