Android VideoView 比例缩放 [英] Android VideoView Proportional Scaling

查看:106
本文介绍了Android VideoView 比例缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android的VideoView中,有没有什么办法可以达到ImageView.ScaleType.CENTER_CROP一样的效果?

In Android's VideoView, is there any way to achieve the same effect as ImageView.ScaleType.CENTER_CROP?

也就是说,我希望我的 VideoView 播放视频,使其填满整个屏幕而不会失真.如果视频纵横比不完全适合屏幕,则应对其进行裁剪而不是扭曲.

That is, I want my VideoView to play the video such that it fills the entire screen without distortion. If the video aspect ratio does not exactly fit the screen's, then it should be cropped rather than distorted.

以下解决方案将填满屏幕,但不会保持视频的纵横比:https://stackoverflow.com/a/6927300/1068656

The following solution will fill the screen, but does not maintain the video's aspect ratio: https://stackoverflow.com/a/6927300/1068656

而且这个解决方案保持了视频的纵横比,但不会填满整个屏幕(视频会被缩放,直到较长的一边碰到屏幕边缘,从而在一边引入条纹):https://stackoverflow.com/a/4855315/1068656

And this solution maintains the video's aspect ratio, but will not fill the entire screen (video is scaled until the longer side hits the screen's edge thereby introducing bars on the side): https://stackoverflow.com/a/4855315/1068656

推荐答案

虽然为时已晚,但它可能会帮助其他人寻找同样的问题.以下答案保持纵横比(videoProportion).额外的部分视频视图被手机的视图裁剪.

Although it is too late, but it might help someone else looking for the same problem. The following answer maintains the aspect ratio(videoProportion). The extra part of the videoview is cropped by the Phone's view.

private void setDimension() {
     // Adjust the size of the video
     // so it fits on the screen
     float videoProportion = getVideoProportion();
     int screenWidth = getResources().getDisplayMetrics().widthPixels;
     int screenHeight = getResources().getDisplayMetrics().heightPixels;
     float screenProportion = (float) screenHeight / (float) screenWidth;
     android.view.ViewGroup.LayoutParams lp = videoView.getLayoutParams();

     if (videoProportion < screenProportion) {
         lp.height= screenHeight;
         lp.width = (int) ((float) screenHeight / videoProportion);
     } else {
         lp.width = screenWidth;
         lp.height = (int) ((float) screenWidth * videoProportion);
     }
     videoView.setLayoutParams(lp);
 }

// This method gets the proportion of the video that you want to display.
// I already know this ratio since my video is hardcoded, you can get the  
// height and width of your video and appropriately generate  the proportion  
//    as :height/width 
private float getVideoProportion(){
  return 1.5f;
}

这篇关于Android VideoView 比例缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆