【问题标题】:How to create a splash screen with percentage progress bar?如何创建带有百分比进度条的启动画面?
【发布时间】:2012-10-23 03:39:05
【问题描述】:

我想在启动时通过进度条为我的应用程序显示一个初始屏幕,其中包含actual 加载应用程序的百分比。

我有以下要求/查询 -

  1. 我应该使用哪个组件来显示进度条
  2. 如果闪屏本身是应用程序的一部分,如何计算负载百分比
  3. 触摸启动画面时,我想突出显示进度条

【问题讨论】:

  • 到目前为止你做了什么??
  • 如果您的应用加载时间足够长,您觉得需要启动画面,您可能应该重新考虑您的应用
  • 首先,当您必须从服务器/网络下载数据时,主要使用闪屏,这些数据将进一步用于您的应用程序。并回答您的问题:为了显示负载百分比,您必须知道您可以计算和显示进度的总大小(以 kb/mb 为单位)。那么在您的情况下,您在启动画面中正在做什么?解释。或者你只是想显示虚拟%load?请解释一下,以便我们提供帮助。谢谢

标签: android


【解决方案1】:

您可以创建线程或使用异步任务并创建自定义进度条

asynctask 示例(仅伪代码)

private class SplashLoading extends AsyncTask<Variable, Variable, Variable> {

     @Override
     protected void onPreExecute(Variable) {
         Show the progress UI in here
     }
     @Override
     protected Long doInBackground(Variable) {
        do the heavy task here and don't forget to publish the progress
     }

     @Override
     protected void onProgressUpdate(Variable) {
         set the progress here
     }

     @Override
     protected void onPostExecute(Variable) {
         what will you do after it complete?
     }
 }

我的 asynctask 伪代码由 4 个函数组成

  1. onPreExecute 将在任务执行后立即在 UI 线程上调用。此步骤通常用于设置任务,例如通过在用户界面中显示进度条。

  2. 在 onPreExecute() 执行完成后,将立即在后台线程上调用 doInBackground。此步骤用于执行可能需要很长时间的后台计算。

  3. 在调用 publishProgress(Progress...) 后,将在 UI 线程上调用 onProgressUpdate。执行的时间是不确定的。此方法用于在后台计算仍在执行时在用户界面中显示任何形式的进度。例如,它可用于动画进度条或在文本字段中显示日志。

  4. onPostExecute(Result) 将在后台计算完成后在 UI 线程上调用。后台计算的结果作为参数传递给这一步。

您还可以添加 onCancelled() 以防您想在任务取消时进行处理

【讨论】:

    【解决方案2】:

    试试这个:我试过了,没有错误

    /**
     * Async class to get News
     * 
     */
    protected class AsynchTask extends AsyncTask<Void, Integer, Integer> {
    
        @Override
        protected void onPreExecute() {
    
        }
    
        @Override
        protected Integer doInBackground(Void... args) {
    
            download();
    
            return 1;
        }
    
        private void download() {
            // We are just imitating some process thats takes a bit of time
            // (loading of resources / downloading)
            int count = 10;
            for (int i = 0; i < count; i++) {
    
                // Update the progress bar after every step
                int progress = (int) ((i / (float) count) * 100);
                publishProgress(progress);
    
                // Do some long loading things
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException ignore) {
                }
            }
    
        }
    
        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            progressBar1.setProgress(values[0]);
        }
    
        @Override
        protected void onPostExecute(Integer a) {
            progressBar1.setVisibility(View.GONE);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-26
      • 1970-01-01
      • 2014-07-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多