How to implement splash screen in android

前端 未结 2 2009
礼貌的吻别
礼貌的吻别 2020-12-07 23:49

I have a application which shows list of items. i need to display a fadeout image before start of main activity. i tried something like that in onCreate method of main activ

相关标签:
2条回答
  • 2020-12-08 00:05

    Basically, what you're looking for is a Splash screen which shows your image and then fades out. A main activity's screen then fades in. So what you could do is create an activity for the Splash screen and then another one for the main activity you might want to call. This would be your Splash Screen activity.

    public class SplashScreen extends Activity {
    private static final int SPLASH_DISPLAY_TIME = 4000; // splash screen delay time
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
    
        new Handler().postDelayed(new Runnable() {
            public void run() {
    
                Intent intent = new Intent();
                intent.setClass(Splash.this, NextActivity.class);
    
                Splash.this.startActivity(intent);
                Splash.this.finish();
    
                // transition from splash to main menu
                overridePendingTransition(R.animate.activityfadein,
                        R.animate.splashfadeout);
    
            }
        }, SPLASH_DISPLAY_TIME);
    }
    

    NextActivity is any activity that you want to fade in and take its place. For the animations you would need to create the two xml files in a folder called animate in your resources. Here is the splashfadeout.xml file

    <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="4000" />
    

    This would be the activityfadein.xml file

    <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="2000" />
    

    These files basically make your activities fade in and out

    0 讨论(0)
  • 2020-12-08 00:07

    A "Splash Screen" like the one @Manish Burman suggests is not a real one. It's really just a "full screen comercial", which no user wants to spend his time looking at. It has no purpose except being there, meaning it doesn't mask long loading times or something in that region, like a real splash screen should.

    If you are to show a splash screen, it should be because you have an initial activity that takes quite some time to load - then a splash screen can be justified, to show the user that stuff is loading or to convince him that the app has not "died".

    Check out this guide on how to create a real splash screen.

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