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

我应该如何进行实现呢?


当前回答

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

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

其他回答

创建一个活动,让我们活动命名为“a”,然后创建一个名为mysscreen .xml的xml文件,在其中设置一个启动画面图像作为背景,然后使用倒计时计时器从一个活动导航到另一个活动。要知道如何使用倒计时定时器看到我的答案在这个问题的TimerTask在Android?

进一步阅读:

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

这就是全部;)

简单的代码,它的工作:)简单的飞溅

int secondsDelayed = 1;
    new Handler().postDelayed(new Runnable() {
        public void run() {
            startActivity(new Intent(LoginSuccessFull.this, LoginActivity.class));
            finish();
        }
    }, secondsDelayed * 1500);

一个超级灵活的启动屏幕如何,可以使用相同的代码,并在AndroidManifest.xml中定义,因此代码永远不需要更改。我通常开发代码库,不喜欢定制代码,因为它很草率。

<activity
        android:name=".SplashActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <meta-data android:name="launch_class" android:value="com.mypackage.MyFirstActivity" />
        <meta-data android:name="duration" android:value="5000" />
</activity>

然后SpashActivity本身查找“launch_class”的元数据,然后创建Intent本身。元数据“持续时间”定义了启动画面持续的时间。

public class SplashActivity extends Activity {

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

    ComponentName componentName = new ComponentName(this, this.getClass());

    try {
        Bundle bundle = null;
        bundle = getPackageManager().getActivityInfo(componentName, PackageManager.GET_META_DATA).metaData;
        String launch_class = bundle.getString("launch_class");
        //default of 2 seconds, otherwise defined in manifest
        int duration = bundle.getInt("duration", 2000);

        if(launch_class != null) {
            try {
                final Class<?> c = Class.forName(launch_class);

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Intent intent = new Intent(SplashActivity.this, c);
                        startActivity(intent);
                        finish();
                    }
                }, duration);

            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
  }
}
     - Add in SplashActivity 

   public class SplashActivity extends Activity {

       private ProgressBar progressBar;
       int i=0;
       Context context;
       private GoogleApiClient googleApiClient;

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

           new Handler().postDelayed(new Runnable() {
               @Override
               public void run() {
                   startActivity(new Intent(Splash.this, LoginActivity.class));
                   finish();
               }
           }, 2000);

       }

   }

  - Add in activity_splash.xml

   <RelativeLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:tools="http://schemas.android.com/tools"
       xmlns:custom="http://schemas.android.com/apk/res-auto"
       android:background="@color/colorAccent"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       tools:context=".Splash">

       <ImageView
           android:id="@+id/ivLogo"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:src="@mipmap/icon_splash"
           android:layout_centerHorizontal="true"
           android:layout_centerVertical="true"/>


       <ProgressBar
           android:id="@+id/circle_progress"
           style="?android:attr/progressBarStyleHorizontal"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:layout_alignParentBottom="true"
           android:layout_marginBottom="5dp"
           android:max="100"
           android:progressTint="@color/green"
           android:visibility="visible" />

   </RelativeLayout>

  - Add in AndroidManifest.xml

    <activity android:name="ex.com.SplashActivity">
               <intent-filter>
                   <action android:name="android.intent.action.MAIN" />

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