Android 主活动启动延迟
当我开始我的活动时,我注意到一个短暂的延迟。我点击我的应用程序图标,主屏幕在屏幕上停留约1-1.5秒,然后显示我的活动 我的活动的onCreate方法大约需要800毫秒才能完成 我还注意到,设置android:screenOrientation=“横向”也会增加明显的延迟,即使在我使用空布局的测试活动时也是如此 有没有办法消除这种延迟,或者至少在加载UI时显示一个黑屏 已编辑:请参见下面的测试活动代码。在我的实际活动中,还有一系列其他加载,包括GUI元素和引擎逻辑、声音等。真正的问题是,即使使用这个小测试活动,延迟也是可见的Android 主活动启动延迟,android,delay,android-activity,launch,Android,Delay,Android Activity,Launch,当我开始我的活动时,我注意到一个短暂的延迟。我点击我的应用程序图标,主屏幕在屏幕上停留约1-1.5秒,然后显示我的活动 我的活动的onCreate方法大约需要800毫秒才能完成 我还注意到,设置android:screenOrientation=“横向”也会增加明显的延迟,即使在我使用空布局的测试活动时也是如此 有没有办法消除这种延迟,或者至少在加载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设置主题,因为在我的例子中,控件很晚才开始创建我的splash活动。直到那时,黑屏仍然可见
这让我想到,窗口必须从我们在menifest中指定的主题进行管理
这是我最喜欢的:
<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>
@可牵引/飞溅
真的
真的
@颜色/绿色\u 09
@颜色/绿色\u 09
假的
Splash在包含位图资源的可绘制资源中,我必须稍微调整以使其看起来完美,而不是拉伸和居中:
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:antialias="true"
android:dither="true"
android:gravity="fill"
android:src="@drawable/splash_screen" />
从onCreate发布代码。它在模拟器中也可见吗?好的。我如何从这个飞溅开始主要活动?如果我将活动启动放入onCreate/onResume/。。。那么问题还是出现了。那么,我应该在哪里完成()启动活动呢?在“blank”主活动的onCreate()方法中使用类似的方法—startActivity(new Intent(getApplicationContext(),RealActivity.class));谢谢我还必须添加android:noHistory=“true”
和android:theme=“@android:style/theme.NoTitleBar.Fullscreen”
,以便在活动之间顺利过渡。