【问题标题】:Add the loading screen in starting of the android application在 android 应用程序的启动中添加加载屏幕
【发布时间】:2013-06-25 18:51:00
【问题描述】:

我的应用将在 10 秒后加载起始页。在 10 秒的时间里,android 屏幕是空白的。 那时我想添加加载屏幕。如何添加它? 并在应用程序中告诉我如何知道开始页面正在加载?并告诉我如何在我的应用中进行操作?

【问题讨论】:

  • 你可以使用全屏对话框或者你可以使用闪屏。当你的进程开始时显示对话框,结束时关闭它。
  • 我认为这个链接会对你有所帮助:stackoverflow.com/questions/15452061/android-splash-screen。祝你好运!
  • 你是否考虑过一些虚假数据,而不是启动画面,或者一些教程,一个 10 秒的启动画面会锁定你的应用程序,这会让用户感到非常沮丧:cyrilmottier.com/2012/05/03/…
  • 这 10 秒内发生了什么?您的应用设计可能有误。

标签: android android-applicationinfo


【解决方案1】:

使用 ProgressDialog。

ProgressDialog dialog=new ProgressDialog(context);
dialog.setMessage("message");
dialog.setCancelable(false);
dialog.setInverseBackgroundForced(false);
dialog.show();

只要您的 UI 准备好数据,就隐藏它。调用:

dialog.hide();

【讨论】:

【解决方案2】:

您可以像这样在第一个加载活动中使用启动画面:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        Thread welcomeThread = new Thread() {

            @Override
            public void run() {
                try {
                    super.run();
                    sleep(10000);  //Delay of 10 seconds
                } catch (Exception e) {

                } finally {

                    Intent i = new Intent(SplashActivity.this,
                            MainActivity.class);
                    startActivity(i);
                    finish();
                }
            }
        };
        welcomeThread.start();
    }

希望此代码对您有所帮助。

【讨论】:

  • 假设你打开这个应用程序,5 秒后你离开它,然后再过 5 秒,应用程序再次显示,因为它开始了主要活动。当您离开应用程序/旋转屏幕等时,不要忘记以某种方式取消它。
  • 所以所有的处理都发生在那个 SplashActivity 中?如果是,那么如何创建一个封闭类,就像我创建了一个名为 SplashActivity 的新类但现在错误说它不是封闭类...请帮助...对不起,我是 android 新手
  • 这并不完全正确。它没有考虑膨胀启动活动布局所需的时间,这将导致启动时(更短的)空白屏幕。请参阅@yathavan 答案。
【解决方案3】:

Please read this article

克里斯·斯图尔特在那里写道:

启动画面只会浪费您的时间,对吧?作为一名安卓开发者, 当我看到启动画面时,我知道一些可怜的开发人员不得不添加一个 代码延迟三秒。

然后,我必须盯着某张照片三秒钟,直到我可以 使用该应用程序。每次启动时我都必须这样做。我知道 我打开了哪个应用程序。我知道它的作用。就让我用吧!

正确的启动画面

我相信 Google 并没有自相矛盾;旧的建议和 新站在一起。 (也就是说,使用它仍然不是一个好主意 一个浪费用户时间的闪屏。请不要那样做。)

不过,Android 应用确实需要一些时间才能启动, 特别是在冷启动时。那里有一个延迟,你可能不会 能够避免。而不是在这段时间留下一个空白屏幕,为什么 不向用户展示一些好东西?这是谷歌的方法 提倡。不要浪费用户的时间,但不要给他们一个空白, 在他们第一次启动应用程序时,应用程序的未配置部分也是如此。

如果您查看 Google 应用的最新更新,您会发现适当的 闪屏的用途。看看 YouTube 应用程序, 例子。

【讨论】:

  • 这应该是被接受的答案。这里提到的其他一切都是垃圾..
  • 它没有工作不知道为什么在 gradle build 中导致问题
  • 内嵌文章中的代码 sn-ps 会有所帮助
【解决方案4】:

您可以创建自定义加载屏幕而不是启动屏幕。如果您显示 10 秒的闪屏,这对于用户体验来说不是一个好主意。所以最好添加一个自定义加载屏幕。对于自定义加载屏幕,您可能需要一些不同的图像来使其感觉像 gif。之后在res 文件夹中添加图像并创建一个这样的类:-

public class LoadingScreen {private ImageView loading;

LoadingScreen(ImageView loading) {
    this.loading = loading;
}

public void setLoadScreen(){
    final Integer[] loadingImages = {R.mipmap.loading_1, R.mipmap.loading_2, R.mipmap.loading_3, R.mipmap.loading_4};
    final Handler loadingHandler = new Handler();
    Runnable runnable = new Runnable() {
        int loadingImgIndex = 0;
        public void run() {
            loading.setImageResource(loadingImages[loadingImgIndex]);
            loadingImgIndex++;
            if (loadingImgIndex >= loadingImages.length)
                loadingImgIndex = 0;
            loadingHandler.postDelayed(this, 500);
        }
    };
    loadingHandler.postDelayed(runnable, 500);
}}

在您的 MainActivity 中,您可以像这样将 a 传递给 LoadingScreen 类:-

private ImageView loadingImage;

不要忘记在activity_main 中添加一个ImageView。 之后像这样调用 LoadingScreen 类;

LoadingScreen loadingscreen = new LoadingScreen(loadingImage);
loadingscreen.setLoadScreen();

希望对你有帮助

【讨论】:

    【解决方案5】:
    public class Splash extends Activity {
    
        private final int SPLASH_DISPLAY_LENGHT = 3000;            //set your time here......
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.splash);
    
            new Handler().postDelayed(new Runnable(){
                @Override
                public void run() {
                    /* Create an Intent that will start the Menu-Activity. */
                    Intent mainIntent = new Intent(Splash.this,MainActivity.class);
                    Splash.this.startActivity(mainIntent);
                    Splash.this.finish();
                }
            }, SPLASH_DISPLAY_LENGHT);
        }
    }
    

    【讨论】:

      【解决方案6】:

      如果应用程序在那 10 秒内没有做任何事情,这将形成一个糟糕的设计,只会让用户等待 10 秒什么都不做。

      如果其中发生了什么,或者如果您希望实现 10 秒延迟启动画面,这里是代码:

      ProgressDialog pd;
      pd = ProgressDialog.show(this,"Please Wait...", "Loading Application..", false, true);
      pd.setCanceledOnTouchOutside(false);
      Thread t = new Thread()
      { 
            @Override
            public void run()
            {
                      try
                      {
                          sleep(10000)  //Delay of 10 seconds
                      } 
              catch (Exception e) {}
              handler.sendEmptyMessage(0);
              }
      } ;
      t.start();
      
      //Handles the thread result of the Backup being executed.
      private Handler handler = new Handler()
      {
          @Override
          public void handleMessage(Message msg) 
          {
              pd.dismiss();
              //Start the Next Activity here...
      
          }
      };
      

      【讨论】:

        【解决方案7】:

        编写代码:

          @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.splash);
        
                Thread welcomeThread = new Thread() {
        
                    @Override
                    public void run() {
                        try {
                            super.run();
                            sleep(10000)  //Delay of 10 seconds
                        } catch (Exception e) {
        
                        } finally {
        
                            Intent i = new Intent(SplashActivity.this,
                                    MainActivity.class);
                            startActivity(i);
                            finish();
                        }
                    }
                };
                welcomeThread.start();
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多