Android 安卓启动画面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1979524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android SplashScreen
提问by JaVadid
I'm developing an application which basically downloads a lot of data at the start of the application itself and displays it in the ListActivity. What I'm planning to do is show a Splash Screen till the data is loaded.
我正在开发一个应用程序,它基本上会在应用程序本身开始时下载大量数据并将其显示在 ListActivity 中。我打算做的是在加载数据之前显示启动画面。
Till now all my attempts have been futile. I tried anddev.org mentioned methods, but my problem is that the main Activity should start but The Splash Screen should be visible till I populate my ListActivity. So in short I have to go through the following steps:
直到现在我所有的尝试都是徒劳的。我尝试过 anddev.org 提到的方法,但我的问题是主活动应该启动,但启动画面应该是可见的,直到我填充我的 ListActivity。所以简而言之,我必须完成以下步骤:
- Start my main activity.
- Show the Splash Screen.
- Keep running the process in background.
- Exit the Splashscreen when processing completed and show the main List.
- 开始我的主要活动。
- 显示启动画面。
- 继续在后台运行该进程。
- 处理完成后退出启动画面并显示主列表。
Hope you understand what it is like....
希望你明白它是什么样的......
回答by Mark B
The problem is most likely that you are running the splash screen (some sort of Dialogsuch as ProgressDialogI assume) in the same thread as all the work being done. This will keep the view of the splash screen from being updated, which can keep it from even getting displayed to the screen. You need to display the splash screen, kick off an instance of AsyncTaskto go download all your data, then hide the splash screen once the task is complete.
问题很可能是您在与所有正在完成的工作相同的线程中运行启动画面(某种对话框,例如我假设的ProgressDialog)。这将防止初始屏幕的视图被更新,这甚至可以防止它显示在屏幕上。您需要显示启动画面,启动AsyncTask 的一个实例以下载所有数据,然后在任务完成后隐藏启动画面。
So your Activity's onCreate() method would simply create a ProgressDialog and show it. Then create the AsyncTask and start it. I would make the AsyncTask an inner class of your main Activity, so it can store the data it has downloaded to some variable in your Activity and close the ProgressDialog in its onPostExecute() method.
因此,您的 Activity 的 onCreate() 方法将简单地创建一个 ProgressDialog 并显示它。然后创建 AsyncTask 并启动它。我会让 AsyncTask 成为主 Activity 的内部类,这样它就可以将下载的数据存储到 Activity 中的某个变量中,并在其 onPostExecute() 方法中关闭 ProgressDialog。
Not sure how to elaborate anymore without just showing the code, so here it is:
不知道如何在不显示代码的情况下再详细说明,所以这里是:
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();
}
}
}
}
Obviously there are some pieces you need to fill in there, but this code should run and give you a good starting point (forgive me if there is a code error, I don't have access to the Android SDK as I'm typing this currently).
显然,您需要在那里填写一些内容,但是此代码应该可以运行并为您提供一个良好的起点(如果有代码错误,请原谅我,我在输入此代码时无法访问 Android SDK目前)。
Some more good reading on the subject of AsyncTasks in Android can be found hereand here.
回答by Raanan
just for reference this is the best way I found to make a splash screen: http://android-developers.blogspot.de/2009/03/window-backgrounds-ui-speed.html
仅供参考,这是我发现制作启动画面的最佳方式:http: //android-developers.blogspot.de/2009/03/window-backgrounds-ui-speed.html
I was searching for this for quite a while, from androids docs.. if you want to avoid those black screens, you need to create a theme with windowBackground so:
我从 androids 文档中搜索了很长一段时间。如果你想避免那些黑屏,你需要用 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>
And set this theme as the theme for your main activity... TADA, splashscreen from the first second.
并将此主题设置为您主要活动的主题... TADA,从第一秒开始的启动画面。
If you want a complex background and not just an image that will be stretched to fill you can use Drawables, here is an example of a layer-list that will keep the logo centered with a black background:
如果您想要一个复杂的背景,而不仅仅是一个将被拉伸以填充的图像,您可以使用 Drawables,这是一个图层列表示例,它将使徽标以黑色背景居中:
<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>
回答by Gabe
- Start my main activity.
- Show the Splash Screen.
- Keep running the process in background.
- Exit the Splashscreen when processing completed and show the main List.
- 开始我的主要活动。
- 显示启动画面。
- 继续在后台运行该进程。
- 处理完成后退出启动画面并显示主列表。
I tried this way, but the problem is; it will show the main activity before starting the splash screen activity.
我试过这种方式,但问题是;它将在启动初始屏幕活动之前显示主要活动。
I made it this way:
我是这样做的:
- Start the Splash screen
- When process gets completed, start the "main activity"
- Do Not forget to handle the back button, so it should close the App ist will be pressed in the main activity. Otherwise will back to the splash screen (loop)
- 启动启动画面
- 流程完成后,启动“主要活动”
- 不要忘记处理后退按钮,所以它应该关闭应用程序,它会在主活动中被按下。否则会回到闪屏(循环)
My Problem is how to disable "show the Menu of the Splash screen activity" by pressing the menu-button.
我的问题是如何通过按菜单按钮禁用“显示启动画面活动的菜单”。
Edit:
编辑:
Disable show menu:
禁用显示菜单:
@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;
}
回答by kablu
Splash screen example :
启动画面示例:
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);
}
}