[英]Main Activity start delay
當我開始我的活動時,我注意到一個短暫的延遲。 我點擊我的應用程序圖標,主屏幕在屏幕上停留約1-1.5秒,然后顯示我的活動。
我的活動onCreate方法需要大約800毫秒才能完成。
我還注意到設置android:screenOrientation="landscape"
也會增加明顯的延遲,即使我使用帶有空布局的測試Activity也是如此。
有沒有辦法擺脫這種延遲,或者至少在UI加載時顯示黑屏?
編輯:請參閱下面的測試活動代碼。 在我的實際活動中,有許多其他加載包括GUI元素和引擎邏輯,聲音等。真正的問題是即使使用這個小的測試活動,延遲也是可見的。
測試活動代碼:
public class TestActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set full screen mode and disable
// the keyguard (lockscreen)
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.main);
}
}
布局XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true" >
</FrameLayout>
表現:
<activity
android:name=".TestActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:windowSoftInputMode="stateAlwaysHidden|adjustPan" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
您可以將默認活動設置為近乎空白的活動,該活動只會顯示背景並啟動您的實際活動..就像啟動畫面一樣
Ans Drake給出的是完美的,但要擴展它 - 讓你想讓你的自定義閃屏圖像一直可見,如果我們將顏色應用到窗口,那么將有一個從顏色切換到實際的斜杠屏幕。 這可能是一個糟糕的用戶體驗。 我建議的ans不需要setContentView但只通過主題管理啟動畫面。
我們無法在java中設置主題,就像在我的情況下,控件來到我的啟動活動的創建很晚。 直到那個時候黑屏保持可見。
這讓我覺得窗口必須從我們在清單中指定的主題進行管理。
我有的是:
<activity
android:name=".main.activities.SplashScreen"
android:theme="@style/Splash"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
現在我創建的主題如下:
<style name="Splash" parent="@style/Theme.AppCompat.Light">
<item name="android:windowBackground">@drawable/splash</item>
<item name="android:windowNoTitle">true</item>
<item name="windowNoTitle">true</item>
<item name="colorPrimaryDark">@color/green_09</item>
<item name="colorPrimary">@color/green_09</item>
<item name="windowActionBar">false</item>
</style>
在包含位圖資源的可繪制資源中進行初始化,我必須調整一下以使其看起來完美而不是拉伸並位於中心:
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:antialias="true"
android:dither="true"
android:gravity="fill"
android:src="@drawable/splash_screen" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.