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:26
public class MainActivity extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Thread t=new Thread()
    {

        public void run()
        {   

            try {

                sleep(2000);
                finish();
                Intent cv=new Intent(MainActivity.this,HomeScreen.class/*otherclass*/);
                startActivity(cv);
            } 

            catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };
    t.start();
}
查看更多
像晚风撩人
3楼-- · 2018-12-31 02:26

This is the best post I've seen on splash screens: http://saulmm.github.io/avoding-android-cold-starts

Saúl Molinero goes into two different options for splash screens: Taking advantage of the window background to animate into your initial screen and displaying placeholder UI (which is a popular choice that Google uses for most of their apps these days).

I refer to this post every time I need to consider cold start time and avoiding user dropoff due to long startup times.

Hope this helps!

查看更多
宁负流年不负卿
4楼-- · 2018-12-31 02:26

I used threads to make the Flash Screen in android.

    import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

public class HomeScreen extends AppCompatActivity{
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen_home);

        Thread thread = new Thread(){
            public void run(){
                try {
                    Thread.sleep(3 * 1000);
                    Intent i = new Intent(HomeScreen.this, MainActivity.class);
                    startActivity(i);
                } catch (InterruptedException e) {
                }
            }
        };
        thread.start();
    }
}
查看更多
爱死公子算了
5楼-- · 2018-12-31 02:28
  • Create an activity: Splash
  • Create a layout XML file: splash.xml
  • Put UI components in the splash.xml layout so it looks how you want
  • your Splash.java may look like this:

    public class Splash extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.splash);
    
            int secondsDelayed = 1;
            new Handler().postDelayed(new Runnable() {
                    public void run() {
                            startActivity(new Intent(Splash.this, ActivityB.class));
                            finish();
                    }
            }, secondsDelayed * 1000);
        }
    }
    
  • change ActivityB.class to whichever activity you want to start after the splash screen

  • check your manifest file and it should look like

        <activity
            android:name=".HomeScreen"
            android:label="@string/app_name">     
        </activity>

        <activity
            android:name=".Splash"
            android:label="@string/title_activity_splash_screen">     
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
查看更多
旧人旧事旧时光
6楼-- · 2018-12-31 02:28

Abdullah's answer is great. But i want to add some more details to it with my answer.

Implementing a 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.

background_splash.xml

<?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>

It just a layerlist with logo in center background color with it.

Now open styles.xml and add this style

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

This theme will have to actionbar and with background that we just created above.

And in manifest you need to set SplashTheme to activity that you want to use as splash.

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

Then inside your activity code navigate user to the specific screen after splash using intent.

public class SplashActivity extends AppCompatActivity {

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

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

That's the right way to do. I used these references for answer.

  1. https://material.google.com/patterns/launch-screens.html
  2. https://www.bignerdranch.com/blog/splash-screens-the-right-way/ Thanks to these guys for pushing me into right direction. I want to help others because accepted answer isn't a recommended to do splash screen.
查看更多
高级女魔头
7楼-- · 2018-12-31 02:28

How about a super-flexible launch screen that can use the same code and is defined in the AndroidManifest.xml, so the code will never need to change. I generally develop libraries of code and do not like customizing code because it is sloppy.

<activity
        android:name=".SplashActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <meta-data android:name="launch_class" android:value="com.mypackage.MyFirstActivity" />
        <meta-data android:name="duration" android:value="5000" />
</activity>

Then the SpashActivity itself looks up the meta-data for "launch_class" to then make the Intent itself. The meta data "duration" defines how long the splash screen stays up.

public class SplashActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_splash);

    ComponentName componentName = new ComponentName(this, this.getClass());

    try {
        Bundle bundle = null;
        bundle = getPackageManager().getActivityInfo(componentName, PackageManager.GET_META_DATA).metaData;
        String launch_class = bundle.getString("launch_class");
        //default of 2 seconds, otherwise defined in manifest
        int duration = bundle.getInt("duration", 2000);

        if(launch_class != null) {
            try {
                final Class<?> c = Class.forName(launch_class);

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Intent intent = new Intent(SplashActivity.this, c);
                        startActivity(intent);
                        finish();
                    }
                }, duration);

            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
  }
}
查看更多
登录 后发表回答