安卓闪屏

新手上路,请多包涵

我正在开发一个应用程序,它基本上在应用程序本身开始时下载大量数据并将其显示在 ListActivity 中。我打算做的是在加载数据之前显示一个启动画面。

直到现在我所有的尝试都是徒劳的。我尝试了 anddev.org 提到的方法,但我的问题是主 Activity 应该启动但在我填充我的 ListActivity 之前启动画面应该是可见的。所以简而言之,我必须经过以下步骤:

  1. 开始我的主要活动。
  2. 显示启动画面。
  3. 继续在后台运行该进程。
  4. 处理完成后退出启动画面并显示主列表。

希望你明白它是什么样的……

原文由 JaVadid 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 687
2 个回答

问题很可能是您在与所有正在完成的工作相同的线程中运行启动画面(我假设是某种 对话框,例如 ProgressDialog )。这将防止启动屏幕的视图被更新,这甚至可以防止它显示到屏幕上。您需要显示启动画面,启动 AsyncTask 实例以下载所有数据,然后在任务完成后隐藏启动画面。

因此,您的 Activity 的 onCreate() 方法将简单地创建一个 ProgressDialog 并显示它。然后创建 AsyncTask 并启动它。我会让 AsyncTask 成为主 Activity 的内部类,因此它可以将下载的数据存储到 Activity 中的某个变量中,并在其 onPostExecute() 方法中关闭 ProgressDialog。

不知道如何在不显示代码的情况下再详细说明,所以这里是:

 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();
             }
         }
    }
}

显然,您需要在那里填写一些内容,但是这段代码应该可以运行并为您提供一个很好的起点(如果有代码错误,请原谅我,因为我正在输入这个,所以我无法访问 Android SDK目前)。

可以在 此处此处 找到有关 Android 中 AsyncTasks 主题的更多好读物。

原文由 Mark B 发布,翻译遵循 CC BY-SA 3.0 许可协议

您应该使用 Android Platform SplashScreen API for Splash Screens,您可以提供自适应图标或 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">
...

原文由 Bex 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏