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

我应该如何进行实现呢?


当前回答

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

实现启动画面

正确执行闪屏的方法与你想象的略有不同。您所看到的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/ 感谢这些人把我推向正确的方向。我想要帮助其他人,因为公认的答案并不是做启动画面的建议。

其他回答

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

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

你可以在你的onCreate方法中添加这个

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

初始化你的时间值在毫秒,因为你想…

private  static int time=5000;

欲了解更多详细信息,请从此链接下载完整代码…

https://github.com/Mr-Perfectt/Splash-Screen

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

这是完整的代码

SplashActivity.java

public class SplashActivity extends AppCompatActivity {

private final int SPLASH_DISPLAY_DURATION = 1000;

@Override
public void onCreate(Bundle bundle) {
    super.onCreate(bundle);


    new Handler().postDelayed(new Runnable(){
        @Override
        public void run() {

            Intent mainIntent = new Intent(SplashActivity.this,MainActivity.class);
            SplashActivity.this.startActivity(mainIntent);
            SplashActivity.this.finish();
        }
    }, SPLASH_DISPLAY_DURATION);
}}

在drawables中创建bg_splash.xml

<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:drawable="@color/app_color"/>

<item>
    <bitmap
        android:gravity="center"
        android:src="@drawable/ic_in_app_logo_big"/>
</item></layer-list>

在styles.xml中创建一个自定义主题

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

最后在AndroidManifest.xml中指定活动的主题

<activity
        android:name=".activities.SplashActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/SplashTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

欢呼。

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

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