Resize/Scaling down a YouTubePlayerFragment while video is playing(在播放视频时调整/缩小 YouTubePlayerFragment)
问题描述
我正在尝试在 Youtube 应用中复制视频最小化,如此处所示.为了实现这一点,我尝试使用 Draggable Panel 库.当我运行示例时,我注意到视频在播放过程中最小化时不会缩放而是裁剪.当视频停止(未暂停)并显示缩略图时,视图会按预期缩放.我在另一个问题上读到 YouTubePlayerView 是用 SurfaceView 实现的.我还在文档中读到 SurfaceView 的行为与普通视图不同,因为它在屏幕上打了一个洞.我相信因为 YoutubePlayerView 是基于 SurfaceView 的,所以它不能正确缩放.如何正确缩放在 YoutubePlayerView 中播放的视频,以在播放期间匹配其父布局的大小?
I am trying to replicate the video minimization in the Youtube app as shown here. In order to achieve this, I've tried using the Draggable Panel library. When I ran the sample, I noticed that the video does not scale but rather crops when minimized during playback. When the video is stopped (not paused) and displaying a thumbnail, the view scales as its supposed to. I read on another question that YouTubePlayerView is implemented with a SurfaceView. I also read in the docs that SurfaceView behaves differently from a normal view because of how it punches a hole in the screen. I believe because YoutubePlayerView is based off of SurfaceView ,it's not scaling properly. How do I scale the video playing within a YoutubePlayerView properly to match the size of its parent layout during playback?
推荐答案
根据我使用 YouTubePlayerView 和 YouTubePlayerFragment 的经验,我发现使用 Nineoldandroids 或 ViewPropertyAnimator 进行缩放不能正常工作.为了调整播放视频的大小,您必须以编程方式设置布局参数的高度和宽度.在 DraggablePanel 库中,有两个类可以更改顶视图的大小.默认值是 ScaleTransformer,它在播放期间不适用于视频,因为它会将部分正在播放的视频裁剪到视图之外,另一个是 ResizeTransformer.ResizeTransformer 不像 ScaleTransformer 那样平滑,但它有点工作.ResizeTransformer 的问题在于 YouTubePlayerView 的布局有时会在被拖动时剪辑到底部视图下方.然后播放停止,因为它检测到与它重叠的视图.我做出妥协,去掉 DraggablePanel 并为 YouTubePlayerView 的容器编写最大化和最小化方法.
In my experience with working with YouTubePlayerView and YouTubePlayerFragment, I found that scaling with Nineoldandroids or ViewPropertyAnimator does not work properly. In order to adjust the size of the playing video you must set the height and width of the layout parameters programmatically. In the DraggablePanel library there are two classes to change the size of the top view. The default is ScaleTransformer which doesn't work for videos during playback because it crops part of the playing video out of the view, the other is ResizeTransformer. ResizeTransformer isn't as smooth as the ScaleTransformer but it works somewhat. The problem with ResizeTransformer is that the YouTubePlayerView's layout sometimes clips under the bottom view while being dragged. Playback then stops because it detects a view overlapped it. I made the compromise to strip out DraggablePanel and write a maximize and minimize method for YouTubePlayerView's container.
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;
}
这篇关于在播放视频时调整/缩小 YouTubePlayerFragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在播放视频时调整/缩小 YouTubePlayerFragment
基础教程推荐
- iOS - UINavigationController 添加多个正确的项目? 2022-01-01
- 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
- UIImage 在开始时不适合 UIScrollView 2022-01-01
- Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
- navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
- 如何将图像从一项活动发送到另一项活动? 2022-01-01
- SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
- Play 商店的设备兼容性问题 2022-01-01
- Android Volley - 如何动画图像加载? 2022-01-01
- 如何比较两个 NSDate:哪个是最近的? 2022-01-01
