Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

點擊圖示直達瀏覽器:用全透明首頁消除閃屏

編輯:關於 Android 程式設計

有個案子是把 App 當成一個入口,點開後直接導向指定的網頁。功能本身幾行程式碼就搞定,但實測時會看到一個空白畫面在跳轉前閃一下,客戶認為這是瑕疵。本文記錄問題成因與解法。

步驟一:在 onCreate() 導向瀏覽器

String url = "https://tiger-kfphone.com/";
Uri u = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, u);
// intent.setData(u);
// intent.setClassName("com.android.browser",
//         "com.android.browser.BrowserActivity");
startActivity(intent);
finish();

問題就在這裡:跳轉之前會先短暫顯示那個空白的首頁,因為程式是從這個畫面跳過去的。

步驟二:把首頁主題改成透明

解法是把這個首頁變成全透明,使用者就看不到那一瞬間的白/黑畫面。先在 styles.xml 定義主題:

<style name="Translucent_NoTitle" parent="android:style/Theme.Dialog">
    <item name="android:windowNoTitle">true</item>
    <item name="android:background">#00000000</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
    <item name="android:windowIsTranslucent">true</item>
</style>

接著在 AndroidManifest.xml 把啟動 Activity 掛上這個主題:

<activity
    android:name="com.hklt.link.MainActivity"
    android:label="@string/app_name"
    android:theme="@style/Translucent_NoTitle" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

幾個關鍵項目說明:windowIsTranslucent 讓視窗半透明、windowBackground 與 background 都設為透明色、colorBackgroundCacheHint 設為 @null 避免系統用快取色填底。這幾項搭配起來,啟動畫面就不會再有那個突兀的閃屏。

如果希望更徹底,也可以考慮改用 windowDisablePreview 搭配主題,直接關掉啟動時的預覽視窗。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved