VideoView to match parent height and keep aspect ratio

前端 未结 12 1906
轻奢々
轻奢々 2020-11-28 08:15

I have a VideoView which is set up like this:



        
12条回答
  •  抹茶落季
    2020-11-28 08:49

    I've tried a lot of solutions, while my video was always in 1000*1000 format, so I've created an easy solution for people who know their aspect ratio. First create a VideoView in a RelativeLayout like this:

    
    
          
    
    

    Then before you load the video change the height and with programmatically like this:

        int i = videoView.getHeight() > videoView.getWidth() ? videoView.getHeight() : videoView.getWidth();
        video_holder.setLayoutParams(new ConstraintLayout.LayoutParams(i, i));
    

    Of course this only works with 1:1 aspect ratio's but you could just use your aspect ratio to change either the height or the width.

提交回复
热议问题