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

我应该如何进行实现呢?


当前回答

这里有一个简单的!

~卢诺克斯

MainActivity.java

package com.example.splashscreen;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

splashscreen.java

package com.example.splashscreen;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

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

        //Splash Screen duration
        int secondsDelayed = 1;
        new Handler().postDelayed(new Runnable() {
            public void run() {
                startActivity(new Intent(splashscreen.this, MainActivity.class));
                finish();
            }
        }, secondsDelayed * 3000);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

splashscreen.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/splashlogo"

    />

splashlogo.png

splashlogo.png

GitHub

SplashScreen

其他回答

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

当你打开任何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

进一步阅读:

应用发布时间和主题发布屏幕(Android Performance Patterns Season 6 Ep. 4) Android中的启动画面:正确的方法

旧的回答:

如何:简单的启动画面

这个答案会告诉你如何在应用启动时固定时间显示启动画面。例如,你可以选择显示3秒的启动画面。然而,如果你想要在可变的时间内(例如应用启动时间)显示飞溅屏幕,你应该看看Abdullah的答案https://stackoverflow.com/a/15832037/401025。但是要注意,应用程序在新设备上的启动速度可能非常快,所以用户只会看到一个flash,这是糟糕的用户体验。

首先,您需要在layout.xml文件中定义飞溅屏幕

  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical" android:layout_width="fill_parent"
          android:layout_height="fill_parent">

          <ImageView android:id="@+id/splashscreen" android:layout_width="wrap_content"
                  android:layout_height="fill_parent"
                  android:src="@drawable/splash"
                  android:layout_gravity="center"/>

          <TextView android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="Hello World, splash"/>

  </LinearLayout>

你的活动:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

public class Splash extends Activity {

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

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

        /* New Handler to start the Menu-Activity 
         * and close this Splash-Screen after some seconds.*/
        new Handler().postDelayed(new Runnable(){
            @Override
            public void run() {
                /* Create an Intent that will start the Menu-Activity. */
                Intent mainIntent = new Intent(Splash.this,Menu.class);
                Splash.this.startActivity(mainIntent);
                Splash.this.finish();
            }
        }, SPLASH_DISPLAY_LENGTH);
    }
}

这就是全部;)

上面的答案很好,但我想补充一些其他的东西。我是Android的新手,我在开发过程中遇到了这些问题。希望这能帮助到像我这样的人。

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> 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(); } } }

编码快乐!

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

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

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

希望这能有所帮助!

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 " /> < /意图过滤器> > < /活动