Resize/Scaling down a YouTubePlayerFragment while video is playing

三世轮回 提交于 2019-11-30 10:46:55

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!