Android 如何制作启动画面(应用启动时屏幕可见)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2222890/
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
How to make a splash screen (screen visible when app starts)?
提问by pambuk
I have a simple application, it starts, loads xml feed from the net, you can browse a list of news and then read details for a chosen news item. What I would like to do is have a splash screen, meaning as soon as you click application, it should display an image (app name in my case) and then display news list only after they've loaded.
我有一个简单的应用程序,它启动,从网络加载 xml 提要,您可以浏览新闻列表,然后阅读所选新闻项目的详细信息。我想要做的是有一个闪屏,这意味着只要你点击应用程序,它就会显示一个图像(在我的例子中是应用程序名称),然后只有在它们加载后才显示新闻列表。
I read about similar (I think) problems, and usually people say to use FrameLayout, but I can't really sort it out. I'm not sure if this can be done in the first activity that is launched, maybe I should just display this splash image in one activity and only then call activity displaying my news list?
我读过类似(我认为)的问题,通常人们说要使用 FrameLayout,但我无法真正解决它。我不确定这是否可以在启动的第一个活动中完成,也许我应该只在一个活动中显示此启动图像,然后才调用活动显示我的新闻列表?
I know that on iPhone you can set splash screen in app settings while developing, would be nice to have this functionality in android's app's manifest...
我知道在 iPhone 上,您可以在开发时在应用程序设置中设置启动画面,在 android 应用程序的清单中拥有此功能会很好......
回答by jagsaund
Android suggests you take advantage of using a splash screen when performing lengthy calculations on start up. Here's an excerpt from the Android Developer Website - Designing for Responsiveness:
Android 建议您在启动时执行冗长的计算时利用启动画面。以下是 Android 开发者网站的摘录 - 响应性设计:
"If your application has a time-consuming initial setup phase, consider showing a splash screen or rendering the main view as quickly as possible and filling in the information asynchronously. In either case, you should indicate somehow that progress is being made, lest the user perceive that the application is frozen." -- Android Developer Site
“如果您的应用程序有一个耗时的初始设置阶段,请考虑尽快显示启动画面或渲染主视图并异步填写信息。无论哪种情况,您都应该以某种方式表明正在取得进展,以免用户认为应用程序已被冻结。” -- Android 开发者网站
You can create an activity that shows a Progress Dialog while using an AsyncTask to download the xml feed from the net, parse it, store it to a db (if needed) and then start the Activity that displays the News Feeds. Close the splash Activity by calling finish()
您可以创建一个显示进度对话框的活动,同时使用 AsyncTask 从网络下载 xml 提要,解析它,将其存储到数据库(如果需要),然后启动显示新闻提要的活动。通过调用完成()关闭飞溅活动
Here's a skeleton code:
这是一个骨架代码:
public class SplashScreen extends Activity{
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
// set the content view for your splash screen you defined in an xml file
setContentView(R.layout.splashscreen);
// perform other stuff you need to do
// execute your xml news feed loader
new AsyncLoadXMLFeed().execute();
}
private class AsyncLoadXMLFeed extends AsyncTask<Void, Void, Void>{
@Override
protected void onPreExecute(){
// show your progress dialog
}
@Override
protected Void doInBackground(Void... voids){
// load your xml feed asynchronously
}
@Override
protected void onPostExecute(Void params){
// dismiss your dialog
// launch your News activity
Intent intent = new Intent(SplashScreen.this, News.class);
startActivity(intent);
// close this activity
finish();
}
}
}
hope that helps!
希望有帮助!
回答by Tomer Hadad
I know this is old but for those of you who are still facing this problem, you can use this simple android-splash libraryto show your splash screen.
我知道这是旧的,但对于那些仍然面临这个问题的人,你可以使用这个简单的android-splash 库来显示你的启动画面。
SplashBuilder
.with(this, savedInstanceState)
.show();
You can set SplashTask
that will execute while the splash screen is displayed.
您可以设置SplashTask
将在显示启动画面时执行。