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:01

The Stopping on the Splash screen for 4's 5's unnecessarily doesn't make much sense. It's Ok if you loading something in background else follow this approach to implement splash screen:- Implementing a splash screen the right way is a little different than you might imagine. The splash view that you see has to be ready immediately, even before you can inflate a layout file in your splash activity.

So you will not use a layout file. Instead, specify your splash screen’s background as the activity’s theme background. To do this, first, create an XML drawable in res/drawable.

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

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

    <item>
        <bitmap
            android:gravity="center"
            android:src="@mipmap/ic_launcher"/>
    </item>

</layer-list>

Here, I’ve set up a background color and an image.

Next, you will set this as your splash activity’s background in the theme. Navigate to your styles.xml file and add a new theme for your splash activity:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    </style>

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

</resources>

In your new SplashTheme, set the window background attribute to your XML drawable. Configure this as your splash activity’s theme in your AndroidManifest.xml:

<activity
    android:name=".SplashActivity"
    android:theme="@style/SplashTheme">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

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

Finally, SplashActivity class should just forward you along to your main activity:

     public class SplashActivity extends AppCompatActivity {

             @Override
             protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);

               Intent intent = new Intent(this, MainActivity.class);
               startActivity(intent);
               finish();
    }
}

More Details read this: 1.https://www.bignerdranch.com/blog/splash-screens-the-right-way/ 2.http://blog.goodbarber.com/3-tips-to-create-a-great-splash-screen-for-your-mobile-app_a287.html

查看更多
何处买醉
3楼-- · 2018-12-31 02:01

Simple Code, it works:) Simple splash

int secondsDelayed = 1;
    new Handler().postDelayed(new Runnable() {
        public void run() {
            startActivity(new Intent(LoginSuccessFull.this, LoginActivity.class));
            finish();
        }
    }, secondsDelayed * 1500);
查看更多
冷夜・残月
4楼-- · 2018-12-31 02:02

@Abdullah's answer is correct, however Google has posted an extended explanation on how to properly implement this without altering your activity's theme:

https://plus.google.com/+AndroidDevelopers/posts/Z1Wwainpjhd

Apps like Google Maps and YouTube have started using the same method.

查看更多
琉璃瓶的回忆
5楼-- · 2018-12-31 02:02

The answers above is very good, but I would like to add something else. I am new to Android, I met these problem during my development. hope this can help someone like me.

  1. The Splash screen is the entry point of my app, so add the following lines in AndroidManifest.xml.

        <activity
            android:name=".SplashActivity"
            android:theme="@android:style/Theme.DeviceDefault.Light.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    
  2. The splash screen should only show once in the app life cycle, I use a boolean variable to record the state of the splash screen, and only show it on the first time.

    public class SplashActivity extends Activity {
        private static boolean splashLoaded = false;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            if (!splashLoaded) {
                setContentView(R.layout.activity_splash);
                int secondsDelayed = 1;
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        startActivity(new Intent(SplashActivity.this, MainActivity.class));
                        finish();
                    }
                }, secondsDelayed * 500);
    
                splashLoaded = true;
            }
            else {
                Intent goToMainActivity = new Intent(SplashActivity.this, MainActivity.class);
                goToMainActivity.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(goToMainActivity);
                finish();
            }
        }
    }
    

happy coding!

查看更多
其实,你不懂
6楼-- · 2018-12-31 02:02

Above all answers are really very good. But there are encounter problem of memory leakage. This issue is often known in the Android community as "Leaking an Activity". Now what exactly does that mean?

When configuration change occurs, such as orientation change, Android destroys the Activity and recreates it. Normally, the Garbage Collector will just clear the allocated memory of the old Activity instance and we're all good.

"Leaking an Activity" refers to the situation where the Garbage Collector cannot clear the allocated memory of the old Activity instance since it's being (strong) referenced from an object that out lived the Activity instance. Every Android app has a specific amount of memory allocated for it. When Garbage Collector cannot free up unused memory, the app's performance will decrease gradually and eventually crash with OutOfMemory error.

How to determine whether the app leaks memory or not? The fastest way is to open the Memory tab in Android Studio and pay attention to allocated memory as you change the orientation. If the allocated memory keeps on increasing and never decreases then you have a memory leak.

1.Memory leak when user change the orientation. enter image description here

First you need to define the splash screen in your layout resource splashscreen.xml file

Sample Code for splash screen activity.

public class Splash extends Activity {
 // 1. Create a static nested class that extends Runnable to start the main Activity
    private static class StartMainActivityRunnable implements Runnable {
        // 2. Make sure we keep the source Activity as a WeakReference (more on that later)
        private WeakReference mActivity;

        private StartMainActivityRunnable(Activity activity) {
         mActivity = new WeakReference(activity);
        }

        @Override
        public void run() {
         // 3. Check that the reference is valid and execute the code
            if (mActivity.get() != null) {
             Activity activity = mActivity.get();
             Intent mainIntent = new Intent(activity, MainActivity.class);
             activity.startActivity(mainIntent);
             activity.finish();
            }
        }
    }

    /** Duration of wait **/
    private final int SPLASH_DISPLAY_LENGTH = 1000;

    // 4. Declare the Handler as a member variable
    private Handler mHandler = new Handler();

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

        // 5. Pass a new instance of StartMainActivityRunnable with reference to 'this'.
        mHandler.postDelayed(new StartMainActivityRunnable(this), SPLASH_DISPLAY_LENGTH);
    }

    // 6. Override onDestroy()
    @Override
    public void onDestroy() {
     // 7. Remove any delayed Runnable(s) and prevent them from executing.
     mHandler.removeCallbacksAndMessages(null);

     // 8. Eagerly clear mHandler allocated memory
     mHandler = null;
    }
}

For more information please go through this link

查看更多
骚的不知所云
7楼-- · 2018-12-31 02:03

Note this solution will not let the user wait more: the delay of the splash screen depends on the start up time of the application.

When you open any android app you will get by default a some what black screen with the title and icon of the app on top, you can change that by using a style/theme.

First, create a style.xml in values folder and add a style to it.

<style name="splashScreenTheme" parent="@android:style/Theme.DeviceDefault.Light.NoActionBar">
    <item name="android:windowBackground">@drawable/splash_screen</item>
</style>

Instead of using @android:style/Theme.DeviceDefault.Light.NoActionBar you can use any other theme as a parent.

Second, in your app Manifest.xml add android:theme="@style/splashScreenTheme" to your main activity.

<activity
        android:name="MainActivity"
        android:label="@string/app_name"
        android:theme="@style/splashScreenTheme" >

Third, Update your theme in your onCreate() launch activity.

protected void onCreate(Bundle savedInstanceState) {
    // Make sure this is before calling super.onCreate
    setTheme(R.style.mainAppTheme);
    super.onCreate(savedInstanceState);
}

UPDATE Check out this post https://plus.google.com/+AndroidDevelopers/posts/Z1Wwainpjhd Thanks to @mat1h and @adelriosantiago

查看更多
登录 后发表回答