Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fade in Activity from previous Activity in Android

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.

like image 852
Karai17 Avatar asked Feb 05 '12 14:02

Karai17


2 Answers

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" />
like image 85
Lumis Avatar answered Oct 17 '22 19:10

Lumis


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

like image 25
Taig Avatar answered Oct 17 '22 21:10

Taig