Fade in Activity from previous Activity in Android

后端 未结 2 1451
生来不讨喜
生来不讨喜 2020-12-29 07:01

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 sc

相关标签:
2条回答
  • 2020-12-29 07:29

    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" />
    
    0 讨论(0)
  • 2020-12-29 07:36

    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

    0 讨论(0)
提交回复
热议问题