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

我应该如何进行实现呢?


当前回答

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

其他回答

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

一种方法是通过创建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,或者在几毫秒后在现有的启动画面布局上膨胀主布局。

另一种方法是使用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();
}

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>

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

设置启动画面的唯一原因(借口)是你要做大量的计算,或者等待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)开始,或者觉得有必要讲一个笑话或解释过去一周发生的事情(当它不是喜剧或生活方式频道时)。秀一秀吧!(只需运行程序)。