ViewPager 未在滚动时裁剪 VideoView

ViewPager is not clipping VideoView on scrolling

我正在 Activity 中编写一个小教程屏幕,它有一个 ViewPager,它又有几个页面,其中一个可以是在无限循环中运行的视频和其他页面上的图像.

问题是,当我在播放(或暂停)视频的情况下滚动页面时,videoview 从容器中伸出来。我尝试在容器和 ViewPager 上设置剪辑子项和填充属性,但无济于事。

你能告诉我一些可能导致这种行为的原因吗?

视频的设置在 PageAdapter 中完成,如下所示:

     ![enter image description here][1]TutorialPage tutorialPageItem = tutorialPagesList.get(position);
    // Set tutorialImageDrawable as a compound drawable of the TextView
    Drawable tutorialImageDrawable = tutorialPageItem.getTutorialImageDrawable();
    if (tutorialImageDrawable != null) {
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)imageView.getLayoutParams();
        params.width = LayoutParams.WRAP_CONTENT;
        params.height = LayoutParams.WRAP_CONTENT;
        imageView.setImageDrawable(tutorialImageDrawable);
        videoView.setVisibility(View.INVISIBLE);
    } else {
        try {
            //set the uri of the video to be played
            videoView.setVideoURI(Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.up_test_640x640_lite_2));

        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }

        videoView.requestFocus();
        videoView.setZOrderMediaOverlay(true);
        //we also set an setOnPreparedListener in order to know when the video file is ready for playback
        videoView.setOnPreparedListener(new OnPreparedListener() {

            private int videoPosition;

            public void onPrepared(MediaPlayer mediaPlayer) {
                // close the progress bar and play the video
                //if we have a position on savedInstanceState, the video playback should start from here
                videoView.seekTo(videoPosition);
                if (videoPosition == 0) {
                    videoView.start();
                } else {
                    //if we come from a resumed activity, video playback will be paused
                    videoView.pause();
                }
            }
        });
        videoView.setOnCompletionListener(new OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
                // TODO Auto-generated method stub
                videoView.seekTo(0);
                videoView.start();
            }

        });


        ViewTreeObserver treeObserver = imageView.getViewTreeObserver();
        treeObserver.addOnPreDrawListener(new OnPreDrawListener() {

            @Override
            public boolean onPreDraw() {
                // TODO Auto-generated method stub
                imageView.getViewTreeObserver().removeOnPreDrawListener(this);
                int width = imageView.getWidth();
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)imageView.getLayoutParams();
                params.height = width;
                imageView.setLayoutParams(params);
                return false;
            }

        });
        imageView.setVisibility(View.INVISIBLE);
    }

VideoView 在内部使用 SurfaceView 来显示您的视频。 根据 SurfaceView Javadoc:

The surface is Z ordered so that it is behind the window holding its SurfaceView; the SurfaceView punches a hole in its window to allow its surface to be displayed. The view hierarchy will take care of correctly compositing with the Surface any siblings of the SurfaceView that would normally appear on top of it.

您可以使用 TextureView 来解决这个问题。 检查这个