我正在尝试在 Youtube 应用程序中复制视频最小化,如图所示here。为了实现这一目标,我尝试使用可拖动面板图书馆。当我运行示例时,我注意到视频在播放过程中最小化时不会缩放,而是会裁剪。当视频停止(不是暂停)并显示缩略图时,视图会按预期缩放。我在另一个问题上读到 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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)