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

我应该如何进行实现呢?


当前回答

在Android中,启动画面是一个有点不可用的对象:为了隐藏主活动启动的延迟,它不能尽快加载。使用它有两个原因:广告和网络运营。

实现为对话框使跳跃没有延迟从启动画面到主UI的活动。

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

    }
}

布局:

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

并开始:

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

    ...
}

其他回答

阿卜杜拉的回答很好。但我想在我的回答中补充一些细节。

实现启动画面

正确执行闪屏的方法与你想象的略有不同。您所看到的splash视图必须立即准备好,甚至在您可以在splash活动中膨胀布局文件之前。

所以你不会使用布局文件。相反,指定启动画面的背景作为活动的主题背景。为此,首先在res/drawable中创建一个XML drawable。

background_splash.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Background Color -->
    <item
        android:drawable="@color/gray"/>

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

</layer-list>

它只是一个层列表与标志在中心背景色与它。

现在打开styles.xml并添加这个样式

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

这个主题将没有动作栏和我们刚才创建的背景。

在manifest中,你需要将SplashTheme设置为你想要用作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>

然后在你的活动代码中,使用意图引导用户进入特定的屏幕。

public class SplashActivity extends AppCompatActivity {

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

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

这才是正确的做法。我用这些参考来回答。

https://material.google.com/patterns/launch-screens.html https://www.bignerdranch.com/blog/splash-screens-the-right-way/ 感谢这些人把我推向正确的方向。我想要帮助其他人,因为公认的答案并不是做启动画面的建议。

一种方法是通过创建FullScreenActivity/EmptyActivity(例如SplashScreenActivity),并将其设置为打开应用程序时显示的第一个活动。在AndroidManifest.xml中添加以下内容到您的活动

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

然后,您可以设置一个处理程序,在几秒钟后取消此活动。

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

其次,如果你不想创建一个单独的活动,你可以在MainActivity上膨胀一个布局,并将布局可见性设置为GONE,或者在几毫秒后在现有的启动画面布局上膨胀主布局。

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();
}
public class SplashActivity extends Activity {

  Context ctx;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      ctx = this;
      setContentView(R.layout.activity_splash);

      Thread thread = new Thread(){
          public void run(){
              try {
                  sleep(3000);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }

              Intent in = new Intent(ctx,MainActivity.class);
              startActivity(in);
              finish();
          }
      };
      thread.start();
  }
}

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