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

我应该如何进行实现呢?


当前回答

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

其他回答

虽然有很好的答案,但我将展示谷歌推荐的方法:

1)首先为启动画面创建一个主题: 你有一个名为splashscreenTheme的主题,你的启动器主题将是:

<style name="splashscreenTheme">
  <item name="android:windowBackground">@drawable/launch_screen</item>
</style>

注意:

android:windowBackground已经设置了你的溅屏图像编号 需要在UI中再次这样做。

你也可以在这里使用颜色来代替绘图。

2)设置主题为splashscreenActivity的manifest

   <activity
            android:name=".activity.splashscreenActivity"
            android:screenOrientation="portrait"
            android:theme="@style/splashscreenTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

3)确保你的launch_screen drawable不在drawable文件夹如果你的图像不小。

这将导致更快的启动屏幕启动和拯救你从黑屏

这也避免了额外的透支

一个超级灵活的启动屏幕如何,可以使用相同的代码,并在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();
    }
  }
}

我在android中使用线程制作Flash屏幕。

    import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

public class HomeScreen extends AppCompatActivity{
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen_home);

        Thread thread = new Thread(){
            public void run(){
                try {
                    Thread.sleep(3 * 1000);
                    Intent i = new Intent(HomeScreen.this, MainActivity.class);
                    startActivity(i);
                } catch (InterruptedException e) {
                }
            }
        };
        thread.start();
    }
}

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

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

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