在 Leanback 上使用 VideoFragment 播放视频时如何设置缩放模式拉伸到全屏

How to set the scale mode to stretch to full screen when using the VideoFragment to play a video on Leanback

Google 的 Leanback showcase 视频播放器没有 ExoPlayer

Google 的 Leanback showcase 带有 ExoPlayer

的视频播放器

我已经尝试了 google 的 Leanback 展示柜,它也在 "fit to screen" 模式下播放视频,两侧有黑条。我也无法在 API 文档的任何位置找到更改缩放模式的选项

我必须查看 VideoFragment 来源才能弄清楚这一点。 VideoFragment 有一个简单的 SurfaceView 作为其布局的根元素,您所要做的就是使 SurfaceView 匹配父级(即设备屏幕)的宽度和高度。为此,只需重写 onVideoSizeChanged 并使用 getSurfaceView 获取对 VideoFragment.

中使用的包私有 SurfaceView 实例的引用
@Override
protected void onVideoSizeChanged(int width, int height) {
    switch (scaleMode) {
        //Flag indicates that this video should stretch to screen
        case MediaMetaData.SCALE_MODE_STRETCH:  
            View rootView = getView();
            SurfaceView surfaceView = getSurfaceView();
            ViewGroup.LayoutParams params = surfaceView.getLayoutParams();
            params.height = rootView.getHeight();
            params.width = rootView.getWidth();
            surfaceView.setLayoutParams(params);
            break;
        //When the video shouldn't stretch, just invoke super to have the VideoFragment's default behavior which is fit to screen
        default:                            
            super.onVideoSizeChanged(width, height);
    }
}