Android VideoView Proportional Scaling

本秂侑毒 提交于 2019-12-18 04:34:17

问题


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

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.

The following solution will fill the screen, but does not maintain the video's aspect ratio: https://stackoverflow.com/a/6927300/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


回答1:


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;
}


来源:https://stackoverflow.com/questions/11736311/android-videoview-proportional-scaling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!