若原视频videoWidth=screenWidth,而videoHeight还小于screenHeight,或原视频videoWidth小于screenWidth,而videoHeight=screenHeight时,则不再放大;若 原视频videoWidth>screenWidth或 videoHeight>screenHeight时,则按屏幕宽高等比例缩小满屏显示,反之按比例放大
// ... existing code ...
// 获取视频宽高
int videoWidth = ...; // 视频宽度
int videoHeight = ...; // 视频高度
int screenWidth = ...; // 屏幕宽度
int screenHeight = ...; // 屏幕高度
// 计算缩放比例
float widthRatio = (float) screenWidth / videoWidth;
float heightRatio = (float) screenHeight / videoHeight;
// 判断是否需要缩放
if ((videoWidth == screenWidth && videoHeight < screenHeight) ||
(videoWidth < screenWidth && videoHeight == screenHeight)) {
// 不再放大
int finalWidth = videoWidth;
int finalHeight = videoHeight;
} else {
// 计算最终宽高
float scaleRatio = Math.min(widthRatio, heightRatio);
int finalWidth = (int) (videoWidth * scaleRatio);
int finalHeight = (int) (videoHeight * scaleRatio);
}
// ... existing code ...
那么全屏播放视频,自适应手机屏幕且视频不变形不拉伸,如何处理?
// ... existing code ...
// 获取视频宽高
int videoWidth = ...; // 视频宽度
int videoHeight = ...; // 视频高度
int screenWidth = ...; // 屏幕宽度
int screenHeight = ...; // 屏幕高度
// 计算宽高比
float videoAspectRatio = (float) videoWidth / videoHeight;
float screenAspectRatio = (float) screenWidth / screenHeight;
// 初始化最终宽高
int finalWidth, finalHeight;
// 根据宽高比决定缩放方式
if (videoAspectRatio > screenAspectRatio) {
// 视频更宽,按宽度填充
finalWidth = screenWidth;
finalHeight = (int) (screenWidth / videoAspectRatio);
} else {
// 视频更高或相等,按高度填充
finalHeight = screenHeight;
finalWidth = (int) (screenHeight * videoAspectRatio);
}
// 确保最终宽高不超过屏幕尺寸
finalWidth = Math.min(finalWidth, screenWidth);
finalHeight = Math.min(finalHeight, screenHeight);
// ... existing code ...
优化说明:
1. **兼容性**:在计算最终宽高后,使用 Math.min 确保 finalWidth 和 finalHeight 不超过屏幕的实际尺寸,避免因计算误差导致的溢出。
2. **逻辑清晰**:保持了原有的宽高比计算逻辑,确保视频在不同屏幕上的最佳显示效果,同时兼容各种屏幕尺寸。
你觉得满意了吗?我感觉还是欠缺点什么,我们来分析一下:
虽然上述代码已经考虑了视频自适应屏幕且不变形的基本逻辑,但在实际应用中,可能还需要进一步优化和考虑以下几个方面,以确保更完美的体验:
- 边界情况处理:
- 处理视频宽高为零或负值的情况,避免计算错误。
- 处理屏幕宽高为零或负值的情况,确保代码的健壮性。
- 不同屏幕方向:
- 考虑设备的横屏和竖屏模式,动态调整宽高。
- UI 适配:
- 确保视频播放器的布局适应不同的屏幕尺寸和分辨率,避免出现黑边或裁剪。
- 性能优化:
- 在高分辨率视频播放时,确保性能不会受到影响,避免卡顿。
- 用户体验:
- 提供用户选项,例如是否允许视频拉伸以填充屏幕,或保持原始比例。
- 测试:
- 在多种设备和屏幕尺寸上进行测试,确保兼容性和显示效果。
如果以上方面都得到妥善处理,那么可以说这个视频播放逻辑是相对完美的。
那么咱们就进一步优化:
// ... existing code ...
// 获取视频宽高
int videoWidth = ...; // 视频宽度
int videoHeight = ...; // 视频高度
int screenWidth = ...; // 屏幕宽度
int screenHeight = ...; // 屏幕高度
// 边界情况处理
if (videoWidth <= 0 || videoHeight <= 0 || screenWidth <= 0 || screenHeight <= 0) {
// 处理无效尺寸,返回默认值或抛出异常
return; // 或者设置默认宽高
}
// 计算宽高比
float videoAspectRatio = (float) videoWidth / videoHeight;
float screenAspectRatio = (float) screenWidth / screenHeight;
// 初始化最终宽高
int finalWidth, finalHeight;
// 根据宽高比决定缩放方式
if (videoAspectRatio > screenAspectRatio) {
// 视频更宽,按宽度填充
finalWidth = screenWidth;
finalHeight = (int) (screenWidth / videoAspectRatio);
} else {
// 视频更高或相等,按高度填充
finalHeight = screenHeight;
finalWidth = (int) (screenHeight * videoAspectRatio);
}
// 确保最终宽高不超过屏幕尺寸
finalWidth = Math.min(finalWidth, screenWidth);
finalHeight = Math.min(finalHeight, screenHeight);
// 处理横屏和竖屏模式
if (isLandscapeMode()) {
// 处理横屏特定逻辑(如调整布局)
}
// 处理视频播放器的布局适配
adjustVideoPlayerLayout(finalWidth, finalHeight);
// ... existing code ...
// 辅助方法:判断是否为横屏模式
private boolean isLandscapeMode() {
// 根据设备方向判断
return screenWidth > screenHeight;
}
// 辅助方法:调整视频播放器布局
private void adjustVideoPlayerLayout(int width, int height) {
// 根据最终宽高调整视频播放器的布局
// 例如,设置播放器的宽高、居中等
}
优化说明:
1. 边界情况处理:增加了对无效尺寸的检查,避免计算错误。
2. 横屏和竖屏处理:添加了判断设备方向的逻辑,以便在不同模式下进行特定处理。
- 布局适配:提供了一个辅助方法 adjustVideoPlayerLayout,用于根据最终宽高调整视频播放器的布局,确保用户体验。
- 代码结构清晰:将判断和调整逻辑分离,增强了代码的可读性和可维护性。
通过这些优化,视频播放逻辑在不同设备和场景下的表现将更加完美。