Android中使用AsyncTask的进度条

时间:2014-07-11 03:28:43

标签: android android-asynctask

我有一个函数名CreateVideo(),问题是:

  1. 因为功能必须在完成创建视频之前运行,否则我无法知道完成视频的百分之几。
  2. 我把功能放在了正确的位置吗?
  3. 以下是代码:

    private class MyVideo extends AsyncTask<Void, Void, String> {
    
        private Context context;
        private String output;
        private String quality;
        private FramePackage framePackage;
    
        CreateMP4Video createMP4Video;
    
        public MyVideo(Context context, FramePackage framePackage, String output, String quality) {
            // TODO Auto-generated constructor stub
            this.context = context;
            this.output = output;
            this.quality = quality;
            this.framePackage = framePackage;
        }
    
        @Override 
        protected void onPreExecute() {
            super.onPreExecute();
            // Create Video Maker
            createMP4Video = new CreateMP4Video(context, framePackage, output, quality);
    
            // Create progress
            mProgress = new ProgressDialog(MainActivity.this);
            mProgress.setTitle("Please Wait..");
            mProgress.setMessage("Creating Video...");
            mProgress.setProgressStyle(mProgress.STYLE_HORIZONTAL);
            mProgress.setProgress(0);
    
            // set Max = total number of frame
            mProgress.setMax(framePackage.getCount());
            mProgress.setCancelable(false);
            mProgress.show();
        };
    
    
        @Override
        protected String doInBackground(Void... params) {
            // TODO Auto-generated method stub
            try {
                createMP4Video.CreateVideo();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            while (mProgress.getProgress() <= mProgress.getMax()) {
    
                try {
    
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } 
    
                updateBarHandler.post(new Runnable() {
    
                    public void run() {
                        // get the current frame (frame 1, 2, 3,...)
                        // that already added to video
                        // In thist case, it always get the last frame
                        // progress bar run from 0% to 100% directly
                        mProgress.setProgress(createMP4Video.getCurrentFrame());
                      }
                  });
    
                if (mProgress.getProgress() == mProgress.getMax()) {    
                    mProgress.dismiss();
                }
            }
            return null;
        }
    
        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            Toast.makeText(context, result, Toast.LENGTH_SHORT).show();
        }
    
    }
    

    提前谢谢!

1 个答案:

答案 0 :(得分:0)

将onprogress对话框放在onPreExecute()方法中,就像你已经完成的那样

//创建进度

mProgress = new ProgressDialog(MainActivity.this);
        mProgress.setTitle("Please Wait..");
        mProgress.setMessage("Creating Video...");
        mProgress.setProgressStyle(mProgress.STYLE_HORIZONTAL);
        mProgress.setProgress(0);

        // set Max = total number of frame
        mProgress.setMax(framePackage.getCount());
        mProgress.setCancelable(false);
        mProgress.show();

取消onPostExecute

中的对话框

您可以查看progressupdate示例ONPROGRESS UPDATE