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

我应该如何进行实现呢?


当前回答

启动画面不应该从布局文件加载,加载时可能仍然有一些延迟。

最好的方法是为你的SplashScreenActivity创建一个主题,并设置android:windowBackground为一个可绘制的资源。

https://www.bignerdranch.com/blog/splash-screens-the-right-way/

简而言之:

在manifest中声明你的SplashScreenActivity:

<activity
        android:name=".activities.SplashScreenActivity"
        android:theme="@style/SplashTheme"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

在SplashScreenActivity.java中:

@Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = new Intent(this, MainActivity_.class);
    startActivity(intent);
    finish();

}

接下来创建主题背景窗口的资源:

<style name="SplashTheme" parent="Theme.Bumpfie.Base">
    <item name="android:windowBackground">@drawable/splash</item>
</style>

可绘制文件splash.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/white"/>
    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/app_logo"/>
    </item>
</layer-list>

其他回答

以上所有的答案都非常好。但是存在内存泄漏的问题。 这个问题在Android社区通常被称为“泄露活动”。这到底是什么意思呢?

当配置发生变化时,比如方向变化,Android会销毁Activity并重新创建它。通常,垃圾收集器只会清除旧Activity实例的已分配内存,一切正常。

“泄漏一个活动”指的是垃圾收集器无法清除旧活动实例的已分配内存的情况,因为它被(强)引用于一个活动实例外的对象。每个Android应用程序都分配了特定数量的内存。当垃圾回收器不能释放未使用的内存时,应用程序的性能将逐渐下降,最终崩溃并出现OutOfMemory错误。

如何确定应用程序是否泄漏内存?最快的方法是在Android Studio中打开内存选项卡,并在改变方向时注意分配的内存。 如果分配的内存一直在增加而不减少,那么就有内存泄漏。

1.当用户改变方向时内存泄漏。

首先,你需要在布局资源splashscreen.xml文件中定义启动画面

启动画面活动的示例代码。

public class Splash extends Activity {
 // 1. Create a static nested class that extends Runnable to start the main Activity
    private static class StartMainActivityRunnable implements Runnable {
        // 2. Make sure we keep the source Activity as a WeakReference (more on that later)
        private WeakReference mActivity;

        private StartMainActivityRunnable(Activity activity) {
         mActivity = new WeakReference(activity);
        }

        @Override
        public void run() {
         // 3. Check that the reference is valid and execute the code
            if (mActivity.get() != null) {
             Activity activity = mActivity.get();
             Intent mainIntent = new Intent(activity, MainActivity.class);
             activity.startActivity(mainIntent);
             activity.finish();
            }
        }
    }

    /** Duration of wait **/
    private final int SPLASH_DISPLAY_LENGTH = 1000;

    // 4. Declare the Handler as a member variable
    private Handler mHandler = new Handler();

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

        // 5. Pass a new instance of StartMainActivityRunnable with reference to 'this'.
        mHandler.postDelayed(new StartMainActivityRunnable(this), SPLASH_DISPLAY_LENGTH);
    }

    // 6. Override onDestroy()
    @Override
    public void onDestroy() {
     // 7. Remove any delayed Runnable(s) and prevent them from executing.
     mHandler.removeCallbacksAndMessages(null);

     // 8. Eagerly clear mHandler allocated memory
     mHandler = null;
    }
}

欲了解更多信息,请点击此链接

public class SplashActivity extends Activity {

  Context ctx;

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

      Thread thread = new Thread(){
          public void run(){
              try {
                  sleep(3000);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }

              Intent in = new Intent(ctx,MainActivity.class);
              startActivity(in);
              finish();
          }
      };
      thread.start();
  }
}

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

一个超级灵活的启动屏幕如何,可以使用相同的代码,并在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的新手,我在开发过程中遇到了这些问题。希望这能帮助到像我这样的人。

The Splash screen is the entry point of my app, so add the following lines in AndroidManifest.xml. <activity android:name=".SplashActivity" android:theme="@android:style/Theme.DeviceDefault.Light.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> The splash screen should only show once in the app life cycle, I use a boolean variable to record the state of the splash screen, and only show it on the first time. public class SplashActivity extends Activity { private static boolean splashLoaded = false; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (!splashLoaded) { setContentView(R.layout.activity_splash); int secondsDelayed = 1; new Handler().postDelayed(new Runnable() { public void run() { startActivity(new Intent(SplashActivity.this, MainActivity.class)); finish(); } }, secondsDelayed * 500); splashLoaded = true; } else { Intent goToMainActivity = new Intent(SplashActivity.this, MainActivity.class); goToMainActivity.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); startActivity(goToMainActivity); finish(); } } }

编码快乐!