ViewPager 没有在滚动时剪辑 VideoView
Posted
技术标签:
【中文标题】ViewPager 没有在滚动时剪辑 VideoView【英文标题】:ViewPager is not clipping VideoView on scrolling 【发布时间】:2015-07-07 20:12:29 【问题描述】:我正在一个有 ViewPager 的 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);
【问题讨论】:
【参考方案1】:VideoView
在内部使用SurfaceView
来显示您的视频。
根据SurfaceView Javadoc:
表面是 Z 顺序的,因此它位于持有其 SurfaceView 的窗口后面; SurfaceView 在其窗口中打了一个孔以显示其表面。视图层次结构将负责正确地将 SurfaceView 的任何兄弟姐妹与 Surface 合成,这些兄弟通常会出现在它的顶部。
您可以使用TextureView
来解决此问题。
检查这个similar question.
【讨论】:
这对我很有帮助,下次我尝试制作类似的东西时会考虑它。谢谢。以上是关于ViewPager 没有在滚动时剪辑 VideoView的主要内容,如果未能解决你的问题,请参考以下文章