创建一个启动画面,不需要额外的时间让应用程序启动

时间:2018-05-17 06:25:34

标签: android screen splash

我有这个启动画面:

enter image description here

它的问题在于它为应用程序启动了额外的时间,这是我不想要的。我发现有一种方法可以更改使用android:windowBackground启动任何应用时显示的默认白色闪屏。目前我正在使用layer-list进行此操作,但我在将LinearLayout转换为layer-list时遇到了很大的问题。知道如何制作这段代码吗?

当前的启动画面代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gradient_background"
android:orientation="vertical"
android:weightSum="100"
tools:context="com.example.kristiqngergov.healthapp.SplashActivity">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="23">
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="34"
    android:gravity="center|bottom">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/splash_logo"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.286" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="43"
    android:gravity="center"
    android:orientation="vertical">
        <TextView
            android:id="@+id/app"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_gravity="center"
            android:layout_marginBottom="10dp"
            android:fontFamily="sans-serif-black"
            android:lineSpacingExtra="9sp"
            android:textColor="#fff"
            android:textSize="53sp"
            android:textStyle="normal"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            tools:layout_editor_absoluteY="448dp"
            tools:text="Dr.Oh" />

        <TextView
            android:id="@+id/wait"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="12sp"
            android:fontFamily="sans-serif"
            android:textStyle="normal"
            android:textColor="#fff"
            android:layout_centerVertical="true"
            tools:text="Моля изчакайте..." />
</LinearLayout>

我想要的启动码:

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

图层列表的问题在于我无法真正设置每个项目的重量,这是一个很大的问题,因为图像太大而且适合整个屏幕。

图层列表启动画面:

enter image description here

1 个答案:

答案 0 :(得分:0)

// Splash screen timer
    private static int SPLASH_TIME_OUT = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_splash);

        new Handler().postDelayed(new Runnable() {

            /*
             * Showing splash screen with a timer. This will be useful when you
             * want to show case your app logo / company
             */

            @Override
            public void run() {
                // This method will be executed once the timer is over
                // Start your app main activity
                Intent i = new Intent(SplashScreen.this, MainActivity.class);
                startActivity(i);

                // close this activity
                finish();
            }
        }, SPLASH_TIME_OUT);
    }