Splash启动界面秒开的正确打开模式

谷歌建议

谷歌在material design中提倡使用Splash启动界面。那Splash启动界面如何呈现秒开的效果呢,我们下面来看看具体的实现步骤。

具体实现

在drawable文件夹下建立splash.xml

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

在style.xml为SplashActivity建立一个theme:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    </style>

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

</resources>

在AndroidManifest.xml中定义SplashActivity的theme为splash_theme,

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

SplashActivity的实现

package com.my.media;

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

public class SplashActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent lIntent = new Intent(this,MainActivity.class);
        startActivity(lIntent);
        finish();
    }
}

注意为了保证启动速度,SplashActivity不需要实现setContentView()方法。如果你的SplashActivity设置了layout文件,那么其在app完全初始化完成后才会显示,带来一定的耗时。这里直接以theme作为SplashActivity展示的UI,减少了加载时间达到秒开的效果。使用该启动画面实现方式亦可以避免有些app点开时出现的白屏问题。
参考

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,242评论 25 708
  • 世界上有很多污染:如噪音污染、环境污染、水污染等。这些污染给人们带来了极大的不便。现在我来做一份噪音污...
    一树_花开阅读 209评论 0 0
  • 人生有苦乐,有喜有悲, 有些是命中注定, 有些是努力争取。 他们出生自大凉山, 命也,时也。 我们与他们之前从未谋...
    3fd25a0a9f73阅读 229评论 0 0
  • 假设 A push B ,B pop 回A页面时候需要刷新A界面 1.B界面遵循协议UINavigationCon...
    toobai阅读 399评论 0 0