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

我应该如何进行实现呢?


当前回答

另一种方法是使用CountDownTimer实现

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

 new CountDownTimer(5000, 1000) { //5 seconds
      public void onTick(long millisUntilFinished) {
          mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
      }

     public void onFinish() {
          startActivity(new Intent(SplashActivity.this, MainActivity.class));
          finish();
     }

  }.start();
}

其他回答

在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: https://www.norio.be/android-feature-graphic-generator/ 选择你的标志和口号,选择你美丽的背景。将其大小调整为4096x4096。 现在下载图片并上传至: https://apetools.webprofusion.com/app/#/tools/imagegorilla 生成所有需要的启动画面,所有设备,所有平台。

享受吧!

这里有足够的答案,将有助于实施。这篇文章旨在帮助其他人完成创建启动画面的第一步!

进一步阅读:

应用发布时间和主题发布屏幕(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);
    }
}

这就是全部;)

它真的很简单,在安卓,我们只是使用处理器的概念来实现启动画面

在SplashScreenActivity java文件中粘贴此代码。

在SplashScreenActivity xml文件中使用imageview放置任何图片。

public void LoadScreen() {
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {                 
                Intent i = new Intent(SplashScreenActivity.this, AgilanbuGameOptionsActivity.class);
                startActivity(i);
            }
        }, 2000);
    }

在默认情况下,启动屏幕不会自动使你的应用程序看起来更专业。一个专业设计的启动画面有可能使你的应用程序看起来更专业,但如果你不知道如何编写一个启动画面,那么你的应用程序的其他部分又会有多专业呢?

设置启动画面的唯一原因(借口)是你要做大量的计算,或者等待GPS/WiFi启动,因为你的应用程序在启动之前依赖于这些。如果没有这些计算的结果或访问GPS/WiFi(等等),你的应用程序是死在水里的,因此你觉得你需要一个启动画面,并且必须阻止任何其他正在运行的程序(包括后台)的屏幕视图。

这样的启动画面应该看起来像你的全屏应用程序,给人一种已经初始化的印象,然后在漫长的计算完成后,最终的细节可以填充(图像调整)。情况果真如此,或者说这是设计该计划的唯一方法的可能性非常小。

最好允许用户(和操作系统的其他部分)在等待的时候做一些其他的事情,而不是把你的程序设计成依赖于一些需要一段时间的事情(当等待的持续时间是不确定的)。

你的手机上已经有图标显示GPS/WiFi正在启动。启动画面所占用的时间或空间可以用于加载预计算或实际进行计算。请参阅下面的第一个链接,了解您创建的问题以及必须考虑的内容。

如果你必须等待这些计算或GPS/WiFi,最好是简单地让应用程序启动,并有一个弹出窗口,告诉你需要等待计算(一个TEXTUAL的“初始化”消息是可以的)。GPS/WiFi的等待是预期的(如果它们没有在另一个程序中启用),所以宣布他们的等待时间是不必要的。

请记住,当启动屏幕启动时,你的程序实际上已经在运行,你所做的只是延迟程序的使用,并占用CPU/GPU来做一些大多数人认为没有必要的事情。

我们最好真的想要等待和看到你的启动屏幕,每次我们开始你的程序,否则我们不会觉得这是非常专业的编写。将启动画面设置为全屏,并复制实际程序的屏幕(所以我们认为它是初始化的,但实际上它并没有初始化)可能会实现你的目标(让你的程序看起来更专业),但我不敢打赌这一点。

为什么不去做呢:http://cyrilmottier.com/2012/05/03/splash-screens-are-evil-dont-use-them/

怎么做:https://encrypted.google.com/search?q=Android+splash+screen+source

所以有一个很好的理由不这样做,但如果你确定你的情况在某种程度上超出了这些例子,那么这样做的方法是上面给出的。一定要让你的申请看起来更专业,否则你就失去了这么做的唯一理由。

它就像一个YouTube频道,每个视频都以冗长的图形介绍(和outo)开始,或者觉得有必要讲一个笑话或解释过去一周发生的事情(当它不是喜剧或生活方式频道时)。秀一秀吧!(只需运行程序)。