我正试图在Youtube应用程序中复制视频最小化,如图所示。为了实现这一点,我尝试使用可拖动面板库。当我运行示例时,我注意到视频在播放过程中最小化时不会缩放,而是裁剪。当视频停止(而不是暂停)并显示缩略图时,视图会按预期缩放。我在另一个问题上读到YouTubePlayerView是用SurfaceView实现的。我还在文档中读到,SurfaceView的行为与普通视图不同,因为它在屏幕上打孔的方式。我相信,因为YoutubePlayerView是基于SurfaceView的,所以它的缩放比例不正确。如何在YoutubePlayerView中正确缩放播放的视频,以在播放过程中匹配其父布局的大小?
根据我使用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;
}