我想让我的应用看起来更专业,所以我决定添加一个启动画面。

我应该如何进行实现呢?


当前回答

启动画面不应该从布局文件加载,加载时可能仍然有一些延迟。

最好的方法是为你的SplashScreenActivity创建一个主题,并设置android:windowBackground为一个可绘制的资源。

https://www.bignerdranch.com/blog/splash-screens-the-right-way/

简而言之:

在manifest中声明你的SplashScreenActivity:

<activity
        android:name=".activities.SplashScreenActivity"
        android:theme="@style/SplashTheme"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

在SplashScreenActivity.java中:

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

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

}

接下来创建主题背景窗口的资源:

<style name="SplashTheme" parent="Theme.Bumpfie.Base">
    <item name="android:windowBackground">@drawable/splash</item>
</style>

可绘制文件splash.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/white"/>
    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/app_logo"/>
    </item>
</layer-list>

其他回答

你可以在你的onCreate方法中添加这个

new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    // going to next activity
                    Intent i=new Intent(SplashScreenActivity.this,MainActivity.class);
                    startActivity(i);
                    finish();
                }
            },time);

初始化你的时间值在毫秒,因为你想…

private  static int time=5000;

欲了解更多详细信息,请从此链接下载完整代码…

https://github.com/Mr-Perfectt/Splash-Screen

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

<活动 android: name = "。屏” android: label = " @string / app_name " > > < /活动 <活动 android: name = "。飞溅” android: label = " @string / title_activity_splash_screen " > <意图过滤器> <action android:name="android.intent.action. main " /> <category android:name="android.intent.category. launcher " /> < /意图过滤器> > < /活动

注意这个解决方案不会让用户等待更长的时间:启动画面的延迟取决于应用程序的启动时间。

当你打开任何android应用程序时,默认情况下你会看到一个黑屏,上面是应用程序的标题和图标,你可以通过使用样式/主题来改变这种情况。

首先,在values文件夹中创建一个style.xml,并向其添加一个样式。

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

而不是使用@android:style/Theme.DeviceDefault.Light。NoActionBar你可以使用任何其他主题作为父主题。

其次,在你的应用Manifest.xml中添加android:theme="@style/splashScreenTheme"到你的主活动。

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

第三,在onCreate()启动活动中更新主题。

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

更新 看看这篇文章。

感谢@mat1h和@adelriosantiago

启动画面不应该从布局文件加载,加载时可能仍然有一些延迟。

最好的方法是为你的SplashScreenActivity创建一个主题,并设置android:windowBackground为一个可绘制的资源。

https://www.bignerdranch.com/blog/splash-screens-the-right-way/

简而言之:

在manifest中声明你的SplashScreenActivity:

<activity
        android:name=".activities.SplashScreenActivity"
        android:theme="@style/SplashTheme"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

在SplashScreenActivity.java中:

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

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

}

接下来创建主题背景窗口的资源:

<style name="SplashTheme" parent="Theme.Bumpfie.Base">
    <item name="android:windowBackground">@drawable/splash</item>
</style>

可绘制文件splash.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/white"/>
    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/app_logo"/>
    </item>
</layer-list>

这是我所见过的关于启动画面的最好的帖子:http://saulmm.github.io/avoding-android-cold-starts

Saúl Molinero介绍了两种不同的启动画面选项:利用窗口背景动画进入初始屏幕和显示占位符UI(这是谷歌目前在大多数应用程序中使用的流行选择)。

每当我需要考虑冷启动时间和避免由于长启动时间而导致用户流失时,我都会参考这篇文章。

希望这能有所帮助!