在播放视频时调整/缩小YouTubePlayerFragment

时间:2014-11-09 16:20:53

标签: android youtube-api android-mediaplayer surfaceview android-videoview

我正在尝试复制Youtube应用中的视频最小化,如图here所示。为了实现这一点,我尝试使用Draggable Panel库。当我运行样本时,我注意到视频没有缩放,而是在播放期间最小化时裁剪。当视频停止(未暂停)并显示缩略图时,视图会按照预期进行缩放。我读到另一个问题,即使用SurfaceView实现了YouTubePlayerView。我还在文档中读到SurfaceView的行为与普通视图不同,因为它在屏幕上打孔。我相信因为YoutubePlayerView基于SurfaceView,所以它不能正确缩放。如何正确缩放YoutubePlayerView中播放的视频以匹配播放期间其父布局的大小?

1 个答案:

答案 0 :(得分:2)

根据我使用YouTubePlayerView和YouTubePlayerFragment的经验,我发现使用Nineoldandroids或ViewPropertyAnimator进行缩放无法正常工作。要调整播放视频的大小,必须以编程方式设置布局参数的高度和宽度。在DraggablePanel库中,有两个类可以更改顶视图的大小。默认值为ScaleTransformer,它在播放期间不适用于视频,因为它会将部分播放视频裁剪出视图,另一个是ResizeTransformer。 ResizeTransformer不像ScaleTransformer那样流畅,但它有点工作。 ResizeTransformer的问题在于,在拖动时,YouTubePlayerView的布局有时会在底部视图下进行剪辑。播放然后停止,因为它检测到重叠的视图。我做出了妥协,剥离了DraggablePanel,并为YouTubePlayerView的容器编写了最大化和最小化方法。

public void minimize() {
    RelativeLayout.LayoutParams playerParams =
            (RelativeLayout.LayoutParams) playerView.getLayoutParams();
    playerParams.width = getResources().getDimensionPixelSize(R.dimen.player_minimized_width);
    playerParams.height = getResources().getDimensionPixelSize(R.dimen.player_minimized_height);
    FrameLayout container = (FrameLayout)playerView.getParent().getParent();
    RelativeLayout.LayoutParams containerParams = (RelativeLayout.LayoutParams)container.getLayoutParams();
    containerParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    containerParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    containerParams.bottomMargin = getResources().getDimensionPixelSize(R.dimen.player_minimized_margin);
    containerParams.rightMargin = getResources().getDimensionPixelSize(R.dimen.player_minimized_margin);
    playerView.requestLayout();
    container.requestLayout();
    isMinimized = true;
}

public void maximize() {
    RelativeLayout.LayoutParams playerParams =
            (RelativeLayout.LayoutParams) playerView.getLayoutParams();
    playerParams.width = RelativeLayout.LayoutParams.MATCH_PARENT;
    playerParams.height = getResources().getDimensionPixelSize(R.dimen.player_height);
    FrameLayout container = (FrameLayout)playerView.getParent().getParent();
    RelativeLayout.LayoutParams containerParams = (RelativeLayout.LayoutParams)container.getLayoutParams();
    containerParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,0);
    containerParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,0);
    containerParams.bottomMargin = 0;
    containerParams.rightMargin = 0;
    playerView.requestLayout();
    container.requestLayout();
    isMinimized = false;
}