How do I make a splash screen?

2018-12-31 01:50发布

I wanted to make my app look more professional, so I decided that I wanted to make a splash screen.

How would I create it and then implement it?

30条回答
浅入江南
2楼-- · 2018-12-31 02:20

This is the full code here

SplashActivity.java

public class SplashActivity extends AppCompatActivity {

private final int SPLASH_DISPLAY_DURATION = 1000;

@Override
public void onCreate(Bundle bundle) {
    super.onCreate(bundle);


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

            Intent mainIntent = new Intent(SplashActivity.this,MainActivity.class);
            SplashActivity.this.startActivity(mainIntent);
            SplashActivity.this.finish();
        }
    }, SPLASH_DISPLAY_DURATION);
}}

In drawables create this bg_splash.xml

<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:drawable="@color/app_color"/>

<item>
    <bitmap
        android:gravity="center"
        android:src="@drawable/ic_in_app_logo_big"/>
</item></layer-list>

In styles.xml create a custom theme

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowBackground">@drawable/bg_splash</item>
</style>

and finally in AndroidManifest.xml specify the theme to your activity

<activity
        android:name=".activities.SplashActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/SplashTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Cheers.

查看更多
大哥的爱人
3楼-- · 2018-12-31 02:21
  1. Create an Activity SplashScreen.java

    public class SplashScreen extends Activity {
    protected boolean _active = true;
    protected int _splashTime = 3000; // time to display the splash screen in ms
    
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splashscreen);
    
        Thread splashTread = new Thread() {
            @Override
            public void run() {
                try {
                    int waited = 0;
                    while (_active && (waited < _splashTime)) {
                        sleep(100);
                        if (_active) {
                            waited += 100;
                        }
                    }
                } catch (Exception e) {
    
                } finally {
    
                    startActivity(new Intent(SplashScreen.this,
                            MainActivity.class));
                    finish();
                }
            };
                 };
        splashTread.start();
    }
     }
    
  2. splashscreen.xml will be like this

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="600px" android:layout_height="1024px"
      android:background="#FF0000">
    </RelativeLayout> 
    
查看更多
大哥的爱人
4楼-- · 2018-12-31 02:21

Another approach is achieved by using CountDownTimer

@Override
public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.splashscreen);

 new CountDownTimer(5000, 1000) { //5 seconds
      public void onTick(long millisUntilFinished) {
          mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
      }

     public void onFinish() {
          startActivity(new Intent(SplashActivity.this, MainActivity.class));
          finish();
     }

  }.start();
}
查看更多
余欢
5楼-- · 2018-12-31 02:22

Splash screen is a little unusable object in Android: it can not be loaded as soon as possible for hiding the delay of main activity starting. There are two reasons to use it: advertising and network operations.

Implementation as dialog makes jump without delay from splash screen to main UI of activity.

public class SplashDialog extends Dialog {
    ImageView splashscreen;
    SplashLoader loader;
    int splashTime = 4000;

    public SplashDialog(Context context, int theme) {
        super(context, theme);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        setCancelable(false);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                cancel();
            }
        }, splashTime);

    }
}

Layout:

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white">

    <ImageView
        android:id="@+id/splashscreen"
        android:layout_width="190dp"
        android:layout_height="190dp"
        android:background="@drawable/whistle"
        android:layout_centerInParent="true" />

</RelativeLayout>

And start:

public class MyActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getIntent().getCategories() != null &&  getIntent().getCategories().contains("android.intent.category.LAUNCHER")) {
            showSplashScreen();
        }
    }

    protected Dialog splashDialog;
    protected void showSplashScreen() {
        splashDialog = new SplashDialog(this, R.style.SplashScreen);
        splashDialog.show();
    }

    ...
}
查看更多
人间绝色
6楼-- · 2018-12-31 02:24

Create a Activity, let us Activity named 'A', then create a xml file called myscreen.xml, in that set a the splash screen image as background, and then use count down timer to navigate from one Activtity to another. To know how to use Count Down timer see my answer in this question TimerTask in Android?

查看更多
千与千寻千般痛.
7楼-- · 2018-12-31 02:25

In my case I didn't want to create a new Activity only to show a image for 2 seconds. When starting my MainAvtivity, images gets loaded into holders using picasso, I know that this takes about 1 second to load so I decided to do the following inside my MainActivity OnCreate:

splashImage = (ImageView) findViewById(R.id.spllll);

    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

    int secondsDelayed = 1;
    new Handler().postDelayed(new Runnable() {
        public void run() {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
            splashImage.setVisibility(View.GONE);

        }
    }, secondsDelayed * 2000);

When starting the application the first thing that happens is the ImageView gets displayed and the statusBar is removed by setting the window flags to full screen. Then I used a Handler to run for 2 seconds, after the 2 seconds I clear the full screen flags and set the visibility of the ImageView to GONE. Easy, simple, effective.

查看更多
登录 后发表回答