Android 视频视图大小动态调整方法 - updateVideoSize()
该方法是一个私有的 void 方法,用于更新视频的大小。具体的实现逻辑如下:
- 获取当前窗口的 WindowManager 对象。
- 创建一个 DisplayMetrics 对象用于获取屏幕的尺寸信息。
- 使用 getDefaultDisplay() 方法获取屏幕的显示信息,并将其存储在 outMetrics 对象中。
- 从 outMetrics 对象中获取屏幕的宽度和高度,并将其分别存储在 width 和 height 变量中。
- 根据视频的宽高比进行计算,如果视频的宽高比大于 1,则将 topMargin 设置为 200,否则 topMargin 保持为 0。
- 获取 m_glSurfaceView 的布局参数对象,并将其转换为 ConstraintLayout.LayoutParams 类型。
- 设置布局参数对象的 topMargin、leftMargin、rightMargin、width 和 height 属性,分别对应视频视图的上边距、左边距、右边距、宽度和高度。
- 将更新后的布局参数对象重新应用到 m_glSurfaceView 上,从而使视频视图的大小相应地进行更新。
总之,这个方法的作用就是根据屏幕的尺寸和视频的宽高比来动态调整视频视图的大小和位置。
WindowManager manager = this.getWindowManager();
DisplayMetrics outMetrics = new DisplayMetrics();
manager.getDefaultDisplay().getMetrics(outMetrics);
int width = outMetrics.widthPixels;
int height = outMetrics.heightPixels;
int topMargin = 0;
if (m_videoRatio > 1) {
topMargin = 200;
}
ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) m_glSurfaceView.getLayoutParams();
layoutParams.topMargin = topMargin;
layoutParams.leftMargin = 0;
layoutParams.rightMargin = 0;
layoutParams.width = width;
layoutParams.height = (int) (width / m_videoRatio);
m_glSurfaceView.setLayoutParams(layoutParams);
}```
原文地址: https://www.cveoy.top/t/topic/Xk1 著作权归作者所有。请勿转载和采集!