到目前为止,所有的尝试都没有成功。我尝试了anddev.org上提到的方法,但我的问题是主Activity应该启动,但是在填充ListActivity之前应该显示闪屏界面。因此,简单来说,我必须执行以下步骤:
1. 启动主Activity。 2. 显示闪屏界面。 3. 在后台持续运行进程。 4. 当处理完成并显示主List时退出闪屏界面。
希望您明白我的需求。
public class MyActivity extends Activity {
private ProgressDialog pd = null;
private Object data = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Show the ProgressDialog on this thread
this.pd = ProgressDialog.show(this, "Working..", "Downloading Data...", true, false);
// Start a new thread that will download all the data
new DownloadTask().execute("Any parameters my download task needs here");
}
private class DownloadTask extends AsyncTask<String, Void, Object> {
protected Object doInBackground(String... args) {
Log.i("MyApp", "Background thread starting");
// This is where you would do all the work of downloading your data
return "replace this with your data object";
}
protected void onPostExecute(Object result) {
// Pass the result data back to the main activity
MyActivity.this.data = result;
if (MyActivity.this.pd != null) {
MyActivity.this.pd.dismiss();
}
}
}
}
ProgressDialog
(在我的情况下是加载布局的常规对话框)从未显示。只有当我在onPostExecute
中注释掉.dismiss()
时才能看到它。我看到的第一个屏幕仍然是MyActivity
主题。之后直接进入R.layout.main
。 - Kevin_TA只是为了参考,这是我发现的制作闪屏界面的最佳方法:http://android-developers.blogspot.de/2009/03/window-backgrounds-ui-speed.html
我搜索了一段时间才从Android文档中找到了这个方法...如果你想避免那些黑屏,你需要创建一个带有windowBackground的主题,如下:
<resources>
<style name="Theme.Shelves" parent="android:Theme">
<item name="android:windowBackground">@drawable/background_shelf</item>
<item name="android:windowNoTitle">true</item>
</style>
</resources>
将此主题设置为您的主活动的主题...... TADA,在第一秒中出现闪屏。
如果您想要一个复杂的背景而不仅仅是一个会被拉伸以填充的图像,您可以使用Drawable。下面是一个layer-list的例子,它将保持标志居中并带有黑色背景:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@color/black">
</item>
<item>
<bitmap
android:gravity="center"
android:src="@drawable/logo"
android:tileMode="disabled" >
</bitmap>
</item>
</layer-list>
- 启动主活动。
- 显示闪屏界面。
- 在后台继续运行进程。
- 当处理完成并显示主列表时,退出闪屏界面。
我尝试过这种方式,但问题是:它会在启动闪屏界面活动之前显示主活动。
我这样做:
我的问题是如何通过按菜单按钮来禁用“显示闪屏界面活动的菜单”。
编辑:
禁用显示菜单:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// getMenuInflater().inflate(R.menu.activity_main, menu);
return false;
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// return super.onMenuItemSelected(featureId, item);
return false;
}
public class MainActivity extends Activity {
private ImageView splashImageView;
boolean splashloading = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
splashImageView = new ImageView(this);
splashImageView.setScaleType(ScaleType.FIT_XY);
splashImageView.setImageResource(R.drawable.ic_launcher);
setContentView(splashImageView);
splashloading = true;
Handler h = new Handler();
h.postDelayed(new Runnable() {
public void run() {
splashloading = false;
setContentView(R.layout.activity_main);
}
}, 3000);
}
}
在编写启动屏幕时,您应该使用 Android 平台的 SplashScreen API,它可以提供自适应图标或 AVD,并控制其可见时间。请查看文档以获取更多信息。
class MainActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
// Handle the splash screen transition.
val splashScreen = installSplashScreen()
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
同时创建一个启动页主题,并将其提供在清单文件中
<style name="Theme.App.Starting" parent="Theme.SplashScreen">
<!-- Set the splash screen background, animated icon, and animation duration. -->
<item name="windowSplashScreenBackground">@color/...</item>
<!-- Use windowSplashScreenAnimatedIcon to add either a drawable or an
animated drawable. One of these is required. -->
<item name="windowSplashScreenAnimatedIcon">@drawable/...</item>
<!-- Required for animated icons -->
<item name="windowSplashScreenAnimationDuration">200</item>
<!-- Set the theme of the Activity that directly follows your splash screen. -->
<!-- Required -->
<item name="postSplashScreenTheme">@style/Theme.App</item>
</style>
在你的清单文件中进行设置:
<manifest>
<application android:theme="@style/Theme.App.Starting">
<!-- or -->
<activity android:theme="@style/Theme.App.Starting">
...
我知道这个问题已经很老了,而且提问者可能不再需要它。但我想添加这个答案来帮助那些可能需要实现启动画面的人。
实际上,在Android的新版本(Android Oreo之后),它已经支持内置启动画面实现。这意味着您不需要创建额外的活动来完成此操作。您只需要一个可显示的可绘制资源文件。
使用这种方式可以更快地显示您的启动画面,并在初始化后立即显示内容。但请注意,这仅适用于Android Oreo或更高版本。在以前的版本中,它将显示白色而不是您的启动画面(至少我认为是这样)。
您需要在AppTheme样式中添加此行:
<item name="android:windowSplashscreenContent">@drawable/YOUR_SPLASH_SCREEN_DRAWABLE</item>
这是一个完整的示例:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<!-- Set your splash screen here, it accept a resource from drawable directory-->
<item name="android:windowSplashscreenContent" tools:targetApi="o">@drawable/splash_screen</item>
</style>
更多关于这个属性的信息,请参考官方文档:https://developer.android.com/reference/android/R.attr#windowSplashscreenContent
如上所述,此属性在API 26中添加。
以下是其中提到的简短摘录:
引用要用作窗口闪屏内容的可绘制对象。此可绘制对象将放置在
windowBackground
之上,并通过系统栏进行边界插入。如果可绘制对象不应由系统栏插入,请使用全屏主题。
请注意,即使在主题中没有设置闪屏内容,系统仍可能使用主题上的其他属性(例如
windowBackground
)显示闪屏。