应用启动时的白屏 - 启动画面

时间:2018-02-28 02:36:32

标签: android android-studio splash-screen

我使用空活动为我的应用创建了启动画面,使用背景图片保持可见 3秒钟。 通常,应用程序在背景图像变为可见之前以白色屏幕开始,但是,某些应用程序已经使用“真实”闪屏图像启动。 如何实施这个?

3 个答案:

答案 0 :(得分:1)

你可以这样使用闪屏。

  1. 在styles.xml中添加启动画面活动的样式

    <style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowBackground">@drawable/yourImageName</item>
    </style>
    
  2. 将该样式作为主题添加到清单文件中的SplashScreenActivity

    <activity android:name=".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>
    
  3. 删除SplashScreenActivity&#39; onCreate()方法中的setContentView()并将其用作扩展AppCompactActivity的java文件

答案 1 :(得分:0)

Splash Activity通常以后台开头。 试试这段代码,它可能对你有帮助。

启动活动页面的代码。

public class SplashScreen extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash_screen);

Thread myThread = new Thread(){

    @Override
    public void run() {
        try {
            sleep(3000);
            Intent intent = new Intent(getApplicationContext(),MainActivity.class);
            startActivity(intent);
            finish();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
};
    myThread.start();
}}

在布局文件中,您只需要包含要在启动页面上看到的图像。

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_splash_screen"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000">
   <ImageView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:src="@drawable/capture"
       />
</RelativeLayout>

并确保在Manifest中的Main活动之前添加Splashscreen页面。

 <activity android:name=".SplashScreen">

答案 2 :(得分:0)

问题是因为系统进程在启动应用时从documentation开始绘制初始黑屏:

  

实现主题启动屏幕的常用方法是使用 windowDisablePreview 主题属性关闭初始空白   启动应用程序时系统进程绘制的屏幕。然而,   这种方法可以导致比没有的应用程序更长的启动时间   禁止预览窗口。此外,它强制用户等待否   活动启动时的反馈,让他们想知道应用程序是否是   运作正常。

您可以使用windowDisablePreview属性禁用它,如下所示:

  <application
      ...
      android:windowDisablePreview="true">
   ...
  </application>