Center Crop an Android VideoView

后端 未结 4 883
既然无缘
既然无缘 2020-12-05 23:46

I am looking for something like the CENTER_CROP in ImageView.ScaleType

Scale the image uniformly (maintain the image\'s aspect ratio) so that both dim

4条回答
  •  温柔的废话
    2020-12-06 00:32

    The simple and easy way if you are using ConstraintLayout.

    XML

    
    
    
        
    
    
    

    then

    In Kotlin:

    videoView.setOnPreparedListener { mediaPlayer ->
        val videoRatio = mediaPlayer.videoWidth / mediaPlayer.videoHeight.toFloat()
        val screenRatio = videoView.width / videoView.height.toFloat()
        val scaleX = videoRatio / screenRatio
        if (scaleX >= 1f) {
            videoView.scaleX = scaleX
        } else {
            videoView.scaleY = 1f / scaleX
        }
    }
    

    See my Java version answer here: https://stackoverflow.com/a/59069357/6255841

    And this worked for me.

提交回复
热议问题