優(yōu)化——apk啟動(dòng)黑白屏

對(duì)于比較大型的應(yīng)用,啟動(dòng)的時(shí)候(特別是冷啟動(dòng))會(huì)顯示一段時(shí)間黑屏或白屏(和應(yīng)用的theme有關(guān)),對(duì)用戶很不友好,一般來說,簡(jiǎn)單的處理方式有以下兩種

<item name="android:windowDisablePreview">true</item>
不顯示啟動(dòng)界面

<item name="android:windowIsTranslucent">true</item>
把啟動(dòng)界面的背景設(shè)成透明的

但是這種處理方案有個(gè)很嚴(yán)重的問題,就是點(diǎn)了圖標(biāo)之后,會(huì)卡在主界面一段時(shí)間再顯示應(yīng)用界面,實(shí)際上因?yàn)閼?yīng)用加載導(dǎo)致的

主流大應(yīng)用的處理方法
  • 自定義主題
  • 將啟動(dòng)activity的theme設(shè)置為自定義主題
  • 在啟動(dòng)activity的onCreate方法中,在super.onCreate之前將theme設(shè)置回去

先定義一個(gè)theme

<style name="LaunchTheme">
    <item name="android:windowBackground">@drawable/launch_layout</item>
    <item name="android:windowFullscreen">true</item>
    <item name="windowNoTitle">true</item>
</style>

定義啟動(dòng)界面顯示的內(nèi)容
launch_layout

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- The background color, preferably the same as your normal theme -->
    <item android:drawable="@android:color/holo_red_dark"/>

    <item android:top="150dp">
        <bitmap android:gravity="top"
            android:src="@drawable/girl" />
    </item>
</layer-list>

定義一個(gè)啟動(dòng)的activity

public class SplashActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                startActivity(new Intent(SplashActivity.this, MainActivity.class));
                finish();
            }
        }).start();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}

activity_splash

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

   <ImageView
       android:id="@+id/go"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:src="@drawable/music"
       android:layout_alignParentBottom="true"
       android:layout_centerHorizontal="true"
       android:layout_marginBottom="50dp"/>

</RelativeLayout>

將啟動(dòng)的activity主題設(shè)為我們的啟動(dòng)主題

<activity android:name="com.example.optimizes.lunchapp.SplashActivity"
    android:theme="@style/LaunchTheme">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容