进度栏可见性消失,导致进度栏没有出现在第一位

时间:2019-04-16 18:34:21

标签: java android xml android-layout android-progressbar

我有一个以xml形式编写的进度条,最初显示为“已消失”。我想在开始共享视频/数据处理时使进度条“可见”,而在共享视频/数据处理完成时使进度条“消失”。

我首先尝试做

ProgressBar PB=(ProgressBar) activity.findViewById(R.id.ProgressBar);

PB.setVisibility(View.VISIBLE);

shareVideo(finalVideoTitle, finalVideoLink, activity);

PB.setVisiblity(View.GONE);

那没有用,我尝试了:

ProgressBar PB=(ProgressBar) activity.findViewById(R.id.ProgressBar);

activity.runOnUiThread(new Runnable() {
                       @Override
                        public void run() {
                            PB.setVisibility(View.VISIBLE);
                        }
                    });

shareVideo(finalVideoTitle, finalVideoLink, activity);

activity.runOnUiThread(new Runnable() {
                       @Override
                        public void run() {
                            PB.setVisibility(View.GONE);
                        }
                    });

这两种尝试均无济于事。但是,我注意到,如果我摆脱了将可见性设置为“消失”的代码,进度条就会显示出来,但显然不会被忽略。

在shareVideo()中,我有:

 private static void shareVideo(String videoTitle, String videoPath, MainActivity activity){
        // find the video file
        String videoPathforTrimmed = videoPath.substring(0, videoPath.length() - 4)+"Trimmed.mp4";
        String videoTitleTrimmed = videoTitle+"Trimmed";
        File file = new File(videoPathforTrimmed);
        long lengthInBytes = file.length();
        mAlertDialog.dismiss();

        if (!file.exists() || (lengthInBytes == 0)) {
            try {
                trimAndDuplicateVideo(videoPath,videoTitleTrimmed, videoPathforTrimmed, activity);
            }
            catch(IOException e)
            {
                System.out.println(e);
                return;
            }
        }


        Uri videoUri = Uri.parse("file:"+videoPathforTrimmed);

        try {
            Thread.sleep(2000);
        }
        catch(Exception e)
        {

        }


        try {
            Thread.sleep(500);
        }
        catch(Exception e)
        {

        }
        /*
        try {
            TimeUnit.SECONDS.sleep(4);
        }
        catch (Exception e)
        {

        }
        */

        ContentResolver resolver = activity.getContentResolver();
        String[] columns = {MediaStore.MediaColumns._ID};
        String selection = MediaStore.Video.VideoColumns.DATA + "=?";
        String selectionArgs[] = { videoPathforTrimmed };

        Cursor cursor = resolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                columns,
                selection,
                selectionArgs,
                null);

       if( cursor.getCount() > 0) {
            cursor.moveToFirst();
            videoUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            videoUri = Uri.withAppendedPath(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
            Integer.toString(cursor.getInt(0)));
            shareVideoIntentBestLap(videoTitleTrimmed, videoUri, activity);
        }
        else {
            videoNotRecognizedPopup(activity);
      }

    }

在trimAndDuplicateVideo()中,我正在使用mp4parser修剪视频,并将保存的视频输出到新文件。

这是位于线性布局内的进度条的xml:


    <ProgressBar
        android:id="@+id/videoTrimProgressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:indeterminate="true"
        android:max="100"
        android:backgroundTint="@color/white"
        android:minHeight="270dp"
        android:visibility="gone"
        android:indeterminateTint="#1a09d6"
        android:layout_marginTop="-7dp"/>

1 个答案:

答案 0 :(得分:0)

使用asynctask类并覆盖其方法:

   @Override
    protected void onPreExecute() {
        super.onPreExecute();
        PB.setVisibility(View.VISIBLE);
    }

   @Override
    protected String doInBackground(String... params) {
    shareVideo(finalVideoTitle, finalVideoLink, activity);
    }

   @Override
    protected void onPostExecute(String result){
     PB.setVisiblity(View.GONE);
    }