用android写一个视频裁剪适配屏幕大小的方法
时间: 2024-01-27 13:05:03 浏览: 40
好的,以下是一个实现视频裁剪并适配屏幕大小的示例方法:
```java
public static void cropAndFitToScreen(String videoPath, TextureView textureView) {
MediaPlayer mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(videoPath);
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
return;
}
int videoWidth = mediaPlayer.getVideoWidth();
int videoHeight = mediaPlayer.getVideoHeight();
float videoAspectRatio = (float) videoWidth / (float) videoHeight;
int viewWidth = textureView.getWidth();
int viewHeight = textureView.getHeight();
float viewAspectRatio = (float) viewWidth / (float) viewHeight;
// Calculate the crop width and height to maintain video aspect ratio
int cropWidth, cropHeight;
if (videoAspectRatio > viewAspectRatio) {
cropWidth = (int) (videoHeight * viewAspectRatio);
cropHeight = videoHeight;
} else {
cropWidth = videoWidth;
cropHeight = (int) (videoWidth / viewAspectRatio);
}
// Calculate the crop position
int cropX = (videoWidth - cropWidth) / 2;
int cropY = (videoHeight - cropHeight) / 2;
// Set up the texture view
textureView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
SurfaceTexture surfaceTexture = textureView.getSurfaceTexture();
surfaceTexture.setDefaultBufferSize(videoWidth, videoHeight);
Surface surface = new Surface(surfaceTexture);
// Set up the media player with the cropped video surface
mediaPlayer.setSurface(surface);
mediaPlayer.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
mediaPlayer.setVideoCrop(cropX, cropY, cropWidth, cropHeight);
// Start the video playback
mediaPlayer.start();
}
```
这个方法首先创建了一个MediaPlayer对象,并设置其数据源为视频文件路径。然后,它获取视频的宽度和高度,并计算出视频的宽高比。接下来,它获取TextureView的宽度和高度,并计算出TextureView的宽高比。然后,它根据视频和TextureView的宽高比,计算出裁剪后的视频宽度、高度和位置。最后,它设置TextureView的布局参数,创建一个SurfaceTexture对象,并将其作为渲染目标传递给MediaPlayer。
这个示例方法可以让你裁剪视频并适配到TextureView上,但你需要在调用这个方法之前确保TextureView已经被正确地添加到布局中。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)